source: FarmatikoServices/Services/Service.cs@ 0a694bb

Last change on this file since 0a694bb was 0a694bb, checked in by Dimitar Slezenkovski <dslezenkovski@…>, 3 years ago

Fix create new user bug.

  • Property mode set to 100644
File size: 9.8 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 Service : IService
13 {
14 private readonly IRepository _repository;
15 public Service(IRepository repository)
16 {
17 _repository = repository;
18 }
19
20 //GET
21 public async Task<IEnumerable<HealthcareWorker>> GetAllWorkers()
22 {
23 var Workers = await _repository.GetAllWorkers();
24 return Workers;
25 }
26
27 public async Task<IEnumerable<HealthFacility>> GetFacilities()
28 {
29 var Facilities = await _repository.GetFacilities();
30 return Facilities;
31 }
32
33 public async Task<HealthFacility> GetFacility(int id)
34 {
35 var Facility = await _repository.GetFacility(id);
36 return Facility;
37 }
38
39 public async Task<Medicine> GetMedicine(int id)
40 {
41 var Medicine = await _repository.GetMedicine(id);
42 return Medicine;
43 }
44
45 public async Task<List<MedicineDTO>> GetMedicines()
46 {
47 var Medicines = await _repository.GetMedicinesAsync();
48 List<MedicineDTO> list = new List<MedicineDTO>();
49 var listPHMedicines = await _repository.GetAllPHMedicines();
50 List<string> headNames = new List<string>();
51 List<PharmacyHead> heads = new List<PharmacyHead>();
52 foreach (var med in Medicines)
53 {
54 var meds = listPHMedicines.Where(x => x.MedicineId == med.Id).ToList();
55 if (meds != null)
56 {
57 heads = meds.Select(x => x.Head).ToList();
58 }
59 headNames = heads.Select(x => x.Name).ToList();
60 MedicineDTO medicine = new MedicineDTO()
61 {
62 Name = med.Name,
63 Manufacturer = med.Manufacturer,
64 Packaging = med.Packaging,
65 Form = med.Form,
66 Price = med.Price,
67 Strength = med.Strength,
68 WayOfIssuing = med.WayOfIssuing,
69 HeadNames = headNames
70 };
71
72 list.Add(medicine);
73 }
74
75 return list;
76 }
77
78 public async Task<Pandemic> GetPandemic()
79 {
80 var Pandemic = await _repository.GetPandemic();
81 return Pandemic;
82 }
83
84 public async Task<List<PharmacyDTO>> GetPharmacies()
85 {
86 var Pharmacies = await _repository.GetPharmacies();
87 List<PharmacyDTO> pharmacies = new List<PharmacyDTO>();
88
89 foreach(var pharm in Pharmacies)
90 {
91 PharmacyDTO pharmacyDTO = new PharmacyDTO()
92 {
93 Name = pharm.Name,
94 Location = pharm.Location,
95 Address = pharm.Address,
96 WorkAllTime = pharm.WorkAllTime
97 };
98 if (pharm.PharmacyHead != null)
99 {
100 pharmacyDTO.HeadName = pharm.PharmacyHead.Name;
101 }
102
103 pharmacies.Add(pharmacyDTO);
104 }
105 return pharmacies;
106 }
107
108 public async Task<Pharmacy> GetPharmacy(int id)
109 {
110 var Pharmacy = await _repository.GetPharmacy(id);
111 return Pharmacy;
112 }
113
114 public async Task<HealthcareWorker> GetWorker(int id)
115 {
116 var Worker = await _repository.GetWorker(id);
117 return Worker;
118 }
119
120 public async Task<IEnumerable<HealthFacility>> SearchFacilities(string query)
121 {
122 var SearchQuery = await _repository.SearchFacilities(query);
123 return SearchQuery;
124 }
125
126 public async Task<IEnumerable<Medicine>> SearchMedicines(string query)
127 {
128 var SearchQuery = await _repository.SearchMedicines(query);
129 return SearchQuery;
130 }
131
132 public async Task<IEnumerable<Pharmacy>> SearchPharmacies(string query)
133 {
134 var SearchQuery = await _repository.SearchPharmacies(query);
135 return SearchQuery;
136 }
137
138 public async Task<IEnumerable<HealthcareWorker>> SearchWorkers(string query)
139 {
140 var SearchQuery = await _repository.SearchWorkers(query);
141 return SearchQuery;
142 }
143
144
145 //POST (ADD NEW OBJECTS)
146 //za json(Sys updateer)
147 public async Task AddFacility(HealthFacility healthFacilities)
148 {
149 if (healthFacilities != null)
150 await _repository.AddFacility(healthFacilities);
151 else throw new Exception("Facility is null");
152 }
153 //za json(Sys updateer)
154 public async Task AddMedicines(Medicine medicine)
155 {
156 if (medicine != null)
157 await _repository.AddMedicines(medicine);
158 else throw new Exception("Medicine is null");
159 }
160 //za json(Sys updateer)
161 public async Task AddPandemic(Pandemic pandemic)
162 {
163 if (pandemic != null)
164 await _repository.AddPandemic(pandemic);
165 else throw new Exception("Pandemic is null");
166 }
167 // Samo PharmacyHead i Admin imaat pristap
168 public async Task AddPharmacy(Pharmacy pharmacy)
169 {
170 if (pharmacy != null)
171 await _repository.AddPharmacy(pharmacy);
172 else throw new Exception("Pharmacy is null");
173 }
174
175 // Ovaa kontrola ja ima samo admin
176 public async Task<User> MakeUser(PharmacyHead head)
177 {
178 User user = new User()
179 {
180 Name = head.Name,
181 Password = head.Password,
182 Email = head.Email,
183 UserRole = User.Role.PharmacyHead
184 };
185 bool Success = await _repository.AddUser(user);
186 if (!Success)
187 return null;
188 return user;
189 }
190 public async Task<bool> AddPharmacyHead(PharmacyHeadDto pharmacyHead)
191 {
192 if (pharmacyHead != null)
193 {
194 var phead = new PharmacyHead()
195 {
196 Name = pharmacyHead.Name,
197 Email = pharmacyHead.Email,
198 Password = pharmacyHead.Password
199 };
200 var user = await MakeUser(phead);
201 if (user is null)
202 {
203 return false;
204 }
205 var users = _repository.GetUsers();
206 var thisUser = users.Where(usr => usr.Value.Email.Equals(user.Email)).Select(x => x.Value).FirstOrDefault();
207 User user1 = new User()
208 {
209 Name = thisUser.Name,
210 Password = thisUser.Password,
211 Email = thisUser.Email,
212 UserRole = thisUser.UserRole
213 };
214 phead.User = user1;
215 await _repository.AddPharmacyHead(phead);
216 return true;
217 }
218 else throw new Exception("PharmacyHeadDto is null");
219 }
220 //za json(Sys updater)
221 public async Task AddWorker(HealthcareWorker worker)
222 {
223 if (worker != null)
224 await _repository.AddWorker(worker);
225 else throw new Exception("Worker is null");
226 }
227
228 //za json(Sys updateer)
229 public async Task UpdateFacility(HealthFacility healthFacilities)
230 {
231 if (healthFacilities != null)
232 await _repository.UpdateFacility(healthFacilities);
233 else throw new Exception("Facility is null");
234 }
235 //PharmacyHead
236 public async Task RemoveMedicine(Medicine medicine)
237 {
238 if (medicine != null)
239 await _repository.RemoveMedicine(medicine);
240 else throw new Exception("Medicine is null");
241 }
242 //PharmacyHead
243 public async Task UpdateMedicine(Medicine medicine)
244 {
245 if (medicine != null)
246 await _repository.UpdateMedicine(medicine);
247 else throw new Exception("Medicine is null");
248 }
249 //za json(Sys updateer)
250 public async Task UpdatePandemic(Pandemic pandemic)
251 {
252 if (pandemic != null)
253 await _repository.UpdatePandemic(pandemic);
254 else throw new Exception("Pandemic is null");
255 }
256 //PharmacyHead
257 public async Task RemovePharmacy(Pharmacy pharmacy)
258 {
259 if (pharmacy != null)
260 await _repository.RemovePharmacy(pharmacy);
261 else throw new Exception("Pharmacy is null");
262 }
263 //PharamcyHead
264 public async Task UpdatePharmacy(Pharmacy pharmacy)
265 {
266 if (pharmacy != null)
267 await _repository.UpadatePharmacy(pharmacy);
268 else throw new Exception("Pharmacy is null");
269 }
270 //za json(Sys updateer)
271 public async Task UpdateWorker(HealthcareWorker worker)
272 {
273 if (worker != null)
274 await _repository.UpdateWorker(worker);
275 else throw new Exception("Worker is null");
276 }
277
278 public async Task RemovePharmacyHead(int Id)
279 {
280 if (Id > 0)
281 {
282 await _repository.RemovePharmacyHead(Id);
283 }
284 else throw new Exception("Index out of bounds.");
285 }
286
287 public HealthFacility GetFacilityJSON(string healthFacility)
288 {
289 if (healthFacility != null)
290 return _repository.GetFacilityJSON(healthFacility);
291 return null;
292 }
293
294 //PUT (EDIT OBJECTS)
295
296
297 //DELETE
298
299 }
300}
Note: See TracBrowser for help on using the repository browser.