[5d02859] | 1 | using FarmatikoData.FarmatikoRepoInterfaces;
|
---|
| 2 | using FarmatikoData.Models;
|
---|
[6f203af] | 3 | using Microsoft.EntityFrameworkCore;
|
---|
| 4 | using System;
|
---|
| 5 | using System.Collections.Generic;
|
---|
[5d02859] | 6 | using System.Linq;
|
---|
| 7 | using System.Threading.Tasks;
|
---|
| 8 |
|
---|
| 9 | namespace 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 | }
|
---|
[afefe75] | 65 | public async Task<List<Pharmacy>> GetPharmacies()
|
---|
[5d02859] | 66 | {
|
---|
[afefe75] | 67 | var Pharmacies = await _context.Pharmacies.Select(x => new Pharmacy
|
---|
| 68 | {
|
---|
| 69 | Name = x.Name,
|
---|
| 70 | Location = x.Location,
|
---|
| 71 | Address = x.Address,
|
---|
| 72 | WorkAllTime = x.WorkAllTime,
|
---|
[0a694bb] | 73 | PheadId = x.PheadId,
|
---|
| 74 | PharmacyHead = x.PharmacyHead
|
---|
[afefe75] | 75 | }).Take(5).ToListAsync();
|
---|
[5d02859] | 76 | return Pharmacies;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | public async Task<Pharmacy> GetPharmacy(int id)
|
---|
| 80 | {
|
---|
| 81 | var Pharmacy = await _context.Pharmacies.FindAsync(id);
|
---|
| 82 | return Pharmacy;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[1454207] | 85 | public async Task<HealthcareWorker> GetWorker(int id)
|
---|
[5d02859] | 86 | {
|
---|
| 87 | var Worker = await _context.HealthcareWorkers.FindAsync(id);
|
---|
| 88 | return Worker;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[6f203af] | 91 | public async Task<IEnumerable<HealthFacility>> SearchFacilities(string query)
|
---|
[5d02859] | 92 | {
|
---|
[d23bf72] | 93 | var SearchQuery = await _context.HealthFacilities
|
---|
[0a694bb] | 94 | .Where(x => x.Name.ToLowerInvariant().Contains(query.ToLowerInvariant()))
|
---|
[db484c9] | 95 | .OrderBy(x => x.Name).ToListAsync();
|
---|
[5d02859] | 96 |
|
---|
| 97 | return SearchQuery;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[6f203af] | 100 | public async Task<IEnumerable<Medicine>> SearchMedicines(string query)
|
---|
[5d02859] | 101 | {
|
---|
[d23bf72] | 102 | var SearchQuery = await _context.Medicines
|
---|
[0a694bb] | 103 | .Where(x => x.Name.ToLowerInvariant().Contains(query.ToLowerInvariant()))
|
---|
[db484c9] | 104 | .OrderBy(x => x.Name).ToListAsync();
|
---|
[5d02859] | 105 |
|
---|
| 106 | return SearchQuery;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[6f203af] | 109 | public async Task<IEnumerable<Pharmacy>> SearchPharmacies(string query)
|
---|
[5d02859] | 110 | {
|
---|
[afefe75] | 111 | var SearchQuery = await _context.Pharmacies
|
---|
[0a694bb] | 112 | .Where(x => x.Name.ToLowerInvariant().Contains(query.ToLowerInvariant()))
|
---|
[db484c9] | 113 | .OrderBy(x => x.Name).ToListAsync();
|
---|
[5d02859] | 114 |
|
---|
| 115 | return SearchQuery;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[6f203af] | 118 | public async Task<IEnumerable<HealthcareWorker>> SearchWorkers(string query)
|
---|
[5d02859] | 119 | {
|
---|
[afefe75] | 120 | var SearchQuery = await _context.HealthcareWorkers
|
---|
[0a694bb] | 121 | .Where(x => x.Name.ToLowerInvariant().Contains(query.ToLowerInvariant()))
|
---|
[db484c9] | 122 | .OrderBy(x => x.Name).ToListAsync();
|
---|
[5d02859] | 123 |
|
---|
| 124 | return SearchQuery;
|
---|
| 125 | }
|
---|
[d23bf72] | 126 | public HealthFacility GetFacilityJSON(string healthFacility)
|
---|
[1454207] | 127 | {
|
---|
[d23bf72] | 128 | var Facility = _context.HealthFacilities.Where(x => x.Name.Equals(healthFacility)).FirstOrDefault();
|
---|
[1454207] | 129 | return Facility;
|
---|
| 130 | }
|
---|
[5d02859] | 131 |
|
---|
| 132 | //POST
|
---|
| 133 |
|
---|
[1454207] | 134 | public async Task AddWorker(HealthcareWorker Worker)
|
---|
[5d02859] | 135 | {
|
---|
[d23bf72] | 136 | await _context.HealthcareWorkers.AddAsync(Worker);
|
---|
| 137 | _context.SaveChanges();
|
---|
[5d02859] | 138 | }
|
---|
| 139 |
|
---|
[1454207] | 140 | public async Task AddFacility(HealthFacility healthFacility)
|
---|
[5d02859] | 141 | {
|
---|
[d23bf72] | 142 | await _context.HealthFacilities.AddAsync(healthFacility);
|
---|
| 143 | _context.SaveChanges();
|
---|
[5d02859] | 144 | }
|
---|
| 145 |
|
---|
| 146 | public async Task AddPharmacy(Pharmacy pharmacy)
|
---|
| 147 | {
|
---|
[d23bf72] | 148 | await _context.Pharmacies.AddAsync(pharmacy);
|
---|
| 149 | _context.SaveChanges();
|
---|
[5d02859] | 150 | }
|
---|
| 151 |
|
---|
| 152 | public async Task AddPharmacyHead(PharmacyHead pharmacyHead)
|
---|
| 153 | {
|
---|
[7d80751] | 154 | pharmacyHead.Id = 0;
|
---|
| 155 | if (pharmacyHead.Id == 0)
|
---|
| 156 | {
|
---|
[0a694bb] | 157 | var pheads = await _context.PharmacyHeads.Select(x => new PharmacyHead
|
---|
| 158 | {
|
---|
| 159 | Name = x.Name,
|
---|
| 160 | Email = x.Email
|
---|
| 161 | }).ToListAsync();
|
---|
| 162 | var pheadusr = pheads.Where(x => x.Email.Equals(pharmacyHead.Email)).ToList();
|
---|
| 163 | if (pheadusr == null || pheadusr.Count() == 0)
|
---|
[7d80751] | 164 | {
|
---|
| 165 | await _context.PharmacyHeads.AddAsync(pharmacyHead);
|
---|
| 166 | await _context.SaveChangesAsync();
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
[5d02859] | 169 | }
|
---|
| 170 |
|
---|
| 171 | public async Task AddMedicines(Medicine medicine)
|
---|
| 172 | {
|
---|
[d23bf72] | 173 | await _context.Medicines.AddAsync(medicine);
|
---|
| 174 | _context.SaveChanges();
|
---|
[5d02859] | 175 | }
|
---|
| 176 |
|
---|
| 177 | public async Task AddPandemic(Pandemic pandemic)
|
---|
| 178 | {
|
---|
[d23bf72] | 179 | var pand = await _context.Pandemics.AddAsync(pandemic);
|
---|
| 180 | _context.SaveChanges();
|
---|
[5d02859] | 181 | }
|
---|
[1454207] | 182 |
|
---|
| 183 | public async Task UpdateFacility(HealthFacility healthFacility)
|
---|
| 184 | {
|
---|
[d23bf72] | 185 | var Facility = await _context.HealthFacilities.Where(x => x.Id == healthFacility.Id).FirstOrDefaultAsync();
|
---|
[1454207] | 186 | Facility.Address = healthFacility.Address;
|
---|
| 187 | Facility.Email = healthFacility.Email;
|
---|
| 188 | Facility.Municipality = healthFacility.Municipality;
|
---|
| 189 | Facility.Name = healthFacility.Name;
|
---|
| 190 | Facility.Phone = healthFacility.Phone;
|
---|
| 191 | Facility.Type = healthFacility.Type;
|
---|
[db484c9] | 192 | await _context.SaveChangesAsync();
|
---|
[1454207] | 193 | }
|
---|
| 194 |
|
---|
| 195 | public async Task RemoveMedicine(Medicine medicine)
|
---|
| 196 | {
|
---|
[db484c9] | 197 | _context.Medicines.Remove(medicine);
|
---|
| 198 | await _context.SaveChangesAsync();
|
---|
[1454207] | 199 | }
|
---|
| 200 |
|
---|
| 201 | public async Task UpdatePandemic(Pandemic pandemic)
|
---|
| 202 | {
|
---|
[d23bf72] | 203 | var Pandemic = await _context.Pandemics.Where(x => x.Id == pandemic.Id).FirstOrDefaultAsync();
|
---|
[1454207] | 204 | Pandemic.ActiveGlobal = pandemic.ActiveGlobal;
|
---|
| 205 | Pandemic.ActiveMK = pandemic.ActiveMK;
|
---|
| 206 | Pandemic.DeathsGlobal = pandemic.DeathsGlobal;
|
---|
| 207 | Pandemic.DeathsMK = pandemic.DeathsMK;
|
---|
| 208 | Pandemic.Name = pandemic.Name;
|
---|
| 209 | Pandemic.NewMK = pandemic.NewMK;
|
---|
| 210 | Pandemic.TotalGlobal = pandemic.TotalGlobal;
|
---|
| 211 | Pandemic.TotalMK = pandemic.TotalMK;
|
---|
[db484c9] | 212 | await _context.SaveChangesAsync();
|
---|
[1454207] | 213 | }
|
---|
| 214 |
|
---|
| 215 | public async Task RemovePharmacy(Pharmacy pharmacy)
|
---|
| 216 | {
|
---|
[db484c9] | 217 | _context.Pharmacies.Remove(pharmacy);
|
---|
| 218 | await _context.SaveChangesAsync();
|
---|
[1454207] | 219 | }
|
---|
[6f203af] | 220 | //not impl
|
---|
[1454207] | 221 | public Task UpdateWorker(HealthcareWorker worker)
|
---|
| 222 | {
|
---|
| 223 | throw new System.NotImplementedException();
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | public async Task UpadatePharmacy(Pharmacy pharmacy)
|
---|
| 227 | {
|
---|
[d23bf72] | 228 | var Pharmacy = await _context.Pharmacies.Where(x => x.Id == pharmacy.Id).FirstOrDefaultAsync();
|
---|
[1454207] | 229 | Pharmacy.Name = pharmacy.Name;
|
---|
| 230 | Pharmacy.Location = pharmacy.Location;
|
---|
| 231 | Pharmacy.WorkAllTime = pharmacy.WorkAllTime;
|
---|
| 232 | Pharmacy.Address = pharmacy.Address;
|
---|
[db484c9] | 233 | await _context.SaveChangesAsync();
|
---|
[1454207] | 234 | }
|
---|
[db484c9] | 235 | //not implemented, not needed
|
---|
[1454207] | 236 | public Task UpdateMedicine(Medicine medicine)
|
---|
| 237 | {
|
---|
[1db5673] | 238 | throw new NotImplementedException();
|
---|
[1454207] | 239 | }
|
---|
| 240 |
|
---|
[6f203af] | 241 | public async Task RemovePharmacyHead(int Id)
|
---|
[1454207] | 242 | {
|
---|
[6f203af] | 243 | var PHead = await _context.PharmacyHeads.Where(x => x.Id == Id).FirstOrDefaultAsync();
|
---|
| 244 | PHead.DeletedOn = DateTime.UtcNow;
|
---|
[db484c9] | 245 | await _context.SaveChangesAsync();
|
---|
[1454207] | 246 | }
|
---|
| 247 |
|
---|
[d23bf72] | 248 | public IDictionary<string, User> GetUsers()
|
---|
| 249 | {
|
---|
| 250 | var users = _context.Users.ToDictionary(x => x.Email, x => new User
|
---|
| 251 | {
|
---|
| 252 | Id = x.Id,
|
---|
| 253 | Name = x.Name,
|
---|
| 254 | Email = x.Email,
|
---|
| 255 | Password = x.Password,
|
---|
| 256 | UserRole = x.UserRole
|
---|
| 257 | });
|
---|
[db484c9] | 258 |
|
---|
[d23bf72] | 259 | return users;
|
---|
| 260 | }
|
---|
[1db5673] | 261 |
|
---|
| 262 | public User GetRole(string userName)
|
---|
| 263 | {
|
---|
| 264 | var user = _context.Users.Where(x => x.Email.Equals(userName)).FirstOrDefault();
|
---|
| 265 | return user;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | public ICollection<Medicine> GetMedicines()
|
---|
| 269 | {
|
---|
| 270 | var Medicines = _context.Medicines.Select(x => new Medicine
|
---|
| 271 | {
|
---|
| 272 | Id = x.Id,
|
---|
| 273 | Name = x.Name,
|
---|
| 274 | Strength = x.Strength,
|
---|
| 275 | Form = x.Form,
|
---|
| 276 | WayOfIssuing = x.WayOfIssuing,
|
---|
| 277 | Manufacturer = x.Manufacturer,
|
---|
| 278 | Price = x.Price,
|
---|
| 279 | Packaging = x.Packaging,
|
---|
[db484c9] | 280 | Medicines = x.Medicines
|
---|
[1db5673] | 281 |
|
---|
| 282 | }).ToList();
|
---|
[68454c6] | 283 | return Medicines;
|
---|
[1db5673] | 284 | }
|
---|
| 285 |
|
---|
| 286 | public ICollection<PharmacyHeadMedicine> GetPHMedicines(string email)
|
---|
| 287 | {
|
---|
| 288 | var head = _context.PharmacyHeads.Where(x => x.Email.Equals(email)).FirstOrDefault();
|
---|
[db484c9] | 289 | var phmeds = _context.PharmacyHeadMedicines.Where(x => x.PheadId == head.Id).Include(x => x.Medicine).ToList();
|
---|
[1db5673] | 290 | return phmeds;
|
---|
| 291 | }
|
---|
[8e74e2f] | 292 |
|
---|
[0a694bb] | 293 | public async Task<bool> AddUser(User user)
|
---|
[8e74e2f] | 294 | {
|
---|
[7d80751] | 295 | if (user.Id == 0)
|
---|
| 296 | {
|
---|
[0a694bb] | 297 | var users = await _context.Users.Select(x => new User
|
---|
| 298 | {
|
---|
| 299 | Name = x.Name,
|
---|
| 300 | Email = x.Email,
|
---|
| 301 | Password = x.Password,
|
---|
| 302 | UserRole = x.UserRole
|
---|
| 303 | }).ToListAsync();
|
---|
| 304 | var usr = users.Where(x => x.Email.Equals(user.Email)).ToList();
|
---|
| 305 | if (usr != null || usr.Count() > 0)
|
---|
| 306 | {
|
---|
| 307 | return true;
|
---|
| 308 | }
|
---|
| 309 | else
|
---|
[7d80751] | 310 | {
|
---|
| 311 | await _context.Users.AddAsync(user);
|
---|
| 312 | await _context.SaveChangesAsync();
|
---|
[0a694bb] | 313 | return true;
|
---|
[7d80751] | 314 | }
|
---|
| 315 | }
|
---|
[0a694bb] | 316 | return false;
|
---|
[8e74e2f] | 317 | }
|
---|
[afefe75] | 318 |
|
---|
| 319 | public async Task<List<PharmacyHeadMedicine>> GetAllPHMedicines()
|
---|
| 320 | {
|
---|
[0a694bb] | 321 | var list = await _context.PharmacyHeadMedicines.Select(x => new PharmacyHeadMedicine
|
---|
| 322 | {
|
---|
| 323 | PheadId = x.PheadId,
|
---|
| 324 | Head = x.Head,
|
---|
| 325 | MedicineId = x.MedicineId,
|
---|
| 326 | Medicine = x.Medicine
|
---|
| 327 | }
|
---|
| 328 | ).ToListAsync();
|
---|
[afefe75] | 329 | return list;
|
---|
| 330 | }
|
---|
[5d02859] | 331 | }
|
---|
| 332 | }
|
---|