source: FarmatikoData/FarmatikoRepo/Repository.cs@ 1db5673

Last change on this file since 1db5673 was 1db5673, checked in by DimitarSlezenkovski <dslezenkovski@…>, 21 months ago

Fix bugs, add some more

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