source: FarmatikoServices/Services/PHService.cs@ 7d80751

Last change on this file since 7d80751 was 7d80751, checked in by DimitarSlezenkovski <dslezenkovski@…>, 20 months ago

Fix create user

  • Property mode set to 100644
File size: 8.4 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
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 PharmacyHead head = new PharmacyHead()
111 {
112 Name = pharmacyHead.Name,
113 Email = pharmacyHead.Email,
114 Password = pharmacyHead.Password,
115 Pharmacies = pharmacyHead.Pharmacies,
116 Medicines = list
117 };
118 if (!phead.Equals(head))
119 {
120 await _iPHRepo.UpdatePharmacyHead(head);
121 }
122 else throw new Exception("Cannot update pharmacy head since there was no changes.");
123 }
124 else throw new Exception("PharmacyHead has a null value.");
125 }
126 public async Task<bool> Add(PharmacyHeadDto pharmacyHead)
127 {
128 if (pharmacyHead != null)
129 {
130 PharmacyHead head = new PharmacyHead()
131 {
132 Name = pharmacyHead.Name,
133 Email = pharmacyHead.Email,
134 Password = pharmacyHead.Password,
135 Pharmacies = null,
136 Medicines = null
137 };
138 await _iPHRepo.Add(head);
139 return true;
140 }
141 return false;
142 }
143
144 public async Task<bool> Remove(int id)
145 {
146 PharmacyHead Phead = await _iPHRepo.GetPharmacyHeadByIdAsync(id);
147 if (Phead != null && id >= 0)
148 {
149 Phead.DeletedOn = DateTime.UtcNow;
150 await _iPHRepo.Remove(Phead);
151 return true;
152 }
153 return false;
154 }
155
156 public async Task<bool> RemoveClaimingRequest(int id)
157 {
158 if (id >= 0)
159 {
160 await _iPHRepo.RemoveClaimingRequest(id);
161 return true;
162 }
163 return false;
164 }
165
166 public PharmacyHeadDto GetPharmacyHead(string userName)
167 {
168 if (userName != null)
169 {
170 var Phead = _iPHRepo.GetPharmacyHeadByUserName(userName);
171 List<PharmacyHeadMedicine> PHMedicines = _iPHRepo.GetPharmacyHeadMedicines(userName);
172 List<Medicine> Medicines = _repository.GetMedicines().ToList();
173 List<Medicine> MedicineList = new List<Medicine>();
174
175 var user = _repository.GetRole(userName);
176
177
178 if (user.UserRole.ToString().Equals("Admin"))
179 {
180 List<Pharmacy> pharmacies = new List<Pharmacy>();
181 pharmacies = Phead.Pharmacies;
182 var Admin = new PharmacyHeadDto()
183 {
184 Id = user.Id,
185 Email = user.Email,
186 Name = user.Name,
187 Password = user.Password,
188 Pharmacies = pharmacies
189 };
190
191 return Admin;
192 }
193 else
194 {
195 foreach (var med in Medicines)
196 {
197 if (Phead.Medicines == null || Phead.Medicines.Count() == 0)
198 break;
199 var PHMObj = Phead.Medicines.Where(x => x.MedicineId == med.Id).SingleOrDefault();
200 if (PHMObj == null)
201 {
202 continue;
203 }
204 if (PHMObj.MedicineId == med.Id)
205 {
206 var medicine = new Medicine()
207 {
208 Id = med.Id,
209 Name = med.Name,
210 Strength = med.Strength,
211 Form = med.Form,
212 WayOfIssuing = med.WayOfIssuing,
213 Manufacturer = med.Manufacturer,
214 Price = med.Price,
215 Packaging = med.Packaging
216 };
217 MedicineList.Add(medicine);
218 }
219 }
220 }
221
222 PharmacyHeadDto pharmacyHead = new PharmacyHeadDto()
223 {
224 Id = Phead.Id,
225 Medicines = MedicineList,
226 Pharmacies = Phead.Pharmacies,
227 Email = Phead.Email,
228 Name = Phead.Name,
229 Password = Phead.Password
230 };
231 return pharmacyHead;
232 }
233 else throw new Exception("Username is null.");
234 }
235 }
236}
Note: See TracBrowser for help on using the repository browser.