source: FarmatikoServices/Services/PHService.cs@ ac51326

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

Fix create user

  • Property mode set to 100644
File size: 8.4 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
[1db5673]75
[db484c9]76 if (!pharmacyHead.Medicines.Equals(PHMedicines))
[1db5673]77 {
[db484c9]78 //phead.Medicines = pharmacyHead.Medicines;
79 if (pharmacyHead.Medicines.Count() == 0)
[68454c6]80 {
[db484c9]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();
[68454c6]85 _iPHRepo.DeletePHMedicine(PHMId, phId, medId);
86 return;
87 }
[db484c9]88 foreach (var med in pharmacyHead.Medicines)
[1db5673]89 {
[8e74e2f]90
[db484c9]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);
[8e74e2f]103
104 }
[1db5673]105
[db484c9]106 phead.Medicines = list;
107
[1db5673]108 await _iPHRepo.UpdatePharmacyHead(phead);
109 }
[db484c9]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))
[8e74e2f]119 {
[db484c9]120 await _iPHRepo.UpdatePharmacyHead(head);
[8e74e2f]121 }
[1db5673]122 else throw new Exception("Cannot update pharmacy head since there was no changes.");
123 }
[1454207]124 else throw new Exception("PharmacyHead has a null value.");
125 }
[db484c9]126 public async Task<bool> Add(PharmacyHeadDto pharmacyHead)
[6f203af]127 {
128 if (pharmacyHead != null)
129 {
[db484c9]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);
[6f203af]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 }
[d23bf72]165
[db484c9]166 public PharmacyHeadDto GetPharmacyHead(string userName)
[d23bf72]167 {
168 if (userName != null)
169 {
[1db5673]170 var Phead = _iPHRepo.GetPharmacyHeadByUserName(userName);
171 List<PharmacyHeadMedicine> PHMedicines = _iPHRepo.GetPharmacyHeadMedicines(userName);
172 List<Medicine> Medicines = _repository.GetMedicines().ToList();
[db484c9]173 List<Medicine> MedicineList = new List<Medicine>();
[1db5673]174
175 var user = _repository.GetRole(userName);
176
177
178 if (user.UserRole.ToString().Equals("Admin"))
179 {
[db484c9]180 List<Pharmacy> pharmacies = new List<Pharmacy>();
181 pharmacies = Phead.Pharmacies;
182 var Admin = new PharmacyHeadDto()
[1db5673]183 {
184 Id = user.Id,
185 Email = user.Email,
186 Name = user.Name,
[8e74e2f]187 Password = user.Password,
[db484c9]188 Pharmacies = pharmacies
[1db5673]189 };
190
191 return Admin;
192 }
[db484c9]193 else
[1db5673]194 {
195 foreach (var med in Medicines)
196 {
[7d80751]197 if (Phead.Medicines == null || Phead.Medicines.Count() == 0)
198 break;
[db484c9]199 var PHMObj = Phead.Medicines.Where(x => x.MedicineId == med.Id).SingleOrDefault();
[8e74e2f]200 if (PHMObj == null)
[1db5673]201 {
[8e74e2f]202 continue;
203 }
[1db5673]204 if (PHMObj.MedicineId == med.Id)
205 {
[8e74e2f]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 };
[db484c9]217 MedicineList.Add(medicine);
[1db5673]218 }
219 }
220 }
221
[db484c9]222 PharmacyHeadDto pharmacyHead = new PharmacyHeadDto()
[1db5673]223 {
224 Id = Phead.Id,
[db484c9]225 Medicines = MedicineList,
226 Pharmacies = Phead.Pharmacies,
[1db5673]227 Email = Phead.Email,
228 Name = Phead.Name,
229 Password = Phead.Password
230 };
[db484c9]231 return pharmacyHead;
[d23bf72]232 }
[1db5673]233 else throw new Exception("Username is null.");
[d23bf72]234 }
[1454207]235 }
236}
Note: See TracBrowser for help on using the repository browser.