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

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

Fix create new user bug.

  • Property mode set to 100644
File size: 8.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 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>();
73
74
75 if (pharmacyHead.Medicines != null || pharmacyHead.Medicines.Count() > 0)
76 if (!pharmacyHead.Medicines.Equals(PHMedicines))
77 {
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)
89 {
90
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;
107
108 await _iPHRepo.UpdatePharmacyHead(phead);
109
110 }
111
112 if (pharmacyHead.Pharmacies != null || pharmacyHead.Pharmacies.Count() > 0)
113 {
114 phead.Pharmacies = pharmacyHead.Pharmacies;
115 }
116 PharmacyHead head = new PharmacyHead()
117 {
118 Name = pharmacyHead.Name,
119 Email = pharmacyHead.Email,
120 Password = pharmacyHead.Password
121 };
122 if (!phead.Equals(head))
123 {
124 await _iPHRepo.UpdatePharmacyHead(head);
125 }
126 else throw new Exception("Cannot update pharmacy head since there was no changes.");
127 }
128 else throw new Exception("PharmacyHead has a null value.");
129 }
130 public async Task<bool> Add(PharmacyHeadDto pharmacyHead)
131 {
132 if (pharmacyHead != null)
133 {
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);
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 }
169
170 public PharmacyHeadDto GetPharmacyHead(string userName)
171 {
172 if (userName != null)
173 {
174 var Phead = _iPHRepo.GetPharmacyHeadByUserName(userName);
175 List<PharmacyHeadMedicine> PHMedicines = _iPHRepo.GetPharmacyHeadMedicines(userName);
176 List<Medicine> Medicines = _repository.GetMedicines().ToList();
177 List<Medicine> MedicineList = new List<Medicine>();
178
179 var user = _repository.GetRole(userName);
180
181
182 if (user.UserRole.ToString().Equals("Admin"))
183 {
184 List<Pharmacy> pharmacies = new List<Pharmacy>();
185 pharmacies = Phead.Pharmacies;
186 var Admin = new PharmacyHeadDto()
187 {
188 Id = user.Id,
189 Email = user.Email,
190 Name = user.Name,
191 Password = user.Password,
192 Pharmacies = pharmacies
193 };
194
195 return Admin;
196 }
197 else
198 {
199 foreach (var med in Medicines)
200 {
201 if (Phead.Medicines == null || Phead.Medicines.Count() == 0)
202 break;
203 var PHMObj = Phead.Medicines.Where(x => x.MedicineId == med.Id).SingleOrDefault();
204 if (PHMObj == null)
205 {
206 continue;
207 }
208 if (PHMObj.MedicineId == med.Id)
209 {
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 };
221 MedicineList.Add(medicine);
222 }
223 }
224 }
225
226 PharmacyHeadDto pharmacyHead = new PharmacyHeadDto()
227 {
228 Id = Phead.Id,
229 Medicines = MedicineList,
230 Pharmacies = Phead.Pharmacies,
231 Email = Phead.Email,
232 Name = Phead.Name,
233 Password = Phead.Password
234 };
235 return pharmacyHead;
236 }
237 else throw new Exception("Username is null.");
238 }
239 }
240}
Note: See TracBrowser for help on using the repository browser.