source: FarmatikoData/FarmatikoRepo/Repository.cs@ 0a694bb

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

Fix create new user bug.

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