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