source: FarmatikoData/FarmatikoRepo/Repository.cs@ d23bf72

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

Add SystemService, Auth, fix a lil bugs :)

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