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