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
RevLine 
[afefe75]1using FarmatikoData.DTOs;
2using FarmatikoData.FarmatikoRepoInterfaces;
[5d02859]3using FarmatikoData.Models;
4using FarmatikoServices.FarmatikoServiceInterfaces;
5using System;
[6f203af]6using System.Collections.Generic;
[5d02859]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
[6f203af]21 public async Task<IEnumerable<HealthcareWorker>> GetAllWorkers()
[5d02859]22 {
23 var Workers = await _repository.GetAllWorkers();
24 return Workers;
25 }
26
[6f203af]27 public async Task<IEnumerable<HealthFacility>> GetFacilities()
[5d02859]28 {
29 var Facilities = await _repository.GetFacilities();
30 return Facilities;
31 }
32
[1454207]33 public async Task<HealthFacility> GetFacility(int id)
[5d02859]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
[afefe75]45 public async Task<List<MedicineDTO>> GetMedicines()
[5d02859]46 {
[1db5673]47 var Medicines = await _repository.GetMedicinesAsync();
[afefe75]48 List<MedicineDTO> list = new List<MedicineDTO>();
49 var listPHMedicines = await _repository.GetAllPHMedicines();
[0a694bb]50 List<string> headNames = new List<string>();
51 List<PharmacyHead> heads = new List<PharmacyHead>();
52 foreach (var med in Medicines)
[afefe75]53 {
[0a694bb]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 }
[afefe75]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;
[5d02859]76 }
77
78 public async Task<Pandemic> GetPandemic()
79 {
80 var Pandemic = await _repository.GetPandemic();
81 return Pandemic;
82 }
83
[afefe75]84 public async Task<List<PharmacyDTO>> GetPharmacies()
[5d02859]85 {
86 var Pharmacies = await _repository.GetPharmacies();
[afefe75]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,
[0a694bb]96 WorkAllTime = pharm.WorkAllTime
[afefe75]97 };
[0a694bb]98 if (pharm.PharmacyHead != null)
99 {
100 pharmacyDTO.HeadName = pharm.PharmacyHead.Name;
101 }
102
[afefe75]103 pharmacies.Add(pharmacyDTO);
104 }
105 return pharmacies;
[5d02859]106 }
107
108 public async Task<Pharmacy> GetPharmacy(int id)
109 {
110 var Pharmacy = await _repository.GetPharmacy(id);
111 return Pharmacy;
112 }
113
[1454207]114 public async Task<HealthcareWorker> GetWorker(int id)
[5d02859]115 {
116 var Worker = await _repository.GetWorker(id);
117 return Worker;
118 }
119
[6f203af]120 public async Task<IEnumerable<HealthFacility>> SearchFacilities(string query)
[5d02859]121 {
122 var SearchQuery = await _repository.SearchFacilities(query);
123 return SearchQuery;
124 }
125
[6f203af]126 public async Task<IEnumerable<Medicine>> SearchMedicines(string query)
[5d02859]127 {
128 var SearchQuery = await _repository.SearchMedicines(query);
129 return SearchQuery;
130 }
131
[6f203af]132 public async Task<IEnumerable<Pharmacy>> SearchPharmacies(string query)
[5d02859]133 {
134 var SearchQuery = await _repository.SearchPharmacies(query);
135 return SearchQuery;
136 }
137
[6f203af]138 public async Task<IEnumerable<HealthcareWorker>> SearchWorkers(string query)
[5d02859]139 {
140 var SearchQuery = await _repository.SearchWorkers(query);
141 return SearchQuery;
142 }
143
144
145 //POST (ADD NEW OBJECTS)
[1454207]146 //za json(Sys updateer)
147 public async Task AddFacility(HealthFacility healthFacilities)
[5d02859]148 {
149 if (healthFacilities != null)
[1454207]150 await _repository.AddFacility(healthFacilities);
[d23bf72]151 else throw new Exception("Facility is null");
[5d02859]152 }
[1454207]153 //za json(Sys updateer)
154 public async Task AddMedicines(Medicine medicine)
[5d02859]155 {
156 if (medicine != null)
[1454207]157 await _repository.AddMedicines(medicine);
[d23bf72]158 else throw new Exception("Medicine is null");
[5d02859]159 }
[1454207]160 //za json(Sys updateer)
161 public async Task AddPandemic(Pandemic pandemic)
[5d02859]162 {
163 if (pandemic != null)
[1454207]164 await _repository.AddPandemic(pandemic);
[d23bf72]165 else throw new Exception("Pandemic is null");
[5d02859]166 }
[1454207]167 // Samo PharmacyHead i Admin imaat pristap
168 public async Task AddPharmacy(Pharmacy pharmacy)
[5d02859]169 {
170 if (pharmacy != null)
[1454207]171 await _repository.AddPharmacy(pharmacy);
[d23bf72]172 else throw new Exception("Pharmacy is null");
[5d02859]173 }
[1db5673]174
[1454207]175 // Ovaa kontrola ja ima samo admin
[0a694bb]176 public async Task<User> MakeUser(PharmacyHead head)
[8e74e2f]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 };
[0a694bb]185 bool Success = await _repository.AddUser(user);
186 if (!Success)
187 return null;
[8e74e2f]188 return user;
189 }
[0a694bb]190 public async Task<bool> AddPharmacyHead(PharmacyHeadDto pharmacyHead)
[5d02859]191 {
192 if (pharmacyHead != null)
[1454207]193 {
[0a694bb]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;
[1454207]217 }
[0a694bb]218 else throw new Exception("PharmacyHeadDto is null");
[5d02859]219 }
[afc9a9a]220 //za json(Sys updater)
[1454207]221 public async Task AddWorker(HealthcareWorker worker)
222 {
223 if (worker != null)
224 await _repository.AddWorker(worker);
[d23bf72]225 else throw new Exception("Worker is null");
[1454207]226 }
[5d02859]227
[1454207]228 //za json(Sys updateer)
229 public async Task UpdateFacility(HealthFacility healthFacilities)
230 {
231 if (healthFacilities != null)
232 await _repository.UpdateFacility(healthFacilities);
[d23bf72]233 else throw new Exception("Facility is null");
[1454207]234 }
235 //PharmacyHead
236 public async Task RemoveMedicine(Medicine medicine)
237 {
238 if (medicine != null)
239 await _repository.RemoveMedicine(medicine);
[d23bf72]240 else throw new Exception("Medicine is null");
[1454207]241 }
242 //PharmacyHead
243 public async Task UpdateMedicine(Medicine medicine)
244 {
245 if (medicine != null)
246 await _repository.UpdateMedicine(medicine);
[d23bf72]247 else throw new Exception("Medicine is null");
[1454207]248 }
249 //za json(Sys updateer)
250 public async Task UpdatePandemic(Pandemic pandemic)
251 {
252 if (pandemic != null)
253 await _repository.UpdatePandemic(pandemic);
[d23bf72]254 else throw new Exception("Pandemic is null");
[1454207]255 }
256 //PharmacyHead
257 public async Task RemovePharmacy(Pharmacy pharmacy)
258 {
259 if (pharmacy != null)
260 await _repository.RemovePharmacy(pharmacy);
[d23bf72]261 else throw new Exception("Pharmacy is null");
[1454207]262 }
263 //PharamcyHead
264 public async Task UpdatePharmacy(Pharmacy pharmacy)
265 {
266 if (pharmacy != null)
267 await _repository.UpadatePharmacy(pharmacy);
[d23bf72]268 else throw new Exception("Pharmacy is null");
[1454207]269 }
270 //za json(Sys updateer)
271 public async Task UpdateWorker(HealthcareWorker worker)
[5d02859]272 {
273 if (worker != null)
[1454207]274 await _repository.UpdateWorker(worker);
[d23bf72]275 else throw new Exception("Worker is null");
[5d02859]276 }
277
[6f203af]278 public async Task RemovePharmacyHead(int Id)
[1454207]279 {
[6f203af]280 if (Id > 0)
[1454207]281 {
[6f203af]282 await _repository.RemovePharmacyHead(Id);
[1454207]283 }
[d23bf72]284 else throw new Exception("Index out of bounds.");
[1454207]285 }
286
[d23bf72]287 public HealthFacility GetFacilityJSON(string healthFacility)
[1454207]288 {
289 if (healthFacility != null)
[d23bf72]290 return _repository.GetFacilityJSON(healthFacility);
[1454207]291 return null;
292 }
293
[5d02859]294 //PUT (EDIT OBJECTS)
295
296
297 //DELETE
298
299 }
300}
Note: See TracBrowser for help on using the repository browser.