source: FarmatikoData/FarmatikoRepo/Repository.cs@ db484c9

Last change on this file since db484c9 was db484c9, checked in by DimitarSlezenkovski <dslezenkovski@…>, 23 months ago

Fix bugs

  • Property mode set to 100644
File size: 9.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
66 public async Task<IEnumerable<Pharmacy>> GetPharmacies()
67 {
68 var Pharmacies = await _context.Pharmacies.Take(5).ToListAsync();
69 return Pharmacies;
70 }
71
72 public async Task<Pharmacy> GetPharmacy(int id)
73 {
74 var Pharmacy = await _context.Pharmacies.FindAsync(id);
75 return Pharmacy;
76 }
77
78 public async Task<HealthcareWorker> GetWorker(int id)
79 {
80 var Worker = await _context.HealthcareWorkers.FindAsync(id);
81 return Worker;
82 }
83
84 public async Task<IEnumerable<HealthFacility>> SearchFacilities(string query)
85 {
86 var SearchQuery = await _context.HealthFacilities
87 .Where(x => x.Name.Contains(query))
88 .OrderBy(x => x.Name).ToListAsync();
89
90 return SearchQuery;
91 }
92
93 public async Task<IEnumerable<Medicine>> SearchMedicines(string query)
94 {
95 var SearchQuery = await _context.Medicines
96 .Where(x => x.Name.Contains(query))
97 .OrderBy(x => x.Name).ToListAsync();
98
99 return SearchQuery;
100 }
101
102 public async Task<IEnumerable<Pharmacy>> SearchPharmacies(string query)
103 {
104 var SearchQuery = await _context.Pharmacies.Take(5)
105 .Where(x => x.Name.Contains(query))
106 .OrderBy(x => x.Name).ToListAsync();
107
108 return SearchQuery;
109 }
110
111 public async Task<IEnumerable<HealthcareWorker>> SearchWorkers(string query)
112 {
113 var SearchQuery = await _context.HealthcareWorkers.Take(5)
114 .Where(x => x.Name.Contains(query))
115 .OrderBy(x => x.Name).ToListAsync();
116
117 return SearchQuery;
118 }
119 public HealthFacility GetFacilityJSON(string healthFacility)
120 {
121 var Facility = _context.HealthFacilities.Where(x => x.Name.Equals(healthFacility)).FirstOrDefault();
122 return Facility;
123 }
124
125 //POST
126
127 public async Task AddWorker(HealthcareWorker Worker)
128 {
129 await _context.HealthcareWorkers.AddAsync(Worker);
130 _context.SaveChanges();
131 }
132
133 public async Task AddFacility(HealthFacility healthFacility)
134 {
135 await _context.HealthFacilities.AddAsync(healthFacility);
136 _context.SaveChanges();
137 }
138
139 public async Task AddPharmacy(Pharmacy pharmacy)
140 {
141 await _context.Pharmacies.AddAsync(pharmacy);
142 _context.SaveChanges();
143 }
144
145 public async Task AddPharmacyHead(PharmacyHead pharmacyHead)
146 {
147 await _context.PharmacyHeads.AddAsync(pharmacyHead);
148 _context.SaveChanges();
149 }
150
151 public async Task AddMedicines(Medicine medicine)
152 {
153 await _context.Medicines.AddAsync(medicine);
154 _context.SaveChanges();
155 }
156
157 public async Task AddPandemic(Pandemic pandemic)
158 {
159 var pand = await _context.Pandemics.AddAsync(pandemic);
160 _context.SaveChanges();
161 }
162
163 public async Task UpdateFacility(HealthFacility healthFacility)
164 {
165 var Facility = await _context.HealthFacilities.Where(x => x.Id == healthFacility.Id).FirstOrDefaultAsync();
166 Facility.Address = healthFacility.Address;
167 Facility.Email = healthFacility.Email;
168 Facility.Municipality = healthFacility.Municipality;
169 Facility.Name = healthFacility.Name;
170 Facility.Phone = healthFacility.Phone;
171 Facility.Type = healthFacility.Type;
172 await _context.SaveChangesAsync();
173 }
174
175 public async Task RemoveMedicine(Medicine medicine)
176 {
177 _context.Medicines.Remove(medicine);
178 await _context.SaveChangesAsync();
179 }
180
181 public async Task UpdatePandemic(Pandemic pandemic)
182 {
183 var Pandemic = await _context.Pandemics.Where(x => x.Id == pandemic.Id).FirstOrDefaultAsync();
184 Pandemic.ActiveGlobal = pandemic.ActiveGlobal;
185 Pandemic.ActiveMK = pandemic.ActiveMK;
186 Pandemic.DeathsGlobal = pandemic.DeathsGlobal;
187 Pandemic.DeathsMK = pandemic.DeathsMK;
188 Pandemic.Name = pandemic.Name;
189 Pandemic.NewMK = pandemic.NewMK;
190 Pandemic.TotalGlobal = pandemic.TotalGlobal;
191 Pandemic.TotalMK = pandemic.TotalMK;
192 await _context.SaveChangesAsync();
193 }
194
195 public async Task RemovePharmacy(Pharmacy pharmacy)
196 {
197 _context.Pharmacies.Remove(pharmacy);
198 await _context.SaveChangesAsync();
199 }
200 //not impl
201 public Task UpdateWorker(HealthcareWorker worker)
202 {
203 throw new System.NotImplementedException();
204 }
205
206 public async Task UpadatePharmacy(Pharmacy pharmacy)
207 {
208 var Pharmacy = await _context.Pharmacies.Where(x => x.Id == pharmacy.Id).FirstOrDefaultAsync();
209 Pharmacy.Name = pharmacy.Name;
210 Pharmacy.Location = pharmacy.Location;
211 Pharmacy.WorkAllTime = pharmacy.WorkAllTime;
212 Pharmacy.Address = pharmacy.Address;
213 await _context.SaveChangesAsync();
214 }
215 //not implemented, not needed
216 public Task UpdateMedicine(Medicine medicine)
217 {
218 throw new NotImplementedException();
219 }
220
221 public async Task RemovePharmacyHead(int Id)
222 {
223 var PHead = await _context.PharmacyHeads.Where(x => x.Id == Id).FirstOrDefaultAsync();
224 PHead.DeletedOn = DateTime.UtcNow;
225 await _context.SaveChangesAsync();
226 }
227
228 public IDictionary<string, User> GetUsers()
229 {
230 var users = _context.Users.ToDictionary(x => x.Email, x => new User
231 {
232 Id = x.Id,
233 Name = x.Name,
234 Email = x.Email,
235 Password = x.Password,
236 UserRole = x.UserRole
237 });
238
239 return users;
240 }
241
242 public User GetRole(string userName)
243 {
244 var user = _context.Users.Where(x => x.Email.Equals(userName)).FirstOrDefault();
245 return user;
246 }
247
248 public ICollection<Medicine> GetMedicines()
249 {
250 var Medicines = _context.Medicines.Select(x => new Medicine
251 {
252 Id = x.Id,
253 Name = x.Name,
254 Strength = x.Strength,
255 Form = x.Form,
256 WayOfIssuing = x.WayOfIssuing,
257 Manufacturer = x.Manufacturer,
258 Price = x.Price,
259 Packaging = x.Packaging,
260 Medicines = x.Medicines
261
262 }).ToList();
263 return Medicines;
264 }
265
266 public ICollection<PharmacyHeadMedicine> GetPHMedicines(string email)
267 {
268 var head = _context.PharmacyHeads.Where(x => x.Email.Equals(email)).FirstOrDefault();
269 var phmeds = _context.PharmacyHeadMedicines.Where(x => x.PheadId == head.Id).Include(x => x.Medicine).ToList();
270 return phmeds;
271 }
272
273 public async Task AddUser(User user)
274 {
275 await _context.Users.AddAsync(user);
276 await _context.SaveChangesAsync();
277 }
278 }
279}
Note: See TracBrowser for help on using the repository browser.