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