source: FarmatikoData/FarmatikoRepo/Repository.cs@ db484c9

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

Fix bugs

  • Property mode set to 100644
File size: 9.1 KB
RevLine 
[5d02859]1using FarmatikoData.FarmatikoRepoInterfaces;
2using FarmatikoData.Models;
[6f203af]3using Microsoft.EntityFrameworkCore;
4using System;
5using System.Collections.Generic;
[5d02859]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
[6f203af]19 public async Task<IEnumerable<HealthcareWorker>> GetAllWorkers()
[5d02859]20 {
[d23bf72]21 var Workers = await _context.HealthcareWorkers.Take(5).ToListAsync();
[5d02859]22 return Workers;
23 }
24
[6f203af]25 public async Task<IEnumerable<HealthFacility>> GetFacilities()
[5d02859]26 {
[d23bf72]27 var Facilities = await _context.HealthFacilities.Take(5).ToListAsync();
[5d02859]28 return Facilities;
29 }
30
[1454207]31 public async Task<HealthFacility> GetFacility(int Id)
[5d02859]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
[1db5673]43 public async Task<IEnumerable<Medicine>> GetMedicinesAsync()
[5d02859]44 {
[1db5673]45 var Medicines = await _context.Medicines.Select(x => new Medicine
46 {
[8e74e2f]47 Id = x.Id,
[1db5673]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();
[5d02859]57 return Medicines;
58 }
59
60 public async Task<Pandemic> GetPandemic()
61 {
[d23bf72]62 var Pandemic = await _context.Pandemics.FirstOrDefaultAsync();
[5d02859]63 return Pandemic;
64 }
65
[6f203af]66 public async Task<IEnumerable<Pharmacy>> GetPharmacies()
[5d02859]67 {
[d23bf72]68 var Pharmacies = await _context.Pharmacies.Take(5).ToListAsync();
[5d02859]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
[1454207]78 public async Task<HealthcareWorker> GetWorker(int id)
[5d02859]79 {
80 var Worker = await _context.HealthcareWorkers.FindAsync(id);
81 return Worker;
82 }
83
[6f203af]84 public async Task<IEnumerable<HealthFacility>> SearchFacilities(string query)
[5d02859]85 {
[d23bf72]86 var SearchQuery = await _context.HealthFacilities
87 .Where(x => x.Name.Contains(query))
[db484c9]88 .OrderBy(x => x.Name).ToListAsync();
[5d02859]89
90 return SearchQuery;
91 }
92
[6f203af]93 public async Task<IEnumerable<Medicine>> SearchMedicines(string query)
[5d02859]94 {
[d23bf72]95 var SearchQuery = await _context.Medicines
96 .Where(x => x.Name.Contains(query))
[db484c9]97 .OrderBy(x => x.Name).ToListAsync();
[5d02859]98
99 return SearchQuery;
100 }
101
[6f203af]102 public async Task<IEnumerable<Pharmacy>> SearchPharmacies(string query)
[5d02859]103 {
[d23bf72]104 var SearchQuery = await _context.Pharmacies.Take(5)
105 .Where(x => x.Name.Contains(query))
[db484c9]106 .OrderBy(x => x.Name).ToListAsync();
[5d02859]107
108 return SearchQuery;
109 }
110
[6f203af]111 public async Task<IEnumerable<HealthcareWorker>> SearchWorkers(string query)
[5d02859]112 {
[d23bf72]113 var SearchQuery = await _context.HealthcareWorkers.Take(5)
114 .Where(x => x.Name.Contains(query))
[db484c9]115 .OrderBy(x => x.Name).ToListAsync();
[5d02859]116
117 return SearchQuery;
118 }
[d23bf72]119 public HealthFacility GetFacilityJSON(string healthFacility)
[1454207]120 {
[d23bf72]121 var Facility = _context.HealthFacilities.Where(x => x.Name.Equals(healthFacility)).FirstOrDefault();
[1454207]122 return Facility;
123 }
[5d02859]124
125 //POST
126
[1454207]127 public async Task AddWorker(HealthcareWorker Worker)
[5d02859]128 {
[d23bf72]129 await _context.HealthcareWorkers.AddAsync(Worker);
130 _context.SaveChanges();
[5d02859]131 }
132
[1454207]133 public async Task AddFacility(HealthFacility healthFacility)
[5d02859]134 {
[d23bf72]135 await _context.HealthFacilities.AddAsync(healthFacility);
136 _context.SaveChanges();
[5d02859]137 }
138
139 public async Task AddPharmacy(Pharmacy pharmacy)
140 {
[d23bf72]141 await _context.Pharmacies.AddAsync(pharmacy);
142 _context.SaveChanges();
[5d02859]143 }
144
145 public async Task AddPharmacyHead(PharmacyHead pharmacyHead)
146 {
[d23bf72]147 await _context.PharmacyHeads.AddAsync(pharmacyHead);
148 _context.SaveChanges();
[5d02859]149 }
150
151 public async Task AddMedicines(Medicine medicine)
152 {
[d23bf72]153 await _context.Medicines.AddAsync(medicine);
154 _context.SaveChanges();
[5d02859]155 }
156
157 public async Task AddPandemic(Pandemic pandemic)
158 {
[d23bf72]159 var pand = await _context.Pandemics.AddAsync(pandemic);
160 _context.SaveChanges();
[5d02859]161 }
[1454207]162
163 public async Task UpdateFacility(HealthFacility healthFacility)
164 {
[d23bf72]165 var Facility = await _context.HealthFacilities.Where(x => x.Id == healthFacility.Id).FirstOrDefaultAsync();
[1454207]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;
[db484c9]172 await _context.SaveChangesAsync();
[1454207]173 }
174
175 public async Task RemoveMedicine(Medicine medicine)
176 {
[db484c9]177 _context.Medicines.Remove(medicine);
178 await _context.SaveChangesAsync();
[1454207]179 }
180
181 public async Task UpdatePandemic(Pandemic pandemic)
182 {
[d23bf72]183 var Pandemic = await _context.Pandemics.Where(x => x.Id == pandemic.Id).FirstOrDefaultAsync();
[1454207]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;
[db484c9]192 await _context.SaveChangesAsync();
[1454207]193 }
194
195 public async Task RemovePharmacy(Pharmacy pharmacy)
196 {
[db484c9]197 _context.Pharmacies.Remove(pharmacy);
198 await _context.SaveChangesAsync();
[1454207]199 }
[6f203af]200 //not impl
[1454207]201 public Task UpdateWorker(HealthcareWorker worker)
202 {
203 throw new System.NotImplementedException();
204 }
205
206 public async Task UpadatePharmacy(Pharmacy pharmacy)
207 {
[d23bf72]208 var Pharmacy = await _context.Pharmacies.Where(x => x.Id == pharmacy.Id).FirstOrDefaultAsync();
[1454207]209 Pharmacy.Name = pharmacy.Name;
210 Pharmacy.Location = pharmacy.Location;
211 Pharmacy.WorkAllTime = pharmacy.WorkAllTime;
212 Pharmacy.Address = pharmacy.Address;
[db484c9]213 await _context.SaveChangesAsync();
[1454207]214 }
[db484c9]215 //not implemented, not needed
[1454207]216 public Task UpdateMedicine(Medicine medicine)
217 {
[1db5673]218 throw new NotImplementedException();
[1454207]219 }
220
[6f203af]221 public async Task RemovePharmacyHead(int Id)
[1454207]222 {
[6f203af]223 var PHead = await _context.PharmacyHeads.Where(x => x.Id == Id).FirstOrDefaultAsync();
224 PHead.DeletedOn = DateTime.UtcNow;
[db484c9]225 await _context.SaveChangesAsync();
[1454207]226 }
227
[d23bf72]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 });
[db484c9]238
[d23bf72]239 return users;
240 }
[1db5673]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,
[db484c9]260 Medicines = x.Medicines
[1db5673]261
262 }).ToList();
[68454c6]263 return Medicines;
[1db5673]264 }
265
266 public ICollection<PharmacyHeadMedicine> GetPHMedicines(string email)
267 {
268 var head = _context.PharmacyHeads.Where(x => x.Email.Equals(email)).FirstOrDefault();
[db484c9]269 var phmeds = _context.PharmacyHeadMedicines.Where(x => x.PheadId == head.Id).Include(x => x.Medicine).ToList();
[1db5673]270 return phmeds;
271 }
[8e74e2f]272
273 public async Task AddUser(User user)
274 {
275 await _context.Users.AddAsync(user);
276 await _context.SaveChangesAsync();
277 }
[5d02859]278 }
279}
Note: See TracBrowser for help on using the repository browser.