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