source: FarmatikoServices/Services/PHService.cs@ e0cdea2

Last change on this file since e0cdea2 was e0cdea2, checked in by Dimitar Slezenkovski <dslezenkovski@…>, 20 months ago

Fix all bugs

  • Property mode set to 100644
File size: 10.7 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 //phead.Medicines = pharmacyHead.Medicines;
89 if (pharmacyHead.Medicines.Count() == 0)
90 {
91 phead.Medicines = null;
92 int PHMId = phead.Medicines.Select(x => x.Id).Single();
93 int phId = phead.Medicines.Select(x => x.PheadId).Single();
94 int medId = phead.Medicines.Select(x => x.MedicineId).Single();
95 _iPHRepo.DeletePHMedicine(PHMId, phId, medId);
96 return;
97 }
98 if (phead.Medicines != null && phead.Medicines.Count() > 0)
99 {
100 foreach (var med in pharmacyHead.Medicines)
101 {
102 Medicine medicine = new Medicine()
103 {
104 Name = med.Name,
105 Form = med.Form,
106 Manufacturer = med.Manufacturer,
107 Medicines = med.Medicines,
108 Packaging = med.Packaging,
109 Price = med.Price,
110 Strength = med.Strength,
111 WayOfIssuing = med.WayOfIssuing
112 };
113
114 PharmacyHeadMedicine phm = new PharmacyHeadMedicine()
115 {
116 PheadId = phead.Id,
117 Head = phead,
118 MedicineId = med.Id,
119 Medicine = medicine
120 };
121
122 bool ifExists = phead.Medicines.Contains(phm);
123 if (!ifExists)
124 list.Add(phm);
125
126 }
127 }
128 else
129 {
130 foreach (var med in pharmacyHead.Medicines)
131 {
132 PharmacyHead head1 = new PharmacyHead()
133 {
134 Id = pharmacyHead.Id,
135 Name = pharmacyHead.Name,
136 Email = pharmacyHead.Email,
137 Password = pharmacyHead.Password
138 };
139 PharmacyHeadMedicine phMed = new PharmacyHeadMedicine()
140 {
141 Head = head1,
142 Medicine = med
143 };
144 list.Add(phMed);
145 }
146 }
147
148
149 phead.Medicines = list;
150
151 await _iPHRepo.UpdatePharmacyHead(phead);
152
153 }
154 }
155
156 PharmacyHead head = new PharmacyHead()
157 {
158 Name = pharmacyHead.Name,
159 Email = pharmacyHead.Email,
160 Password = pharmacyHead.Password
161 };
162
163 if (pharmacyHead.Pharmacies != null)
164 {
165 if (pharmacyHead.Pharmacies.Count() > 0)
166 {
167 phead.Pharmacies = pharmacyHead.Pharmacies;
168 head.Pharmacies = pharmacyHead.Pharmacies;
169 }
170 }
171
172 if (!phead.Equals(head))
173 {
174 await _iPHRepo.UpdatePharmacyHead(head);
175 }
176 else throw new Exception("Cannot update pharmacy head since there was no changes.");
177 }
178 else throw new Exception("PharmacyHead has a null value.");
179 }
180 public async Task<bool> Add(PharmacyHeadDto pharmacyHead)
181 {
182 if (pharmacyHead != null)
183 {
184 PharmacyHead head = new PharmacyHead()
185 {
186 Name = pharmacyHead.Name,
187 Email = pharmacyHead.Email,
188 Password = pharmacyHead.Password,
189 Pharmacies = null,
190 Medicines = null
191 };
192 await _iPHRepo.Add(head);
193 return true;
194 }
195 return false;
196 }
197
198 public async Task<bool> Remove(int id)
199 {
200 PharmacyHead Phead = await _iPHRepo.GetPharmacyHeadByIdAsync(id);
201 if (Phead != null && id >= 0)
202 {
203 Phead.DeletedOn = DateTime.UtcNow;
204 await _iPHRepo.Remove(Phead);
205 return true;
206 }
207 return false;
208 }
209
210 public async Task<bool> RemoveClaimingRequest(int id)
211 {
212 if (id >= 0)
213 {
214 await _iPHRepo.RemoveClaimingRequest(id);
215 return true;
216 }
217 return false;
218 }
219
220 public PharmacyHeadDto GetPharmacyHead(string userName)
221 {
222 if (userName != null)
223 {
224 var Phead = _iPHRepo.GetPharmacyHeadByUserName(userName);
225 List<PharmacyHeadMedicine> PHMedicines = _iPHRepo.GetPharmacyHeadMedicines(userName);
226 List<Medicine> Medicines = _repository.GetMedicines().ToList();
227 List<Medicine> MedicineList = new List<Medicine>();
228
229 var user = _repository.GetRole(userName);
230
231
232 if (user.UserRole.ToString().Equals("Admin"))
233 {
234 List<Pharmacy> pharmacies = new List<Pharmacy>();
235 pharmacies = Phead.Pharmacies;
236 var Admin = new PharmacyHeadDto()
237 {
238 Id = user.Id,
239 Email = user.Email,
240 Name = user.Name,
241 Password = user.Password,
242 Pharmacies = pharmacies
243 };
244
245 return Admin;
246 }
247 else
248 {
249 foreach (var med in Medicines)
250 {
251 if (Phead.Medicines == null || Phead.Medicines.Count() == 0)
252 break;
253 var PHMObj = Phead.Medicines.Where(x => x.MedicineId == med.Id).SingleOrDefault();
254 if (PHMObj == null)
255 {
256 continue;
257 }
258 if (PHMObj.MedicineId == med.Id)
259 {
260 var medicine = new Medicine()
261 {
262 Id = med.Id,
263 Name = med.Name,
264 Strength = med.Strength,
265 Form = med.Form,
266 WayOfIssuing = med.WayOfIssuing,
267 Manufacturer = med.Manufacturer,
268 Price = med.Price,
269 Packaging = med.Packaging
270 };
271 MedicineList.Add(medicine);
272 }
273 }
274 }
275
276 PharmacyHeadDto pharmacyHead = new PharmacyHeadDto()
277 {
278 Id = Phead.Id,
279 Medicines = MedicineList,
280 Pharmacies = Phead.Pharmacies,
281 Email = Phead.Email,
282 Name = Phead.Name,
283 Password = Phead.Password
284 };
285 return pharmacyHead;
286 }
287 else throw new Exception("Username is null.");
288 }
289 }
290}
Note: See TracBrowser for help on using the repository browser.