source: FarmatikoServices/Services/PHService.cs@ 6e6f04b

Last change on this file since 6e6f04b was 8eb1e21, checked in by Dimitar Slezenkovski <dslezenkovski@…>, 4 years ago

Fix delete pharmacy head method.

  • Property mode set to 100644
File size: 11.3 KB
Line 
1using FarmatikoData.DTOs;
2using FarmatikoData.FarmatikoRepoInterfaces;
3using FarmatikoData.Models;
4using FarmatikoServices.FarmatikoServiceInterfaces;
5using System;
6using System.Collections.Generic;
7using System.Linq;
8using System.Threading.Tasks;
9
10namespace FarmatikoServices.Services
11{
12 public class PHService : IPHService
13 {
14 private readonly IPHRepo _iPHRepo;
15 private readonly IRepository _repository;
16 public PHService(IPHRepo iPHRepo, IRepository repository)
17 {
18 _iPHRepo = iPHRepo;
19 _repository = repository;
20 }
21
22 public async Task<bool> ClaimPharmacy(RequestPharmacyHead pharmacy)
23 {
24 if (pharmacy != null)
25 {
26 await _iPHRepo.ClaimPharmacy(pharmacy);
27 return true;
28 }
29 return false;
30 }
31
32 public async Task<PharmacyHead> GetPharmacyHeadByIdAsync(int id)
33 {
34 PharmacyHead Phead = null;
35 if (id >= 0)
36 Phead = await _iPHRepo.GetPharmacyHeadByIdAsync(id);
37 if (Phead != null)
38 return Phead;
39 throw new Exception("No data found.");
40 }
41
42 public async Task<IEnumerable<PharmacyHead>> GetPharmacyHeadInfo()
43 {
44 var PHeads = await _iPHRepo.GetPharmacyHeadInfo();
45 if (PHeads != null)
46 return PHeads;
47 throw new Exception("No Pharmacy heads found.");
48 }
49
50 public async Task<int> Login(PharmacyHead pharmacyHead)
51 {
52 var PHead = await _iPHRepo.GetPharmacyHeadByIdAsync(pharmacyHead.Id);
53 if (PHead.Password.Equals(pharmacyHead.Password))
54 return PHead.Id;
55 return -1;
56 }
57
58
59
60 public async Task UpdatePharmacyHead(PharmacyHeadDto pharmacyHead)
61 {
62 if (pharmacyHead != null)
63 {
64 var phead = _iPHRepo.GetPharmacyHead(pharmacyHead.Email);
65
66 phead.Medicines = _repository.GetPHMedicines(phead.Email).ToList();
67
68 var phmeds = await _repository.GetAllPHMedicines();
69
70 List<Medicine> medicines = _repository.GetMedicines().ToList();
71
72 List<Medicine> PHMedicines = new List<Medicine>();
73
74 List<PharmacyHeadMedicine> list = new List<PharmacyHeadMedicine>();
75
76
77 if (pharmacyHead.Medicines != null && pharmacyHead.Medicines.Count() > 0)
78 {
79 foreach (var med in phead.Medicines)
80 {
81 var medicine = medicines.Where(x => x.Id == med.MedicineId).FirstOrDefault();
82 if (medicine != null)
83 PHMedicines.Add(medicine);
84 }
85
86 if (!pharmacyHead.Medicines.Equals(PHMedicines))
87 {
88 /*
89 * USELESS
90 * if (pharmacyHead.Medicines.Count() == 0)
91 {
92 phead.Medicines = null;
93 int PHMId = phead.Medicines.Select(x => x.Id).Single();
94 int phId = phead.Medicines.Select(x => x.PheadId).Single();
95 int medId = phead.Medicines.Select(x => x.MedicineId).Single();
96 _iPHRepo.DeletePHMedicine(PHMId, phId, medId);
97 return;
98 }*/
99 if (phead.Medicines != null && phead.Medicines.Count() > 0)
100 {
101 foreach (var med in pharmacyHead.Medicines)
102 {
103 Medicine medicine = new Medicine()
104 {
105 Name = med.Name,
106 Form = med.Form,
107 Manufacturer = med.Manufacturer,
108 Medicines = med.Medicines,
109 Packaging = med.Packaging,
110 Price = med.Price,
111 Strength = med.Strength,
112 WayOfIssuing = med.WayOfIssuing
113 };
114
115 PharmacyHeadMedicine phm = new PharmacyHeadMedicine()
116 {
117 PheadId = phead.Id,
118 Head = phead,
119 MedicineId = med.Id,
120 Medicine = medicine
121 };
122
123 bool ifExists = phead.Medicines.Contains(phm);
124 if (!ifExists)
125 list.Add(phm);
126
127 }
128 }
129 else
130 {
131 foreach (var med in pharmacyHead.Medicines)
132 {
133 PharmacyHead head1 = new PharmacyHead()
134 {
135 Id = pharmacyHead.Id,
136 Name = pharmacyHead.Name,
137 Email = pharmacyHead.Email,
138 Password = pharmacyHead.Password
139 };
140 PharmacyHeadMedicine phMed = new PharmacyHeadMedicine()
141 {
142 Head = head1,
143 Medicine = med
144 };
145 list.Add(phMed);
146 }
147 }
148
149
150 phead.Medicines = list;
151
152 await _iPHRepo.UpdatePharmacyHead(phead);
153
154 }
155 }
156 PharmacyHead head = new PharmacyHead()
157 {
158 Name = pharmacyHead.Name,
159 Email = pharmacyHead.Email,
160 Password = pharmacyHead.Password,
161 Medicines = phead.Medicines,
162 Pharmacies = phead.Pharmacies
163 };
164 if (!phead.Name.Equals(head.Name) && !phead.Password.Equals(head.Email))
165 {
166 await _iPHRepo.UpdatePharmacyHead(head);
167 }
168 List<Pharmacy> pharmacies = new List<Pharmacy>();
169 pharmacies = phead.Pharmacies;
170 if (head.Pharmacies != null && pharmacyHead.Pharmacies != null)
171 {
172 if (head.Pharmacies.Count() > 0 && pharmacyHead.Pharmacies.Count() > 0)
173 {
174 foreach (var pharmacy in pharmacyHead.Pharmacies)
175 {
176 if (!head.Pharmacies.Contains(pharmacy))
177 {
178 pharmacy.PheadId = phead.Id;
179 pharmacy.PharmacyHead = phead;
180 pharmacies.Add(pharmacy);
181 }
182 }
183 head.Pharmacies = pharmacies;
184 await _iPHRepo.UpdatePharmacyHead(head);
185 }
186 }
187 }
188 else throw new Exception("Cannot update pharmacy head since there was no changes.");
189 }
190 public async Task<bool> Add(PharmacyHeadDto pharmacyHead)
191 {
192 if (pharmacyHead != null)
193 {
194 PharmacyHead head = new PharmacyHead()
195 {
196 Name = pharmacyHead.Name,
197 Email = pharmacyHead.Email,
198 Password = pharmacyHead.Password,
199 Pharmacies = null,
200 Medicines = null
201 };
202 await _iPHRepo.Add(head);
203 return true;
204 }
205 return false;
206 }
207
208 public async Task<bool> Remove(int id)
209 {
210 PharmacyHead Phead = await _iPHRepo.GetPharmacyHeadByIdAsync(id);
211 if (Phead != null && id >= 0)
212 {
213 Phead.DeletedOn = DateTime.UtcNow;
214 await _iPHRepo.Remove(Phead);
215 return true;
216 }
217 return false;
218 }
219
220 public async Task<bool> RemoveClaimingRequest(int id)
221 {
222 if (id >= 0)
223 {
224 await _iPHRepo.RemoveClaimingRequest(id);
225 return true;
226 }
227 return false;
228 }
229
230 public PharmacyHeadDto GetPharmacyHead(string userName)
231 {
232 if (userName != null)
233 {
234 var Phead = _iPHRepo.GetPharmacyHeadByUserName(userName);
235 List<PharmacyHeadMedicine> PHMedicines = _iPHRepo.GetPharmacyHeadMedicines(userName);
236 List<Medicine> Medicines = _repository.GetMedicines().ToList();
237 List<Medicine> MedicineList = new List<Medicine>();
238
239 var user = _repository.GetRole(userName);
240
241
242 if (user.UserRole.ToString().Equals("Admin"))
243 {
244 List<Pharmacy> pharmacies = new List<Pharmacy>();
245 pharmacies = Phead.Pharmacies;
246 var Admin = new PharmacyHeadDto()
247 {
248 Id = user.Id,
249 Email = user.Email,
250 Name = user.Name,
251 Password = user.Password,
252 Pharmacies = pharmacies
253 };
254
255 return Admin;
256 }
257 else
258 {
259 foreach (var med in Medicines)
260 {
261 if (Phead.Medicines == null || Phead.Medicines.Count() == 0)
262 break;
263 var PHMObj = Phead.Medicines.Where(x => x.MedicineId == med.Id).SingleOrDefault();
264 if (PHMObj == null)
265 {
266 continue;
267 }
268 if (PHMObj.MedicineId == med.Id)
269 {
270 var medicine = new Medicine()
271 {
272 Id = med.Id,
273 Name = med.Name,
274 Strength = med.Strength,
275 Form = med.Form,
276 WayOfIssuing = med.WayOfIssuing,
277 Manufacturer = med.Manufacturer,
278 Price = med.Price,
279 Packaging = med.Packaging
280 };
281 MedicineList.Add(medicine);
282 }
283 }
284 }
285
286 PharmacyHeadDto pharmacyHead = new PharmacyHeadDto()
287 {
288 Id = Phead.Id,
289 Medicines = MedicineList,
290 Pharmacies = Phead.Pharmacies,
291 Email = Phead.Email,
292 Name = Phead.Name,
293 Password = Phead.Password
294 };
295 return pharmacyHead;
296 }
297 else throw new Exception("Username is null.");
298 }
299 }
300}
Note: See TracBrowser for help on using the repository browser.