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