source: FarmatikoServices/Services/PHService.cs@ f554983

Last change on this file since f554983 was f554983, checked in by Dimitar Slezenkovski <dslezenkovski@…>, 3 years ago

Add cron job for updating data, with Quartz.NET

  • Property mode set to 100644
File size: 11.6 KB
RevLine 
[db484c9]1using FarmatikoData.DTOs;
2using FarmatikoData.FarmatikoRepoInterfaces;
[1454207]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;
[1db5673]15 private readonly IRepository _repository;
16 public PHService(IPHRepo iPHRepo, IRepository repository)
[1454207]17 {
18 _iPHRepo = iPHRepo;
[1db5673]19 _repository = repository;
[1454207]20 }
21
[6f203af]22 public async Task<bool> ClaimPharmacy(RequestPharmacyHead pharmacy)
[1454207]23 {
[6f203af]24 if (pharmacy != null)
25 {
26 await _iPHRepo.ClaimPharmacy(pharmacy);
27 return true;
28 }
29 return false;
[1454207]30 }
31
[6f203af]32 public async Task<PharmacyHead> GetPharmacyHeadByIdAsync(int id)
[1454207]33 {
[6f203af]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
[d23bf72]42 public async Task<IEnumerable<PharmacyHead>> GetPharmacyHeadInfo()
[6f203af]43 {
44 var PHeads = await _iPHRepo.GetPharmacyHeadInfo();
45 if (PHeads != null)
46 return PHeads;
47 throw new Exception("No Pharmacy heads found.");
[1454207]48 }
49
50 public async Task<int> Login(PharmacyHead pharmacyHead)
51 {
[6f203af]52 var PHead = await _iPHRepo.GetPharmacyHeadByIdAsync(pharmacyHead.Id);
[1454207]53 if (PHead.Password.Equals(pharmacyHead.Password))
54 return PHead.Id;
55 return -1;
56 }
57
58
[1db5673]59
[db484c9]60 public async Task UpdatePharmacyHead(PharmacyHeadDto pharmacyHead)
[1454207]61 {
62 if (pharmacyHead != null)
[1db5673]63 {
64 var phead = _iPHRepo.GetPharmacyHead(pharmacyHead.Email);
65
[db484c9]66 phead.Medicines = _repository.GetPHMedicines(phead.Email).ToList();
[e0cdea2]67
68 var phmeds = await _repository.GetAllPHMedicines();
69
[db484c9]70 List<Medicine> medicines = _repository.GetMedicines().ToList();
[e0cdea2]71
72 List<Medicine> PHMedicines = new List<Medicine>();
73
[db484c9]74 List<PharmacyHeadMedicine> list = new List<PharmacyHeadMedicine>();
[68454c6]75
[e0cdea2]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 }
[f554983]85 foreach(var phMed in PHMedicines)
[1db5673]86 {
[f554983]87 if (!pharmacyHead.Medicines.Contains(phMed))
[db484c9]88 {
[f554983]89 /*
90 * USELESS
91 * if (pharmacyHead.Medicines.Count() == 0)
[0a694bb]92 {
[f554983]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)
[e0cdea2]103 {
[f554983]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 }
[e0cdea2]129 }
[f554983]130 else
[e0cdea2]131 {
[f554983]132 foreach (var med in pharmacyHead.Medicines)
[e0cdea2]133 {
[f554983]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 }
[e0cdea2]148 }
[0a694bb]149
[e0cdea2]150
[f554983]151 phead.Medicines = list;
[1db5673]152
[f554983]153 await _iPHRepo.UpdatePharmacyHead(phead);
[db484c9]154
[f554983]155 }
[0a694bb]156 }
[f554983]157
[1db5673]158 }
[db484c9]159 PharmacyHead head = new PharmacyHead()
160 {
161 Name = pharmacyHead.Name,
162 Email = pharmacyHead.Email,
[8eb1e21]163 Password = pharmacyHead.Password,
164 Medicines = phead.Medicines,
165 Pharmacies = phead.Pharmacies
[db484c9]166 };
[8eb1e21]167 if (!phead.Name.Equals(head.Name) && !phead.Password.Equals(head.Email))
[e0cdea2]168 {
[8eb1e21]169 await _iPHRepo.UpdatePharmacyHead(head);
[e0cdea2]170 }
[8eb1e21]171 List<Pharmacy> pharmacies = new List<Pharmacy>();
172 pharmacies = phead.Pharmacies;
173 if (head.Pharmacies != null && pharmacyHead.Pharmacies != null)
[8e74e2f]174 {
[8eb1e21]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 }
[8e74e2f]189 }
[1db5673]190 }
[8eb1e21]191 else throw new Exception("Cannot update pharmacy head since there was no changes.");
[1454207]192 }
[db484c9]193 public async Task<bool> Add(PharmacyHeadDto pharmacyHead)
[6f203af]194 {
195 if (pharmacyHead != null)
196 {
[db484c9]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);
[6f203af]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 }
[d23bf72]232
[db484c9]233 public PharmacyHeadDto GetPharmacyHead(string userName)
[d23bf72]234 {
235 if (userName != null)
236 {
[1db5673]237 var Phead = _iPHRepo.GetPharmacyHeadByUserName(userName);
238 List<PharmacyHeadMedicine> PHMedicines = _iPHRepo.GetPharmacyHeadMedicines(userName);
239 List<Medicine> Medicines = _repository.GetMedicines().ToList();
[db484c9]240 List<Medicine> MedicineList = new List<Medicine>();
[1db5673]241
242 var user = _repository.GetRole(userName);
243
244
245 if (user.UserRole.ToString().Equals("Admin"))
246 {
[db484c9]247 List<Pharmacy> pharmacies = new List<Pharmacy>();
248 pharmacies = Phead.Pharmacies;
249 var Admin = new PharmacyHeadDto()
[1db5673]250 {
251 Id = user.Id,
252 Email = user.Email,
253 Name = user.Name,
[8e74e2f]254 Password = user.Password,
[db484c9]255 Pharmacies = pharmacies
[1db5673]256 };
257
258 return Admin;
259 }
[db484c9]260 else
[1db5673]261 {
262 foreach (var med in Medicines)
263 {
[7d80751]264 if (Phead.Medicines == null || Phead.Medicines.Count() == 0)
265 break;
[db484c9]266 var PHMObj = Phead.Medicines.Where(x => x.MedicineId == med.Id).SingleOrDefault();
[8e74e2f]267 if (PHMObj == null)
[1db5673]268 {
[8e74e2f]269 continue;
270 }
[1db5673]271 if (PHMObj.MedicineId == med.Id)
272 {
[8e74e2f]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 };
[db484c9]284 MedicineList.Add(medicine);
[1db5673]285 }
286 }
287 }
288
[db484c9]289 PharmacyHeadDto pharmacyHead = new PharmacyHeadDto()
[1db5673]290 {
291 Id = Phead.Id,
[db484c9]292 Medicines = MedicineList,
293 Pharmacies = Phead.Pharmacies,
[1db5673]294 Email = Phead.Email,
295 Name = Phead.Name,
296 Password = Phead.Password
297 };
[db484c9]298 return pharmacyHead;
[d23bf72]299 }
[1db5673]300 else throw new Exception("Username is null.");
[d23bf72]301 }
[1454207]302 }
303}
Note: See TracBrowser for help on using the repository browser.