source: FarmatikoServices/Services/Service.cs@ ac51326

Last change on this file since ac51326 was ac51326, checked in by Dimitar Slezenkovski <dslezenkovski@…>, 20 months ago

Fix null object reference error

Check if heads name is null, if null returns empty string

  • Property mode set to 100644
File size: 8.7 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 foreach(var med in Medicines)
51 {
52 var heads = listPHMedicines.Where(x => x.MedicineId == med.Id).Select(x => x.Head).ToList();
53 List<string> headNames = new List<string>();
54 headNames = heads.Select(x => x.Name).ToList();
55 MedicineDTO medicine = new MedicineDTO()
56 {
57 Name = med.Name,
58 Manufacturer = med.Manufacturer,
59 Packaging = med.Packaging,
60 Form = med.Form,
61 Price = med.Price,
62 Strength = med.Strength,
63 WayOfIssuing = med.WayOfIssuing,
64 HeadNames = headNames
65 };
66
67 list.Add(medicine);
68 }
69
70 return list;
71 }
72
73 public async Task<Pandemic> GetPandemic()
74 {
75 var Pandemic = await _repository.GetPandemic();
76 return Pandemic;
77 }
78
79 public async Task<List<PharmacyDTO>> GetPharmacies()
80 {
81 var Pharmacies = await _repository.GetPharmacies();
82 List<PharmacyDTO> pharmacies = new List<PharmacyDTO>();
83
84 foreach(var pharm in Pharmacies)
85 {
86 PharmacyDTO pharmacyDTO = new PharmacyDTO()
87 {
88 Name = pharm.Name,
89 Location = pharm.Location,
90 Address = pharm.Address,
91 WorkAllTime = pharm.WorkAllTime,
92 HeadName = pharm.PharmacyHead.Name?? ""
93 };
94 pharmacies.Add(pharmacyDTO);
95 }
96 return pharmacies;
97 }
98
99 public async Task<Pharmacy> GetPharmacy(int id)
100 {
101 var Pharmacy = await _repository.GetPharmacy(id);
102 return Pharmacy;
103 }
104
105 public async Task<HealthcareWorker> GetWorker(int id)
106 {
107 var Worker = await _repository.GetWorker(id);
108 return Worker;
109 }
110
111 public async Task<IEnumerable<HealthFacility>> SearchFacilities(string query)
112 {
113 var SearchQuery = await _repository.SearchFacilities(query);
114 return SearchQuery;
115 }
116
117 public async Task<IEnumerable<Medicine>> SearchMedicines(string query)
118 {
119 var SearchQuery = await _repository.SearchMedicines(query);
120 return SearchQuery;
121 }
122
123 public async Task<IEnumerable<Pharmacy>> SearchPharmacies(string query)
124 {
125 var SearchQuery = await _repository.SearchPharmacies(query);
126 return SearchQuery;
127 }
128
129 public async Task<IEnumerable<HealthcareWorker>> SearchWorkers(string query)
130 {
131 var SearchQuery = await _repository.SearchWorkers(query);
132 return SearchQuery;
133 }
134
135
136 //POST (ADD NEW OBJECTS)
137 //za json(Sys updateer)
138 public async Task AddFacility(HealthFacility healthFacilities)
139 {
140 if (healthFacilities != null)
141 await _repository.AddFacility(healthFacilities);
142 else throw new Exception("Facility is null");
143 }
144 //za json(Sys updateer)
145 public async Task AddMedicines(Medicine medicine)
146 {
147 if (medicine != null)
148 await _repository.AddMedicines(medicine);
149 else throw new Exception("Medicine is null");
150 }
151 //za json(Sys updateer)
152 public async Task AddPandemic(Pandemic pandemic)
153 {
154 if (pandemic != null)
155 await _repository.AddPandemic(pandemic);
156 else throw new Exception("Pandemic is null");
157 }
158 // Samo PharmacyHead i Admin imaat pristap
159 public async Task AddPharmacy(Pharmacy pharmacy)
160 {
161 if (pharmacy != null)
162 await _repository.AddPharmacy(pharmacy);
163 else throw new Exception("Pharmacy is null");
164 }
165
166 // Ovaa kontrola ja ima samo admin
167 public User MakeUser(PharmacyHead head)
168 {
169
170
171 User user = new User()
172 {
173 Name = head.Name,
174 Password = head.Password,
175 Email = head.Email,
176 UserRole = User.Role.PharmacyHead
177 };
178 return user;
179 }
180 public async Task AddPharmacyHead(PharmacyHead pharmacyHead)
181 {
182 if (pharmacyHead != null)
183 {
184 var user = MakeUser(pharmacyHead);
185 await _repository.AddUser(user);
186 await _repository.AddPharmacyHead(pharmacyHead);
187 }
188 else throw new Exception("PharmacyHead is null");
189 }
190 //za json(Sys updater)
191 public async Task AddWorker(HealthcareWorker worker)
192 {
193 if (worker != null)
194 await _repository.AddWorker(worker);
195 else throw new Exception("Worker is null");
196 }
197
198 //za json(Sys updateer)
199 public async Task UpdateFacility(HealthFacility healthFacilities)
200 {
201 if (healthFacilities != null)
202 await _repository.UpdateFacility(healthFacilities);
203 else throw new Exception("Facility is null");
204 }
205 //PharmacyHead
206 public async Task RemoveMedicine(Medicine medicine)
207 {
208 if (medicine != null)
209 await _repository.RemoveMedicine(medicine);
210 else throw new Exception("Medicine is null");
211 }
212 //PharmacyHead
213 public async Task UpdateMedicine(Medicine medicine)
214 {
215 if (medicine != null)
216 await _repository.UpdateMedicine(medicine);
217 else throw new Exception("Medicine is null");
218 }
219 //za json(Sys updateer)
220 public async Task UpdatePandemic(Pandemic pandemic)
221 {
222 if (pandemic != null)
223 await _repository.UpdatePandemic(pandemic);
224 else throw new Exception("Pandemic is null");
225 }
226 //PharmacyHead
227 public async Task RemovePharmacy(Pharmacy pharmacy)
228 {
229 if (pharmacy != null)
230 await _repository.RemovePharmacy(pharmacy);
231 else throw new Exception("Pharmacy is null");
232 }
233 //PharamcyHead
234 public async Task UpdatePharmacy(Pharmacy pharmacy)
235 {
236 if (pharmacy != null)
237 await _repository.UpadatePharmacy(pharmacy);
238 else throw new Exception("Pharmacy is null");
239 }
240 //za json(Sys updateer)
241 public async Task UpdateWorker(HealthcareWorker worker)
242 {
243 if (worker != null)
244 await _repository.UpdateWorker(worker);
245 else throw new Exception("Worker is null");
246 }
247
248 public async Task RemovePharmacyHead(int Id)
249 {
250 if (Id > 0)
251 {
252 await _repository.RemovePharmacyHead(Id);
253 }
254 else throw new Exception("Index out of bounds.");
255 }
256
257 public HealthFacility GetFacilityJSON(string healthFacility)
258 {
259 if (healthFacility != null)
260 return _repository.GetFacilityJSON(healthFacility);
261 return null;
262 }
263
264 //PUT (EDIT OBJECTS)
265
266
267 //DELETE
268
269 }
270}
Note: See TracBrowser for help on using the repository browser.