source: FarmatikoData/FarmatikoRepo/Repository.cs@ 1454207

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

Change structure, Add repo, services & controllers

  • Property mode set to 100644
File size: 7.3 KB
Line 
1using FarmatikoData.FarmatikoRepoInterfaces;
2using FarmatikoData.Models;
3using System.Linq;
4using System.Threading.Tasks;
5
6namespace FarmatikoData.FarmatikoRepo
7{
8 public class Repository : IRepository
9 {
10 private readonly FarmatikoDataContext _context;
11 public Repository(FarmatikoDataContext context)
12 {
13 _context = context;
14 }
15 //GET
16 public async Task<IQueryable<HealthcareWorker>> GetAllWorkers()
17 {
18 var Workers = await Task.Run(() => _context.HealthcareWorkers.Take(10));
19 return Workers;
20 }
21
22 public async Task<IQueryable<HealthFacility>> GetFacilities()
23 {
24 var Facilities = await Task.Run(() => _context.HealthFacilities.Take(10));
25 return Facilities;
26 }
27
28 public async Task<HealthFacility> GetFacility(int Id)
29 {
30 var Facility = await _context.HealthFacilities.FindAsync(Id);
31 return Facility;
32 }
33
34 public async Task<Medicine> GetMedicine(int Id)
35 {
36 var Medicine = await _context.Medicines.FindAsync(Id);
37 return Medicine;
38 }
39
40 public async Task<IQueryable<Medicine>> GetMedicines()
41 {
42 var Medicines = await Task.Run(() => _context.Medicines.Take(10));
43 return Medicines;
44 }
45
46 public async Task<Pandemic> GetPandemic()
47 {
48 var Pandemic = await Task.Run(() => _context.Pandemics.FirstOrDefault());
49 return Pandemic;
50 }
51
52 public async Task<IQueryable<Pharmacy>> GetPharmacies()
53 {
54 var Pharmacies = await Task.Run(() => _context.Pharmacies.Take(10));
55 return Pharmacies;
56 }
57
58 public async Task<Pharmacy> GetPharmacy(int id)
59 {
60 var Pharmacy = await _context.Pharmacies.FindAsync(id);
61 return Pharmacy;
62 }
63
64 public async Task<HealthcareWorker> GetWorker(int id)
65 {
66 var Worker = await _context.HealthcareWorkers.FindAsync(id);
67 return Worker;
68 }
69
70 public async Task<IQueryable<HealthFacility>> SearchFacilities(string query)
71 {
72 var SearchQuery = await Task.Run(() => _context.HealthFacilities
73 .Where(x => x.Name.Equals(query))
74 .OrderBy(x => x.Name));
75
76 return SearchQuery;
77 }
78
79 public async Task<IQueryable<Medicine>> SearchMedicines(string query)
80 {
81 var SearchQuery = await Task.Run(() => _context.Medicines
82 .Where(x => x.Name.Equals(query))
83 .OrderBy(x => x.Name));
84
85 return SearchQuery;
86 }
87
88 public async Task<IQueryable<Pharmacy>> SearchPharmacies(string query)
89 {
90 var SearchQuery = await Task.Run(() => _context.Pharmacies
91 .Where(x => x.Name.Equals(query))
92 .OrderBy(x => x.Name));
93
94 return SearchQuery;
95 }
96
97 public async Task<IQueryable<HealthcareWorker>> SearchWorkers(string query)
98 {
99 var SearchQuery = await Task.Run(() => _context.HealthcareWorkers
100 .Where(x => x.Name.Equals(query))
101 .OrderBy(x => x.Name));
102
103 return SearchQuery;
104 }
105 public async Task<HealthFacility> GetFacilityJSON(string healthFacility)
106 {
107 var Facility = await Task.Run(() => _context.HealthFacilities.Where(x => x.Name.Equals(healthFacility)).FirstOrDefault());
108 return Facility;
109 }
110
111 //POST
112
113 public async Task AddWorker(HealthcareWorker Worker)
114 {
115 await Task.Run(() => _context.HealthcareWorkers.Add(Worker));
116 await _context.SaveChangesAsync();
117 }
118
119 public async Task AddFacility(HealthFacility healthFacility)
120 {
121 await Task.Run(() => _context.HealthFacilities.Add(healthFacility));
122 await _context.SaveChangesAsync();
123 }
124
125 public async Task AddPharmacy(Pharmacy pharmacy)
126 {
127 await Task.Run(() => _context.Pharmacies.Add(pharmacy));
128 await _context.SaveChangesAsync();
129 }
130
131 public async Task AddPharmacyHead(PharmacyHead pharmacyHead)
132 {
133 await Task.Run(() => _context.PharmacyHeads.Add(pharmacyHead));
134 await _context.SaveChangesAsync();
135 }
136
137 public async Task AddMedicines(Medicine medicine)
138 {
139 await Task.Run(() => _context.Medicines.Add(medicine));
140 await _context.SaveChangesAsync();
141 }
142
143 public async Task AddPandemic(Pandemic pandemic)
144 {
145 await Task.Run(() => _context.Pandemics.Add(pandemic));
146 await _context.SaveChangesAsync();
147 }
148
149 public async Task UpdateFacility(HealthFacility healthFacility)
150 {
151 var Facility = await Task.Run(() => _context.HealthFacilities.Where(x => x.Id == healthFacility.Id).FirstOrDefault());
152 Facility.Address = healthFacility.Address;
153 Facility.Email = healthFacility.Email;
154 Facility.Municipality = healthFacility.Municipality;
155 Facility.Name = healthFacility.Name;
156 Facility.Phone = healthFacility.Phone;
157 Facility.Type = healthFacility.Type;
158 await _context.SaveChangesAsync();
159 }
160
161 public async Task RemoveMedicine(Medicine medicine)
162 {
163 await Task.Run(() => _context.Medicines.Remove(medicine));
164 await _context.SaveChangesAsync();
165 }
166
167 public async Task UpdatePandemic(Pandemic pandemic)
168 {
169 var Pandemic = await Task.Run(() => _context.Pandemics.Where(x => x.Id == pandemic.Id).FirstOrDefault());
170 Pandemic.ActiveGlobal = pandemic.ActiveGlobal;
171 Pandemic.ActiveMK = pandemic.ActiveMK;
172 Pandemic.DeathsGlobal = pandemic.DeathsGlobal;
173 Pandemic.DeathsMK = pandemic.DeathsMK;
174 Pandemic.Name = pandemic.Name;
175 Pandemic.NewMK = pandemic.NewMK;
176 Pandemic.TotalGlobal = pandemic.TotalGlobal;
177 Pandemic.TotalMK = pandemic.TotalMK;
178 await _context.SaveChangesAsync();
179 }
180
181 public async Task RemovePharmacy(Pharmacy pharmacy)
182 {
183 await Task.Run(() => _context.Pharmacies.Remove(pharmacy));
184 await _context.SaveChangesAsync();
185 }
186 //ke vidime
187 public Task UpdateWorker(HealthcareWorker worker)
188 {
189 throw new System.NotImplementedException();
190 }
191
192 public async Task UpadatePharmacy(Pharmacy pharmacy)
193 {
194 var Pharmacy = await Task.Run(() => _context.Pharmacies.Where(x => x.Id == pharmacy.Id).FirstOrDefault());
195 Pharmacy.Name = pharmacy.Name;
196 Pharmacy.Location = pharmacy.Location;
197 Pharmacy.WorkAllTime = pharmacy.WorkAllTime;
198 Pharmacy.Address = pharmacy.Address;
199 await _context.SaveChangesAsync();
200 }
201 //ke vidime
202 public Task UpdateMedicine(Medicine medicine)
203 {
204 throw new System.NotImplementedException();
205 }
206
207 public async Task RemovePharmacyHead(PharmacyHead pharmacyHead)
208 {
209 var PHead = await Task.Run(() => _context.PharmacyHeads.Where(x => x.Id == pharmacyHead.Id).FirstOrDefault());
210 PHead.DeletedOn = pharmacyHead.DeletedOn;
211 await _context.SaveChangesAsync();
212 }
213
214 }
215}
Note: See TracBrowser for help on using the repository browser.