source: FarmatikoData/FarmatikoRepo/Repository.cs

Last change on this file was f554983, checked in by Dimitar Slezenkovski <dslezenkovski@…>, 22 months ago

Add cron job for updating data, with Quartz.NET

  • Property mode set to 100644
File size: 13.2 KB
Line 
1using FarmatikoData.FarmatikoRepoInterfaces;
2using FarmatikoData.Models;
3using Microsoft.EntityFrameworkCore;
4using System;
5using System.Collections.Generic;
6using System.Linq;
7using System.Threading.Tasks;
8
9namespace FarmatikoData.FarmatikoRepo
10{
11 public class Repository : IRepository
12 {
13 private readonly FarmatikoDataContext _context;
14 public Repository(FarmatikoDataContext context)
15 {
16 _context = context;
17 }
18 //GET
19 public async Task<IEnumerable<HealthcareWorker>> GetAllWorkers()
20 {
21 var Workers = await _context.HealthcareWorkers.Select(x => new HealthcareWorker
22 {
23 Id = x.Id,
24 Name = x.Name,
25 Branch = x.Branch,
26 Facility = x.Facility,
27 Title = x.Title
28 }).Take(5).ToListAsync();
29 return Workers;
30 }
31
32 public async Task<IEnumerable<HealthFacility>> GetFacilities()
33 {
34 var Facilities = await _context.HealthFacilities.Take(5).ToListAsync();
35 return Facilities;
36 }
37
38 public async Task<HealthFacility> GetFacility(int Id)
39 {
40 var Facility = await _context.HealthFacilities.FindAsync(Id);
41 return Facility;
42 }
43
44 public async Task<Medicine> GetMedicine(int Id)
45 {
46 var Medicine = await _context.Medicines.FindAsync(Id);
47 return Medicine;
48 }
49
50 public async Task<IEnumerable<Medicine>> GetMedicinesAsync()
51 {
52 var Medicines = await _context.Medicines.Select(x => new Medicine
53 {
54 Id = x.Id,
55 Name = x.Name,
56 Strength = x.Strength,
57 Form = x.Form,
58 WayOfIssuing = x.WayOfIssuing,
59 Manufacturer = x.Manufacturer,
60 Price = x.Price,
61 Packaging = x.Packaging
62
63 }).Take(5).ToListAsync();
64 return Medicines;
65 }
66
67 public async Task<Pandemic> GetPandemic()
68 {
69 var Pandemic = await _context.Pandemics.FirstOrDefaultAsync();
70 return Pandemic;
71 }
72 public async Task<List<Pharmacy>> GetPharmacies()
73 {
74 var Pharmacies = await _context.Pharmacies.Select(x => new Pharmacy
75 {
76 Name = x.Name,
77 Location = x.Location,
78 Address = x.Address,
79 WorkAllTime = x.WorkAllTime,
80 PheadId = x.PheadId,
81 PharmacyHead = x.PharmacyHead
82 }).Take(5).ToListAsync();
83 return Pharmacies;
84 }
85
86 public async Task<Pharmacy> GetPharmacy(int id)
87 {
88 var Pharmacy = await _context.Pharmacies.FindAsync(id);
89 return Pharmacy;
90 }
91
92 public async Task<HealthcareWorker> GetWorker(int id)
93 {
94 var Worker = await _context.HealthcareWorkers.FindAsync(id);
95 return Worker;
96 }
97
98 public async Task<IEnumerable<HealthFacility>> SearchFacilities(string query)
99 {
100 var SearchQuery = await _context.HealthFacilities
101 .Where(x => x.Name.ToLower().Contains(query.ToLower())
102 || x.Municipality.ToLower().Contains(query.ToLower())
103 || x.Type.ToLower().Contains(query.ToLower())).Take(5)
104 .OrderBy(x => x.Name).ToListAsync();
105
106 return SearchQuery;
107 }
108
109 public async Task<IEnumerable<Medicine>> SearchMedicines(string query)
110 {
111 var SearchQuery = await _context.Medicines
112 .Where(x => x.Name.ToLower().Contains(query.ToLower())
113 || x.Form.ToLower().Contains(query.ToLower())
114 || x.Strength.ToLower().Contains(query.ToLower())
115 || x.Packaging.ToLower().Contains(query.ToLower())).Take(20)
116 .OrderBy(x => x.Name).ToListAsync();
117
118 return SearchQuery;
119 }
120
121 public async Task<IEnumerable<Pharmacy>> SearchPharmacies(string query)
122 {
123 var SearchQuery = await _context.Pharmacies
124 .Where(x => x.Name.ToLower().Contains(query.ToLower())
125 || x.PharmacyHead.Name.ToLower().Contains(query.ToLower())).Take(5)
126 .OrderBy(x => x.Name).ToListAsync();
127
128 return SearchQuery;
129 }
130
131 public async Task<IEnumerable<HealthcareWorker>> SearchWorkers(string query)
132 {
133 var SearchQuery = await _context.HealthcareWorkers.Include(x => x.Facility)
134 .Where(x => x.Name.ToLower().Contains(query.ToLower())
135 || x.Facility.Name.ToLower().Contains(query.ToLower())).Take(20)
136 .OrderBy(x => x.Name).ToListAsync();
137
138 return SearchQuery;
139 }
140 public HealthFacility GetFacilityJSON(string healthFacility)
141 {
142 var Facility = _context.HealthFacilities.Where(x => x.Name.Equals(healthFacility)).FirstOrDefault();
143 return Facility;
144 }
145
146 //POST
147
148 public async Task AddWorker(HealthcareWorker Worker)
149 {
150 await _context.HealthcareWorkers.AddAsync(Worker);
151 _context.SaveChanges();
152 }
153
154 public async Task AddFacility(HealthFacility healthFacility)
155 {
156 await _context.HealthFacilities.AddAsync(healthFacility);
157 _context.SaveChanges();
158 }
159
160 public async Task AddPharmacy(Pharmacy pharmacy)
161 {
162 pharmacy.Id = 0;
163 if (pharmacy.Id == 0)
164 {
165 var phars = _context.Pharmacies.Select(x => new Pharmacy
166 {
167 Name = x.Name,
168 Location = x.Location,
169 Address = x.Address
170 }).ToList();
171 var pharms = phars.Where(x => x.Name.Equals(pharmacy.Name) && x.Location.Equals(pharmacy.Location) && x.Address.Equals(pharmacy.Address)).ToList();
172 if (pharms is null || pharms.Count() == 0)
173 {
174 await _context.Pharmacies.AddAsync(pharmacy);
175 _context.SaveChanges();
176 }
177
178 }
179 }
180
181 public async Task AddPharmacyHead(PharmacyHead pharmacyHead)
182 {
183 pharmacyHead.Id = 0;
184 if (pharmacyHead.Id == 0)
185 {
186 var pheads = await _context.PharmacyHeads.Select(x => new PharmacyHead
187 {
188 Name = x.Name,
189 Email = x.Email
190 }).ToListAsync();
191 var pheadusr = pheads.Where(x => x.Email.Equals(pharmacyHead.Email)).ToList();
192 if (pheadusr == null || pheadusr.Count() == 0)
193 {
194 await _context.PharmacyHeads.AddAsync(pharmacyHead);
195 await _context.SaveChangesAsync();
196 }
197 }
198 }
199
200 public async Task AddMedicines(Medicine medicine)
201 {
202 await _context.Medicines.AddAsync(medicine);
203 _context.SaveChanges();
204 }
205
206 public async Task AddPandemic(Pandemic pandemic)
207 {
208 var pand = await _context.Pandemics.AddAsync(pandemic);
209 _context.SaveChanges();
210 }
211
212 public async Task UpdateFacility(HealthFacility healthFacility)
213 {
214 var Facility = await _context.HealthFacilities.Where(x => x.Id == healthFacility.Id).FirstOrDefaultAsync();
215 Facility.Address = healthFacility.Address;
216 Facility.Email = healthFacility.Email;
217 Facility.Municipality = healthFacility.Municipality;
218 Facility.Name = healthFacility.Name;
219 Facility.Phone = healthFacility.Phone;
220 Facility.Type = healthFacility.Type;
221 await _context.SaveChangesAsync();
222 }
223
224 public async Task RemoveMedicine(Medicine medicine)
225 {
226 _context.Medicines.Remove(medicine);
227 await _context.SaveChangesAsync();
228 }
229
230 public async Task UpdatePandemic(Pandemic pandemic)
231 {
232 var Pandemic = await _context.Pandemics.Where(x => x.Id == pandemic.Id).FirstOrDefaultAsync();
233 Pandemic.ActiveGlobal = pandemic.ActiveGlobal;
234 Pandemic.ActiveMK = pandemic.ActiveMK;
235 Pandemic.DeathsGlobal = pandemic.DeathsGlobal;
236 Pandemic.DeathsMK = pandemic.DeathsMK;
237 Pandemic.Name = pandemic.Name;
238 Pandemic.NewMK = pandemic.NewMK;
239 Pandemic.TotalGlobal = pandemic.TotalGlobal;
240 Pandemic.TotalMK = pandemic.TotalMK;
241 await _context.SaveChangesAsync();
242 }
243
244 public async Task RemovePharmacy(Pharmacy pharmacy)
245 {
246 _context.Pharmacies.Remove(pharmacy);
247 await _context.SaveChangesAsync();
248 }
249 //not impl
250 public Task UpdateWorker(HealthcareWorker worker)
251 {
252 throw new System.NotImplementedException();
253 }
254
255 public async Task UpadatePharmacy(Pharmacy pharmacy)
256 {
257 var Pharmacy = await _context.Pharmacies.Where(x => x.Id == pharmacy.Id).FirstOrDefaultAsync();
258 Pharmacy.Name = pharmacy.Name;
259 Pharmacy.Location = pharmacy.Location;
260 Pharmacy.WorkAllTime = pharmacy.WorkAllTime;
261 Pharmacy.Address = pharmacy.Address;
262 await _context.SaveChangesAsync();
263 }
264 //not implemented, not needed
265 public Task UpdateMedicine(Medicine medicine)
266 {
267 throw new NotImplementedException();
268 }
269
270 public async Task RemovePharmacyHead(int Id)
271 {
272 var PHead = await _context.PharmacyHeads.Where(x => x.Id == Id).Include(x => x.Pharmacies).Include(x => x.Medicines).FirstOrDefaultAsync();
273 var user = await _context.Users.Where(x => x.Email.Equals(PHead.Email)).FirstOrDefaultAsync();
274 var PHreqs = await _context.PHRequests.Where(x => x.Head.Id.Equals(PHead.Id)).FirstOrDefaultAsync();
275 PHead.Pharmacies.Select(x => x.PheadId = null);
276 PHead.Pharmacies.Select(x => x.PharmacyHead = null);
277 _context.PHRequests.Remove(PHreqs);
278 _context.PharmacyHeads.Remove(PHead);
279 _context.Users.Remove(user);
280 _context.SaveChanges();
281 }
282
283 public IDictionary<string, User> GetUsers()
284 {
285 var users = _context.Users.ToDictionary(x => x.Email, x => new User
286 {
287 Id = x.Id,
288 Name = x.Name,
289 Email = x.Email,
290 Password = x.Password,
291 UserRole = x.UserRole
292 });
293
294 return users;
295 }
296
297 public User GetRole(string userName)
298 {
299 var user = _context.Users.Where(x => x.Email.Equals(userName)).FirstOrDefault();
300 return user;
301 }
302
303 public ICollection<Medicine> GetMedicines()
304 {
305 var Medicines = _context.Medicines.Select(x => new Medicine
306 {
307 Id = x.Id,
308 Name = x.Name,
309 Strength = x.Strength,
310 Form = x.Form,
311 WayOfIssuing = x.WayOfIssuing,
312 Manufacturer = x.Manufacturer,
313 Price = x.Price,
314 Packaging = x.Packaging,
315 Medicines = x.Medicines
316
317 }).ToList();
318 return Medicines;
319 }
320
321 public ICollection<PharmacyHeadMedicine> GetPHMedicines(string email)
322 {
323 var head = _context.PharmacyHeads.Where(x => x.Email.Equals(email)).FirstOrDefault();
324 var phmeds = _context.PharmacyHeadMedicines.Where(x => x.PheadId == head.Id).Include(x => x.Medicine).ToList();
325 return phmeds;
326 }
327
328 /*public async Task<bool> AddUser(User user)
329 {
330 if (user.Id == 0)
331 {
332 var users = await _context.Users.Select(x => new User
333 {
334 Name = x.Name,
335 Email = x.Email,
336 Password = x.Password,
337 UserRole = x.UserRole
338 }).ToListAsync();
339 var usr = users.Where(x => x.Email.Equals(user.Email)).ToList();
340 if (usr != null && usr.Count() > 0)
341 {
342 return true;
343 }
344 else
345 {
346 await _context.Users.AddAsync(user);
347 await _context.SaveChangesAsync();
348 return true;
349 }
350 }
351 return false;
352 }*/
353
354 public async Task<List<PharmacyHeadMedicine>> GetAllPHMedicines()
355 {
356 var list = await _context.PharmacyHeadMedicines.Select(x => new PharmacyHeadMedicine
357 {
358 PheadId = x.PheadId,
359 Head = x.Head,
360 MedicineId = x.MedicineId,
361 Medicine = x.Medicine
362 }
363 ).ToListAsync();
364 return list;
365 }
366
367 public ICollection<PharmacyHeadMedicine> GetPHMedicines()
368 {
369 var meds = _context.PharmacyHeadMedicines.Select(x => new PharmacyHeadMedicine
370 {
371 PheadId = x.PheadId,
372 Head = x.Head,
373 MedicineId = x.MedicineId,
374 Medicine = x.Medicine
375 }).ToList();
376 return meds;
377 }
378 }
379}
Note: See TracBrowser for help on using the repository browser.