source: FarmatikoServices/Services/PHService.cs@ 0a694bb

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

Fix create new user bug.

  • Property mode set to 100644
File size: 8.7 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();
67
68 List<Medicine> medicines = _repository.GetMedicines().ToList();
69
70 List<Medicine> PHMedicines = medicines.Where(x => x.Id == phead.Medicines.Select(x => x.MedicineId).Single()).ToList();
71
72 List<PharmacyHeadMedicine> list = new List<PharmacyHeadMedicine>();
[68454c6]73
[db484c9]74
[0a694bb]75 if (pharmacyHead.Medicines != null || pharmacyHead.Medicines.Count() > 0)
76 if (!pharmacyHead.Medicines.Equals(PHMedicines))
[1db5673]77 {
[0a694bb]78 //phead.Medicines = pharmacyHead.Medicines;
79 if (pharmacyHead.Medicines.Count() == 0)
80 {
81 phead.Medicines = null;
82 int PHMId = phead.Medicines.Select(x => x.Id).Single();
83 int phId = phead.Medicines.Select(x => x.PheadId).Single();
84 int medId = phead.Medicines.Select(x => x.MedicineId).Single();
85 _iPHRepo.DeletePHMedicine(PHMId, phId, medId);
86 return;
87 }
88 foreach (var med in pharmacyHead.Medicines)
[db484c9]89 {
[8e74e2f]90
[0a694bb]91 PharmacyHeadMedicine PHMObj = phead.Medicines.Select(x => new PharmacyHeadMedicine
92 {
93 Id = x.Id,
94 PheadId = x.PheadId,
95 Head = x.Head,
96 MedicineId = x.MedicineId,
97 Medicine = x.Medicine
98 }).Where(x => !x.Medicine.Equals(med)).Single();
99 if (PHMObj == null || PHMObj == default)
100 break;
101 if (PHMObj.MedicineId == med.Id)
102 list.Add(PHMObj);
103
104 }
105
106 phead.Medicines = list;
[1db5673]107
[0a694bb]108 await _iPHRepo.UpdatePharmacyHead(phead);
[db484c9]109
[0a694bb]110 }
111
112 if (pharmacyHead.Pharmacies != null || pharmacyHead.Pharmacies.Count() > 0)
113 {
114 phead.Pharmacies = pharmacyHead.Pharmacies;
[1db5673]115 }
[db484c9]116 PharmacyHead head = new PharmacyHead()
117 {
118 Name = pharmacyHead.Name,
119 Email = pharmacyHead.Email,
[0a694bb]120 Password = pharmacyHead.Password
[db484c9]121 };
122 if (!phead.Equals(head))
[8e74e2f]123 {
[db484c9]124 await _iPHRepo.UpdatePharmacyHead(head);
[8e74e2f]125 }
[1db5673]126 else throw new Exception("Cannot update pharmacy head since there was no changes.");
127 }
[1454207]128 else throw new Exception("PharmacyHead has a null value.");
129 }
[db484c9]130 public async Task<bool> Add(PharmacyHeadDto pharmacyHead)
[6f203af]131 {
132 if (pharmacyHead != null)
133 {
[db484c9]134 PharmacyHead head = new PharmacyHead()
135 {
136 Name = pharmacyHead.Name,
137 Email = pharmacyHead.Email,
138 Password = pharmacyHead.Password,
139 Pharmacies = null,
140 Medicines = null
141 };
142 await _iPHRepo.Add(head);
[6f203af]143 return true;
144 }
145 return false;
146 }
147
148 public async Task<bool> Remove(int id)
149 {
150 PharmacyHead Phead = await _iPHRepo.GetPharmacyHeadByIdAsync(id);
151 if (Phead != null && id >= 0)
152 {
153 Phead.DeletedOn = DateTime.UtcNow;
154 await _iPHRepo.Remove(Phead);
155 return true;
156 }
157 return false;
158 }
159
160 public async Task<bool> RemoveClaimingRequest(int id)
161 {
162 if (id >= 0)
163 {
164 await _iPHRepo.RemoveClaimingRequest(id);
165 return true;
166 }
167 return false;
168 }
[d23bf72]169
[db484c9]170 public PharmacyHeadDto GetPharmacyHead(string userName)
[d23bf72]171 {
172 if (userName != null)
173 {
[1db5673]174 var Phead = _iPHRepo.GetPharmacyHeadByUserName(userName);
175 List<PharmacyHeadMedicine> PHMedicines = _iPHRepo.GetPharmacyHeadMedicines(userName);
176 List<Medicine> Medicines = _repository.GetMedicines().ToList();
[db484c9]177 List<Medicine> MedicineList = new List<Medicine>();
[1db5673]178
179 var user = _repository.GetRole(userName);
180
181
182 if (user.UserRole.ToString().Equals("Admin"))
183 {
[db484c9]184 List<Pharmacy> pharmacies = new List<Pharmacy>();
185 pharmacies = Phead.Pharmacies;
186 var Admin = new PharmacyHeadDto()
[1db5673]187 {
188 Id = user.Id,
189 Email = user.Email,
190 Name = user.Name,
[8e74e2f]191 Password = user.Password,
[db484c9]192 Pharmacies = pharmacies
[1db5673]193 };
194
195 return Admin;
196 }
[db484c9]197 else
[1db5673]198 {
199 foreach (var med in Medicines)
200 {
[7d80751]201 if (Phead.Medicines == null || Phead.Medicines.Count() == 0)
202 break;
[db484c9]203 var PHMObj = Phead.Medicines.Where(x => x.MedicineId == med.Id).SingleOrDefault();
[8e74e2f]204 if (PHMObj == null)
[1db5673]205 {
[8e74e2f]206 continue;
207 }
[1db5673]208 if (PHMObj.MedicineId == med.Id)
209 {
[8e74e2f]210 var medicine = new Medicine()
211 {
212 Id = med.Id,
213 Name = med.Name,
214 Strength = med.Strength,
215 Form = med.Form,
216 WayOfIssuing = med.WayOfIssuing,
217 Manufacturer = med.Manufacturer,
218 Price = med.Price,
219 Packaging = med.Packaging
220 };
[db484c9]221 MedicineList.Add(medicine);
[1db5673]222 }
223 }
224 }
225
[db484c9]226 PharmacyHeadDto pharmacyHead = new PharmacyHeadDto()
[1db5673]227 {
228 Id = Phead.Id,
[db484c9]229 Medicines = MedicineList,
230 Pharmacies = Phead.Pharmacies,
[1db5673]231 Email = Phead.Email,
232 Name = Phead.Name,
233 Password = Phead.Password
234 };
[db484c9]235 return pharmacyHead;
[d23bf72]236 }
[1db5673]237 else throw new Exception("Username is null.");
[d23bf72]238 }
[1454207]239 }
240}
Note: See TracBrowser for help on using the repository browser.