source: FarmatikoData/FarmatikoRepo/Repository.cs@ 7d80751

Last change on this file since 7d80751 was 7d80751, checked in by DimitarSlezenkovski <dslezenkovski@…>, 4 years ago

Fix create user

  • Property mode set to 100644
File size: 9.6 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 pharmacyHead.Id = 0;
148 if (pharmacyHead.Id == 0)
149 {
150 var pheads = await _context.PharmacyHeads.ToListAsync();
151 if (!pheads.Select(x => x.Equals(pharmacyHead)).FirstOrDefault())
152 {
153 await _context.PharmacyHeads.AddAsync(pharmacyHead);
154 await _context.SaveChangesAsync();
155 }
156 }
157 }
158
159 public async Task AddMedicines(Medicine medicine)
160 {
161 await _context.Medicines.AddAsync(medicine);
162 _context.SaveChanges();
163 }
164
165 public async Task AddPandemic(Pandemic pandemic)
166 {
167 var pand = await _context.Pandemics.AddAsync(pandemic);
168 _context.SaveChanges();
169 }
170
171 public async Task UpdateFacility(HealthFacility healthFacility)
172 {
173 var Facility = await _context.HealthFacilities.Where(x => x.Id == healthFacility.Id).FirstOrDefaultAsync();
174 Facility.Address = healthFacility.Address;
175 Facility.Email = healthFacility.Email;
176 Facility.Municipality = healthFacility.Municipality;
177 Facility.Name = healthFacility.Name;
178 Facility.Phone = healthFacility.Phone;
179 Facility.Type = healthFacility.Type;
180 await _context.SaveChangesAsync();
181 }
182
183 public async Task RemoveMedicine(Medicine medicine)
184 {
185 _context.Medicines.Remove(medicine);
186 await _context.SaveChangesAsync();
187 }
188
189 public async Task UpdatePandemic(Pandemic pandemic)
190 {
191 var Pandemic = await _context.Pandemics.Where(x => x.Id == pandemic.Id).FirstOrDefaultAsync();
192 Pandemic.ActiveGlobal = pandemic.ActiveGlobal;
193 Pandemic.ActiveMK = pandemic.ActiveMK;
194 Pandemic.DeathsGlobal = pandemic.DeathsGlobal;
195 Pandemic.DeathsMK = pandemic.DeathsMK;
196 Pandemic.Name = pandemic.Name;
197 Pandemic.NewMK = pandemic.NewMK;
198 Pandemic.TotalGlobal = pandemic.TotalGlobal;
199 Pandemic.TotalMK = pandemic.TotalMK;
200 await _context.SaveChangesAsync();
201 }
202
203 public async Task RemovePharmacy(Pharmacy pharmacy)
204 {
205 _context.Pharmacies.Remove(pharmacy);
206 await _context.SaveChangesAsync();
207 }
208 //not impl
209 public Task UpdateWorker(HealthcareWorker worker)
210 {
211 throw new System.NotImplementedException();
212 }
213
214 public async Task UpadatePharmacy(Pharmacy pharmacy)
215 {
216 var Pharmacy = await _context.Pharmacies.Where(x => x.Id == pharmacy.Id).FirstOrDefaultAsync();
217 Pharmacy.Name = pharmacy.Name;
218 Pharmacy.Location = pharmacy.Location;
219 Pharmacy.WorkAllTime = pharmacy.WorkAllTime;
220 Pharmacy.Address = pharmacy.Address;
221 await _context.SaveChangesAsync();
222 }
223 //not implemented, not needed
224 public Task UpdateMedicine(Medicine medicine)
225 {
226 throw new NotImplementedException();
227 }
228
229 public async Task RemovePharmacyHead(int Id)
230 {
231 var PHead = await _context.PharmacyHeads.Where(x => x.Id == Id).FirstOrDefaultAsync();
232 PHead.DeletedOn = DateTime.UtcNow;
233 await _context.SaveChangesAsync();
234 }
235
236 public IDictionary<string, User> GetUsers()
237 {
238 var users = _context.Users.ToDictionary(x => x.Email, x => new User
239 {
240 Id = x.Id,
241 Name = x.Name,
242 Email = x.Email,
243 Password = x.Password,
244 UserRole = x.UserRole
245 });
246
247 return users;
248 }
249
250 public User GetRole(string userName)
251 {
252 var user = _context.Users.Where(x => x.Email.Equals(userName)).FirstOrDefault();
253 return user;
254 }
255
256 public ICollection<Medicine> GetMedicines()
257 {
258 var Medicines = _context.Medicines.Select(x => new Medicine
259 {
260 Id = x.Id,
261 Name = x.Name,
262 Strength = x.Strength,
263 Form = x.Form,
264 WayOfIssuing = x.WayOfIssuing,
265 Manufacturer = x.Manufacturer,
266 Price = x.Price,
267 Packaging = x.Packaging,
268 Medicines = x.Medicines
269
270 }).ToList();
271 return Medicines;
272 }
273
274 public ICollection<PharmacyHeadMedicine> GetPHMedicines(string email)
275 {
276 var head = _context.PharmacyHeads.Where(x => x.Email.Equals(email)).FirstOrDefault();
277 var phmeds = _context.PharmacyHeadMedicines.Where(x => x.PheadId == head.Id).Include(x => x.Medicine).ToList();
278 return phmeds;
279 }
280
281 public async Task AddUser(User user)
282 {
283 if (user.Id == 0)
284 {
285 var users = await _context.Users.ToListAsync();
286 if (!users.Select(x => x.Equals(user)).FirstOrDefault())
287 {
288 await _context.Users.AddAsync(user);
289 await _context.SaveChangesAsync();
290 }
291 }
292 }
293 }
294}
Note: See TracBrowser for help on using the repository browser.