source: FarmatikoServices/Services/PHService.cs@ 68454c6

Last change on this file since 68454c6 was 68454c6, checked in by DimitarSlezenkovski <dslezenkovski@…>, 2 years ago

fix bugs

  • Property mode set to 100644
File size: 7.6 KB
Line 
1using FarmatikoData.FarmatikoRepoInterfaces;
2using FarmatikoData.Models;
3using FarmatikoServices.FarmatikoServiceInterfaces;
4using System;
5using System.Collections.Generic;
6using System.Linq;
7using System.Text;
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(PharmacyHead pharmacyHead)
61 {
62 if (pharmacyHead != null)
63 {
64 var phead = _iPHRepo.GetPharmacyHead(pharmacyHead.Email);
65
66 if (pharmacyHead.PharmaciesList.Count() == 0)
67 pharmacyHead.PharmaciesList = null;
68
69
70 phead.PHMedicineList = _repository.GetPHMedicines(phead.Email);
71
72 if (phead.MedicineList != pharmacyHead.MedicineList || phead != pharmacyHead)
73 {
74 phead.MedicineList = pharmacyHead.MedicineList;
75 List<PharmacyHeadMedicine> list = new List<PharmacyHeadMedicine>();
76 if (pharmacyHead.MedicineList.Count() == 0)
77 {
78 phead.MedicineList = null;
79 int PHMId = phead.PHMedicineList.Select(x => x.Id).Single();
80 int phId = phead.PHMedicineList.Select(x => x.PheadId).Single();
81 int medId = phead.PHMedicineList.Select(x => x.MedicineId).Single();
82 _iPHRepo.DeletePHMedicine(PHMId, phId, medId);
83 return;
84 }
85 foreach (var med in phead.MedicineList)
86 {
87 med.Id++;
88 var PHMObj = phead.PHMedicineList.Select(x => new PharmacyHeadMedicine
89 {
90 PheadId = x.PheadId,
91 Head = x.Head,
92 MedicineId = x.MedicineId,
93 Medicine = x.Medicine
94 }).Where(x => x.MedicineId == med.Id).Single();
95 list.Add(PHMObj);
96 }
97
98 phead.PHMedicineList = list;
99
100 await _iPHRepo.UpdatePharmacyHead(phead);
101 }
102 else throw new Exception("Cannot update pharmacy head since there was no changes.");
103 }
104 else throw new Exception("PharmacyHead has a null value.");
105 }
106 public async Task<bool> Add(PharmacyHead pharmacyHead)
107 {
108 if (pharmacyHead != null)
109 {
110 await _iPHRepo.Add(pharmacyHead);
111 return true;
112 }
113 return false;
114 }
115
116 public async Task<bool> Remove(int id)
117 {
118 PharmacyHead Phead = await _iPHRepo.GetPharmacyHeadByIdAsync(id);
119 if (Phead != null && id >= 0)
120 {
121 Phead.DeletedOn = DateTime.UtcNow;
122 await _iPHRepo.Remove(Phead);
123 return true;
124 }
125 return false;
126 }
127
128 public async Task<bool> RemoveClaimingRequest(int id)
129 {
130 if (id >= 0)
131 {
132 await _iPHRepo.RemoveClaimingRequest(id);
133 return true;
134 }
135 return false;
136 }
137
138 public object GetPharmacyHead(string userName)
139 {
140 if (userName != null)
141 {
142 var Phead = _iPHRepo.GetPharmacyHeadByUserName(userName);
143 List<PharmacyHeadMedicine> PHMedicines = _iPHRepo.GetPharmacyHeadMedicines(userName);
144 List<Medicine> Medicines = _repository.GetMedicines().ToList();
145 List<Medicine> PHMedicineList = new List<Medicine>();
146
147
148 //var meds = PHMedicines.Where(x => x.Id == Phead.Id).ToList();
149 var pharmacies = _iPHRepo.GetPharmacies();
150 var PheadPharms = pharmacies.Where(x => x.PheadId == Phead.Id).ToList();
151 var user = _repository.GetRole(userName);
152
153
154 if (user.UserRole.ToString().Equals("Admin"))
155 {
156 var Admin = new
157 {
158 Id = user.Id,
159 Email = user.Email,
160 Name = user.Name,
161 Passwd = user.Password,
162 MedicineList = PHMedicines,
163 PharmaciesList = Phead.PharmaciesList
164 };
165
166 return Admin;
167 }
168
169 if (PheadPharms.Count() > 0)
170 Phead.MedicineList = PHMedicineList;
171 else Phead.PharmaciesList = pharmacies;
172
173 if (Phead.PHMedicineList.Count() > 0)
174 {
175 foreach (var med in Medicines)
176 {
177 var PHMObj = Phead.PHMedicineList.Select(x => new PharmacyHeadMedicine
178 {
179 PheadId = x.PheadId,
180 Head = x.Head,
181 MedicineId = x.MedicineId,
182 Medicine = x.Medicine
183 }).Where(x => x.MedicineId == med.Id).SingleOrDefault();
184 if (PHMObj == null || PHMObj == default)
185 break;
186 if (PHMObj.MedicineId == med.Id)
187 {
188 Medicine medicine = new Medicine(med.Name, med.Strength, med.Form, med.WayOfIssuing, med.Manufacturer, med.Price, med.Packaging);
189 PHMedicineList.Add(medicine);
190 }
191 }
192 Phead.MedicineList = PHMedicineList;
193 }
194 else
195 {
196 Phead.MedicineList = Medicines;
197 }
198
199 PharmacyHead pharHead = new PharmacyHead()
200 {
201 Id = Phead.Id,
202 MedicineList = Phead.MedicineList,
203 PharmaciesList = Phead.PharmaciesList,
204 Email = Phead.Email,
205 Name = Phead.Name,
206 Password = Phead.Password
207 };
208 return pharHead;
209 }
210 else throw new Exception("Username is null.");
211 }
212 }
213}
Note: See TracBrowser for help on using the repository browser.