source: FarmatikoData/FarmatikoRepo/Repository.cs@ d23bf72

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

Add SystemService, Auth, fix a lil bugs :)

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