source: FarmatikoServices/Services/PHService.cs@ 8e74e2f

Last change on this file since 8e74e2f was 8e74e2f, checked in by DimitarSlezenkovski <dslezenkovski@…>, 3 years ago

Fix bugs, add some more.

  • Property mode set to 100644
File size: 8.4 KB
Line 
1using FarmatikoData.FarmatikoRepoInterfaces;
2using FarmatikoData.Models;
3using FarmatikoServices.FarmatikoServiceInterfaces;
4using System;
5using System.Collections.Generic;
6using System.Linq;
7using System.Threading.Tasks;
8
9namespace FarmatikoServices.Services
10{
11 public class PHService : IPHService
12 {
13 private readonly IPHRepo _iPHRepo;
14 private readonly IRepository _repository;
15 public PHService(IPHRepo iPHRepo, IRepository repository)
16 {
17 _iPHRepo = iPHRepo;
18 _repository = repository;
19 }
20
21 public async Task<bool> ClaimPharmacy(RequestPharmacyHead pharmacy)
22 {
23 if (pharmacy != null)
24 {
25 await _iPHRepo.ClaimPharmacy(pharmacy);
26 return true;
27 }
28 return false;
29 }
30
31 public async Task<PharmacyHead> GetPharmacyHeadByIdAsync(int id)
32 {
33 PharmacyHead Phead = null;
34 if (id >= 0)
35 Phead = await _iPHRepo.GetPharmacyHeadByIdAsync(id);
36 if (Phead != null)
37 return Phead;
38 throw new Exception("No data found.");
39 }
40
41 public async Task<IEnumerable<PharmacyHead>> GetPharmacyHeadInfo()
42 {
43 var PHeads = await _iPHRepo.GetPharmacyHeadInfo();
44 if (PHeads != null)
45 return PHeads;
46 throw new Exception("No Pharmacy heads found.");
47 }
48
49 public async Task<int> Login(PharmacyHead pharmacyHead)
50 {
51 var PHead = await _iPHRepo.GetPharmacyHeadByIdAsync(pharmacyHead.Id);
52 if (PHead.Password.Equals(pharmacyHead.Password))
53 return PHead.Id;
54 return -1;
55 }
56
57
58
59 public async Task UpdatePharmacyHead(PharmacyHead pharmacyHead)
60 {
61 if (pharmacyHead != null)
62 {
63 var phead = _iPHRepo.GetPharmacyHead(pharmacyHead.Email);
64
65 /*if (pharmacyHead.PharmaciesList.Count() == 0)
66 pharmacyHead.PharmaciesList = null;*/
67
68
69 phead.PHMedicineList = _repository.GetPHMedicines(phead.Email);
70
71 if (phead.MedicineList != pharmacyHead.MedicineList)
72 {
73 phead.MedicineList = pharmacyHead.MedicineList;
74 List<PharmacyHeadMedicine> list = new List<PharmacyHeadMedicine>();
75 if (pharmacyHead.MedicineList.Count() == 0)
76 {
77 phead.MedicineList = null;
78 int PHMId = phead.PHMedicineList.Select(x => x.Id).Single();
79 int phId = phead.PHMedicineList.Select(x => x.PheadId).Single();
80 int medId = phead.PHMedicineList.Select(x => x.MedicineId).Single();
81 _iPHRepo.DeletePHMedicine(PHMId, phId, medId);
82 return;
83 }
84 List<PharmacyHeadMedicine> PHMList = new List<PharmacyHeadMedicine>();
85 if (phead.PHMedicineList == pharmacyHead.PHMedicineList)
86 {
87 foreach (var med in phead.MedicineList)
88 {
89 //list = phead.PHMedicineList.ToList();
90
91 var PHMObj = phead.PHMedicineList.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.MedicineId == med.Id).Single();
99 if (PHMObj == null || PHMObj == default)
100 break;
101 if (PHMObj.MedicineId == med.Id)
102 list.Add(PHMObj);
103
104 }
105
106 phead.PHMedicineList = list;
107 }
108
109 await _iPHRepo.UpdatePharmacyHead(phead);
110 }
111 else if (!phead.Equals(pharmacyHead))
112 {
113 await _iPHRepo.UpdatePharmacyHead(pharmacyHead);
114 }
115 else throw new Exception("Cannot update pharmacy head since there was no changes.");
116 }
117 else throw new Exception("PharmacyHead has a null value.");
118 }
119 public async Task<bool> Add(PharmacyHead pharmacyHead)
120 {
121 if (pharmacyHead != null)
122 {
123 await _iPHRepo.Add(pharmacyHead);
124 return true;
125 }
126 return false;
127 }
128
129 public async Task<bool> Remove(int id)
130 {
131 PharmacyHead Phead = await _iPHRepo.GetPharmacyHeadByIdAsync(id);
132 if (Phead != null && id >= 0)
133 {
134 Phead.DeletedOn = DateTime.UtcNow;
135 await _iPHRepo.Remove(Phead);
136 return true;
137 }
138 return false;
139 }
140
141 public async Task<bool> RemoveClaimingRequest(int id)
142 {
143 if (id >= 0)
144 {
145 await _iPHRepo.RemoveClaimingRequest(id);
146 return true;
147 }
148 return false;
149 }
150
151 public object GetPharmacyHead(string userName)
152 {
153 if (userName != null)
154 {
155 var Phead = _iPHRepo.GetPharmacyHeadByUserName(userName);
156 List<PharmacyHeadMedicine> PHMedicines = _iPHRepo.GetPharmacyHeadMedicines(userName);
157 List<Medicine> Medicines = _repository.GetMedicines().ToList();
158 List<Medicine> PHMedicineList = new List<Medicine>();
159
160
161 //var meds = PHMedicines.Where(x => x.Id == Phead.Id).ToList();
162 var pharmacies = _iPHRepo.GetPharmacies();
163 var PheadPharms = pharmacies.Where(x => x.PheadId == Phead.Id).ToList();
164 var user = _repository.GetRole(userName);
165
166
167 if (user.UserRole.ToString().Equals("Admin"))
168 {
169 var Admin = new PharmacyHead()
170 {
171 Id = user.Id,
172 Email = user.Email,
173 Name = user.Name,
174 Password = user.Password,
175 PharmaciesList = Phead.PharmaciesList
176 };
177
178 return Admin;
179 }
180
181 if (PheadPharms.Count() > 0 || PheadPharms != null)
182 Phead.PharmaciesList = PheadPharms;
183 else Phead.PharmaciesList = pharmacies;
184
185 if (Phead.PHMedicineList.Count() > 0 || Phead.PHMedicineList != null)
186 {
187 foreach (var med in Medicines)
188 {
189
190 var PHMObj = Phead.PHMedicineList.Where(x => x.MedicineId == med.Id).SingleOrDefault();
191 if (PHMObj == null)
192 {
193 continue;
194 }
195 var phm = Phead.MedicineList;
196 if (PHMObj.MedicineId == med.Id)
197 {
198 var medicine = new Medicine()
199 {
200 Id = med.Id,
201 Name = med.Name,
202 Strength = med.Strength,
203 Form = med.Form,
204 WayOfIssuing = med.WayOfIssuing,
205 Manufacturer = med.Manufacturer,
206 Price = med.Price,
207 Packaging = med.Packaging
208 };
209 PHMedicineList.Add(medicine);
210 }
211 }
212 Phead.MedicineList = PHMedicineList;
213 }
214 else
215 {
216 Phead.MedicineList = Medicines;
217 }
218
219 PharmacyHead pharHead = new PharmacyHead()
220 {
221 Id = Phead.Id,
222 MedicineList = Phead.MedicineList,
223 PharmaciesList = Phead.PharmaciesList,
224 Email = Phead.Email,
225 Name = Phead.Name,
226 Password = Phead.Password
227 };
228 return pharHead;
229 }
230 else throw new Exception("Username is null.");
231 }
232 }
233}
Note: See TracBrowser for help on using the repository browser.