source: FarmatikoServices/Services/PHService.cs@ 1db5673

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

Fix bugs, add some more

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