source: FarmatikoData/FarmatikoRepo/PHRepo.cs

Last change on this file was 8eb1e21, checked in by Dimitar Slezenkovski <dslezenkovski@…>, 22 months ago

Fix delete pharmacy head method.

  • Property mode set to 100644
File size: 6.0 KB
Line 
1using FarmatikoData.FarmatikoRepoInterfaces;
2using FarmatikoData.Models;
3using Microsoft.EntityFrameworkCore;
4using System.Collections.Generic;
5using System.Linq;
6using System.Threading.Tasks;
7
8namespace FarmatikoData.FarmatikoRepo
9{
10 public class PHRepo : IPHRepo
11 {
12 private readonly FarmatikoDataContext _context;
13 public PHRepo(FarmatikoDataContext context)
14 {
15 _context = context;
16 }
17 //GET
18 public async Task<PharmacyHead> GetPharmacyHeadByIdAsync(int id)
19 {
20 var Phead = await _context.PharmacyHeads.Where(x => x.Id == id).FirstOrDefaultAsync();
21 return Phead;
22 }
23
24 public async Task<IEnumerable<PharmacyHead>> GetPharmacyHeadInfo()
25 {
26 var PHeadInfo = await _context.PharmacyHeads.Take(10)
27 .Where(x => x.DeletedOn == null)
28 .Select(x => new PharmacyHead
29 {
30 Id = x.Id,
31 Name = x.Name,
32 Email = x.Email,
33 Password = x.Password,
34 Pharmacies = x.Pharmacies,
35 Medicines = x.Medicines
36
37 }).ToListAsync();
38 return PHeadInfo;
39 }
40 //POST
41 public async Task UpdatePharmacyHead(PharmacyHead pharmacyHead)
42 {
43 var user = await _context.Users.Where(x => x.Email == pharmacyHead.Email).FirstOrDefaultAsync();
44 var EditedPHead = await _context.PharmacyHeads.Where(x => x.Email.Equals(pharmacyHead.Email)).FirstOrDefaultAsync();
45
46 /*if (!EditedPHead.Email.Equals(pharmacyHead.Email) && !user.Email.Equals(pharmacyHead.Email))
47 {
48 EditedPHead.Email = pharmacyHead.Email;
49 user.Email = pharmacyHead.Email;
50 }*/
51
52 if (!EditedPHead.Name.Equals(pharmacyHead.Name) || !user.Name.Equals(pharmacyHead.Name))
53 {
54 EditedPHead.Name = pharmacyHead.Name;
55 user.Name = pharmacyHead.Name;
56 }
57
58 if (!EditedPHead.Password.Equals(pharmacyHead.Password) || !user.Password.Equals(pharmacyHead.Password))
59 {
60 EditedPHead.Password = pharmacyHead.Password;
61 user.Password = pharmacyHead.Password;
62 }
63 foreach(var pharmacy in pharmacyHead.Pharmacies)
64 {
65 if (!EditedPHead.Pharmacies.Contains(pharmacy))
66 {
67 pharmacy.PheadId = EditedPHead.Id;
68 pharmacy.PharmacyHead = EditedPHead;
69 EditedPHead.Pharmacies.Add(pharmacy);
70 }
71 }
72 _context.Entry(EditedPHead).State = EntityState.Modified;
73
74 _context.SaveChanges();
75 }
76 public async Task ClaimPharmacy(RequestPharmacyHead pharmacy)
77 {
78 var phead = _context.PharmacyHeads.Where(x => x.Email.Equals(pharmacy.Head.Email)).FirstOrDefault();
79 pharmacy.Head = phead;
80 await _context.PHRequests.AddAsync(pharmacy);
81 await _context.SaveChangesAsync();
82 }
83 public async Task Add(PharmacyHead pharmacyHead)
84 {
85 await _context.PharmacyHeads.AddAsync(pharmacyHead);
86 await _context.SaveChangesAsync();
87 }
88
89 public async Task Remove(PharmacyHead phead)
90 {
91 var Phead = await _context.PharmacyHeads.Where(x => x.Id == phead.Id).FirstOrDefaultAsync();
92 Phead.DeletedOn = phead.DeletedOn;
93 await _context.SaveChangesAsync();
94 }
95
96 public async Task RemoveClaimingRequest(int id)
97 {
98 var req = await _context.PHRequests.Where(r => r.Id == id).FirstOrDefaultAsync();
99 _context.PHRequests.Remove(req);
100 await _context.SaveChangesAsync();
101 }
102
103
104 public PharmacyHead GetPharmacyHeadByUserName(string userName)
105 {
106
107
108 var PHead = _context.PharmacyHeads
109 .Where(x => x.Email.Equals(userName))
110 .Select(x => new PharmacyHead
111 {
112 Email = x.Email,
113 Name = x.Name,
114 Password = x.Password,
115 Medicines = x.Medicines,
116 Pharmacies = x.Pharmacies
117 }).FirstOrDefault();
118
119
120
121 return PHead;
122 }
123
124 public List<PharmacyHeadMedicine> GetPharmacyHeadMedicines(string email)
125 {
126 var Phead = _context.PharmacyHeads.Where(x => x.Email.Equals(email)).FirstOrDefault();
127 var Medicines = _context.PharmacyHeadMedicines.Select(x => new PharmacyHeadMedicine
128 {
129 PheadId = x.PheadId,
130 Head = x.Head,
131 MedicineId = x.MedicineId,
132 Medicine = x.Medicine
133 }).ToList();
134 if (Medicines == null || Medicines == default)
135 Medicines = null;
136 var meds = Medicines.Where(x => x.PheadId == Phead.Id).ToList();
137
138 return meds;
139 }
140
141 public IEnumerable<PharmacyHead> GetPharmacyHeads()
142 {
143 var heads = _context.PharmacyHeads.ToList();
144 return heads;
145 }
146
147 public PharmacyHead GetPharmacyHead(string head)
148 {
149 var phead = _context.PharmacyHeads.Where(x => x.Email.Equals(head)).Include(x => x.Pharmacies).FirstOrDefault();
150 return phead;
151 }
152
153 public List<Pharmacy> GetPharmacies()
154 {
155 var pharms = _context.Pharmacies.ToList();
156 return pharms;
157 }
158
159 public void DeletePHMedicine(int id, int phId, int medId)
160 {
161 var PH = _context.PharmacyHeadMedicines.Where(x => x.PheadId == phId).Single();
162 var PHMed = _context.PharmacyHeadMedicines.Where(x => x.MedicineId == medId).Single();
163 var PHID = _context.PharmacyHeadMedicines.Where(x => x.Id == id).Single();
164 _context.PharmacyHeadMedicines.Remove(PHID);
165 _context.SaveChanges();
166 }
167 }
168}
Note: See TracBrowser for help on using the repository browser.