[db484c9] | 1 | using FarmatikoData.DTOs;
|
---|
| 2 | using FarmatikoData.FarmatikoRepoInterfaces;
|
---|
[1454207] | 3 | using FarmatikoData.Models;
|
---|
| 4 | using FarmatikoServices.FarmatikoServiceInterfaces;
|
---|
| 5 | using System;
|
---|
| 6 | using System.Collections.Generic;
|
---|
| 7 | using System.Linq;
|
---|
| 8 | using System.Threading.Tasks;
|
---|
| 9 |
|
---|
| 10 | namespace FarmatikoServices.Services
|
---|
| 11 | {
|
---|
| 12 | public class PHService : IPHService
|
---|
| 13 | {
|
---|
| 14 | private readonly IPHRepo _iPHRepo;
|
---|
[1db5673] | 15 | private readonly IRepository _repository;
|
---|
| 16 | public PHService(IPHRepo iPHRepo, IRepository repository)
|
---|
[1454207] | 17 | {
|
---|
| 18 | _iPHRepo = iPHRepo;
|
---|
[1db5673] | 19 | _repository = repository;
|
---|
[1454207] | 20 | }
|
---|
| 21 |
|
---|
[6f203af] | 22 | public async Task<bool> ClaimPharmacy(RequestPharmacyHead pharmacy)
|
---|
[1454207] | 23 | {
|
---|
[6f203af] | 24 | if (pharmacy != null)
|
---|
| 25 | {
|
---|
| 26 | await _iPHRepo.ClaimPharmacy(pharmacy);
|
---|
| 27 | return true;
|
---|
| 28 | }
|
---|
| 29 | return false;
|
---|
[1454207] | 30 | }
|
---|
| 31 |
|
---|
[6f203af] | 32 | public async Task<PharmacyHead> GetPharmacyHeadByIdAsync(int id)
|
---|
[1454207] | 33 | {
|
---|
[6f203af] | 34 | PharmacyHead Phead = null;
|
---|
| 35 | if (id >= 0)
|
---|
| 36 | Phead = await _iPHRepo.GetPharmacyHeadByIdAsync(id);
|
---|
| 37 | if (Phead != null)
|
---|
| 38 | return Phead;
|
---|
| 39 | throw new Exception("No data found.");
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[d23bf72] | 42 | public async Task<IEnumerable<PharmacyHead>> GetPharmacyHeadInfo()
|
---|
[6f203af] | 43 | {
|
---|
| 44 | var PHeads = await _iPHRepo.GetPharmacyHeadInfo();
|
---|
| 45 | if (PHeads != null)
|
---|
| 46 | return PHeads;
|
---|
| 47 | throw new Exception("No Pharmacy heads found.");
|
---|
[1454207] | 48 | }
|
---|
| 49 |
|
---|
| 50 | public async Task<int> Login(PharmacyHead pharmacyHead)
|
---|
| 51 | {
|
---|
[6f203af] | 52 | var PHead = await _iPHRepo.GetPharmacyHeadByIdAsync(pharmacyHead.Id);
|
---|
[1454207] | 53 | if (PHead.Password.Equals(pharmacyHead.Password))
|
---|
| 54 | return PHead.Id;
|
---|
| 55 | return -1;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 |
|
---|
[1db5673] | 59 |
|
---|
[db484c9] | 60 | public async Task UpdatePharmacyHead(PharmacyHeadDto pharmacyHead)
|
---|
[1454207] | 61 | {
|
---|
| 62 | if (pharmacyHead != null)
|
---|
[1db5673] | 63 | {
|
---|
| 64 | var phead = _iPHRepo.GetPharmacyHead(pharmacyHead.Email);
|
---|
| 65 |
|
---|
[db484c9] | 66 | phead.Medicines = _repository.GetPHMedicines(phead.Email).ToList();
|
---|
[e0cdea2] | 67 |
|
---|
| 68 | var phmeds = await _repository.GetAllPHMedicines();
|
---|
| 69 |
|
---|
[db484c9] | 70 | List<Medicine> medicines = _repository.GetMedicines().ToList();
|
---|
[e0cdea2] | 71 |
|
---|
| 72 | List<Medicine> PHMedicines = new List<Medicine>();
|
---|
| 73 |
|
---|
[db484c9] | 74 | List<PharmacyHeadMedicine> list = new List<PharmacyHeadMedicine>();
|
---|
[68454c6] | 75 |
|
---|
[e0cdea2] | 76 |
|
---|
| 77 | if (pharmacyHead.Medicines != null && pharmacyHead.Medicines.Count() > 0)
|
---|
| 78 | {
|
---|
| 79 | foreach (var med in phead.Medicines)
|
---|
| 80 | {
|
---|
| 81 | var medicine = medicines.Where(x => x.Id == med.MedicineId).FirstOrDefault();
|
---|
| 82 | if (medicine != null)
|
---|
| 83 | PHMedicines.Add(medicine);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[0a694bb] | 86 | if (!pharmacyHead.Medicines.Equals(PHMedicines))
|
---|
[1db5673] | 87 | {
|
---|
[8eb1e21] | 88 | /*
|
---|
| 89 | * USELESS
|
---|
| 90 | * if (pharmacyHead.Medicines.Count() == 0)
|
---|
[0a694bb] | 91 | {
|
---|
| 92 | phead.Medicines = null;
|
---|
| 93 | int PHMId = phead.Medicines.Select(x => x.Id).Single();
|
---|
| 94 | int phId = phead.Medicines.Select(x => x.PheadId).Single();
|
---|
| 95 | int medId = phead.Medicines.Select(x => x.MedicineId).Single();
|
---|
| 96 | _iPHRepo.DeletePHMedicine(PHMId, phId, medId);
|
---|
| 97 | return;
|
---|
[8eb1e21] | 98 | }*/
|
---|
[e0cdea2] | 99 | if (phead.Medicines != null && phead.Medicines.Count() > 0)
|
---|
[db484c9] | 100 | {
|
---|
[e0cdea2] | 101 | foreach (var med in pharmacyHead.Medicines)
|
---|
[0a694bb] | 102 | {
|
---|
[e0cdea2] | 103 | Medicine medicine = new Medicine()
|
---|
| 104 | {
|
---|
| 105 | Name = med.Name,
|
---|
| 106 | Form = med.Form,
|
---|
| 107 | Manufacturer = med.Manufacturer,
|
---|
| 108 | Medicines = med.Medicines,
|
---|
| 109 | Packaging = med.Packaging,
|
---|
| 110 | Price = med.Price,
|
---|
| 111 | Strength = med.Strength,
|
---|
| 112 | WayOfIssuing = med.WayOfIssuing
|
---|
| 113 | };
|
---|
[0a694bb] | 114 |
|
---|
[e0cdea2] | 115 | PharmacyHeadMedicine phm = new PharmacyHeadMedicine()
|
---|
| 116 | {
|
---|
| 117 | PheadId = phead.Id,
|
---|
| 118 | Head = phead,
|
---|
| 119 | MedicineId = med.Id,
|
---|
| 120 | Medicine = medicine
|
---|
| 121 | };
|
---|
[8eb1e21] | 122 |
|
---|
[e0cdea2] | 123 | bool ifExists = phead.Medicines.Contains(phm);
|
---|
| 124 | if (!ifExists)
|
---|
| 125 | list.Add(phm);
|
---|
| 126 |
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | else
|
---|
| 130 | {
|
---|
| 131 | foreach (var med in pharmacyHead.Medicines)
|
---|
| 132 | {
|
---|
| 133 | PharmacyHead head1 = new PharmacyHead()
|
---|
| 134 | {
|
---|
| 135 | Id = pharmacyHead.Id,
|
---|
| 136 | Name = pharmacyHead.Name,
|
---|
| 137 | Email = pharmacyHead.Email,
|
---|
| 138 | Password = pharmacyHead.Password
|
---|
| 139 | };
|
---|
| 140 | PharmacyHeadMedicine phMed = new PharmacyHeadMedicine()
|
---|
| 141 | {
|
---|
| 142 | Head = head1,
|
---|
| 143 | Medicine = med
|
---|
| 144 | };
|
---|
| 145 | list.Add(phMed);
|
---|
| 146 | }
|
---|
[0a694bb] | 147 | }
|
---|
| 148 |
|
---|
[e0cdea2] | 149 |
|
---|
[0a694bb] | 150 | phead.Medicines = list;
|
---|
[1db5673] | 151 |
|
---|
[0a694bb] | 152 | await _iPHRepo.UpdatePharmacyHead(phead);
|
---|
[db484c9] | 153 |
|
---|
[0a694bb] | 154 | }
|
---|
[1db5673] | 155 | }
|
---|
[db484c9] | 156 | PharmacyHead head = new PharmacyHead()
|
---|
| 157 | {
|
---|
| 158 | Name = pharmacyHead.Name,
|
---|
| 159 | Email = pharmacyHead.Email,
|
---|
[8eb1e21] | 160 | Password = pharmacyHead.Password,
|
---|
| 161 | Medicines = phead.Medicines,
|
---|
| 162 | Pharmacies = phead.Pharmacies
|
---|
[db484c9] | 163 | };
|
---|
[8eb1e21] | 164 | if (!phead.Name.Equals(head.Name) && !phead.Password.Equals(head.Email))
|
---|
[e0cdea2] | 165 | {
|
---|
[8eb1e21] | 166 | await _iPHRepo.UpdatePharmacyHead(head);
|
---|
[e0cdea2] | 167 | }
|
---|
[8eb1e21] | 168 | List<Pharmacy> pharmacies = new List<Pharmacy>();
|
---|
| 169 | pharmacies = phead.Pharmacies;
|
---|
| 170 | if (head.Pharmacies != null && pharmacyHead.Pharmacies != null)
|
---|
[8e74e2f] | 171 | {
|
---|
[8eb1e21] | 172 | if (head.Pharmacies.Count() > 0 && pharmacyHead.Pharmacies.Count() > 0)
|
---|
| 173 | {
|
---|
| 174 | foreach (var pharmacy in pharmacyHead.Pharmacies)
|
---|
| 175 | {
|
---|
| 176 | if (!head.Pharmacies.Contains(pharmacy))
|
---|
| 177 | {
|
---|
| 178 | pharmacy.PheadId = phead.Id;
|
---|
| 179 | pharmacy.PharmacyHead = phead;
|
---|
| 180 | pharmacies.Add(pharmacy);
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | head.Pharmacies = pharmacies;
|
---|
| 184 | await _iPHRepo.UpdatePharmacyHead(head);
|
---|
| 185 | }
|
---|
[8e74e2f] | 186 | }
|
---|
[1db5673] | 187 | }
|
---|
[8eb1e21] | 188 | else throw new Exception("Cannot update pharmacy head since there was no changes.");
|
---|
[1454207] | 189 | }
|
---|
[db484c9] | 190 | public async Task<bool> Add(PharmacyHeadDto pharmacyHead)
|
---|
[6f203af] | 191 | {
|
---|
| 192 | if (pharmacyHead != null)
|
---|
| 193 | {
|
---|
[db484c9] | 194 | PharmacyHead head = new PharmacyHead()
|
---|
| 195 | {
|
---|
| 196 | Name = pharmacyHead.Name,
|
---|
| 197 | Email = pharmacyHead.Email,
|
---|
| 198 | Password = pharmacyHead.Password,
|
---|
| 199 | Pharmacies = null,
|
---|
| 200 | Medicines = null
|
---|
| 201 | };
|
---|
| 202 | await _iPHRepo.Add(head);
|
---|
[6f203af] | 203 | return true;
|
---|
| 204 | }
|
---|
| 205 | return false;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | public async Task<bool> Remove(int id)
|
---|
| 209 | {
|
---|
| 210 | PharmacyHead Phead = await _iPHRepo.GetPharmacyHeadByIdAsync(id);
|
---|
| 211 | if (Phead != null && id >= 0)
|
---|
| 212 | {
|
---|
| 213 | Phead.DeletedOn = DateTime.UtcNow;
|
---|
| 214 | await _iPHRepo.Remove(Phead);
|
---|
| 215 | return true;
|
---|
| 216 | }
|
---|
| 217 | return false;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | public async Task<bool> RemoveClaimingRequest(int id)
|
---|
| 221 | {
|
---|
| 222 | if (id >= 0)
|
---|
| 223 | {
|
---|
| 224 | await _iPHRepo.RemoveClaimingRequest(id);
|
---|
| 225 | return true;
|
---|
| 226 | }
|
---|
| 227 | return false;
|
---|
| 228 | }
|
---|
[d23bf72] | 229 |
|
---|
[db484c9] | 230 | public PharmacyHeadDto GetPharmacyHead(string userName)
|
---|
[d23bf72] | 231 | {
|
---|
| 232 | if (userName != null)
|
---|
| 233 | {
|
---|
[1db5673] | 234 | var Phead = _iPHRepo.GetPharmacyHeadByUserName(userName);
|
---|
| 235 | List<PharmacyHeadMedicine> PHMedicines = _iPHRepo.GetPharmacyHeadMedicines(userName);
|
---|
| 236 | List<Medicine> Medicines = _repository.GetMedicines().ToList();
|
---|
[db484c9] | 237 | List<Medicine> MedicineList = new List<Medicine>();
|
---|
[1db5673] | 238 |
|
---|
| 239 | var user = _repository.GetRole(userName);
|
---|
| 240 |
|
---|
| 241 |
|
---|
| 242 | if (user.UserRole.ToString().Equals("Admin"))
|
---|
| 243 | {
|
---|
[db484c9] | 244 | List<Pharmacy> pharmacies = new List<Pharmacy>();
|
---|
| 245 | pharmacies = Phead.Pharmacies;
|
---|
| 246 | var Admin = new PharmacyHeadDto()
|
---|
[1db5673] | 247 | {
|
---|
| 248 | Id = user.Id,
|
---|
| 249 | Email = user.Email,
|
---|
| 250 | Name = user.Name,
|
---|
[8e74e2f] | 251 | Password = user.Password,
|
---|
[db484c9] | 252 | Pharmacies = pharmacies
|
---|
[1db5673] | 253 | };
|
---|
| 254 |
|
---|
| 255 | return Admin;
|
---|
| 256 | }
|
---|
[db484c9] | 257 | else
|
---|
[1db5673] | 258 | {
|
---|
| 259 | foreach (var med in Medicines)
|
---|
| 260 | {
|
---|
[7d80751] | 261 | if (Phead.Medicines == null || Phead.Medicines.Count() == 0)
|
---|
| 262 | break;
|
---|
[db484c9] | 263 | var PHMObj = Phead.Medicines.Where(x => x.MedicineId == med.Id).SingleOrDefault();
|
---|
[8e74e2f] | 264 | if (PHMObj == null)
|
---|
[1db5673] | 265 | {
|
---|
[8e74e2f] | 266 | continue;
|
---|
| 267 | }
|
---|
[1db5673] | 268 | if (PHMObj.MedicineId == med.Id)
|
---|
| 269 | {
|
---|
[8e74e2f] | 270 | var medicine = new Medicine()
|
---|
| 271 | {
|
---|
| 272 | Id = med.Id,
|
---|
| 273 | Name = med.Name,
|
---|
| 274 | Strength = med.Strength,
|
---|
| 275 | Form = med.Form,
|
---|
| 276 | WayOfIssuing = med.WayOfIssuing,
|
---|
| 277 | Manufacturer = med.Manufacturer,
|
---|
| 278 | Price = med.Price,
|
---|
| 279 | Packaging = med.Packaging
|
---|
| 280 | };
|
---|
[db484c9] | 281 | MedicineList.Add(medicine);
|
---|
[1db5673] | 282 | }
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[db484c9] | 286 | PharmacyHeadDto pharmacyHead = new PharmacyHeadDto()
|
---|
[1db5673] | 287 | {
|
---|
| 288 | Id = Phead.Id,
|
---|
[db484c9] | 289 | Medicines = MedicineList,
|
---|
| 290 | Pharmacies = Phead.Pharmacies,
|
---|
[1db5673] | 291 | Email = Phead.Email,
|
---|
| 292 | Name = Phead.Name,
|
---|
| 293 | Password = Phead.Password
|
---|
| 294 | };
|
---|
[db484c9] | 295 | return pharmacyHead;
|
---|
[d23bf72] | 296 | }
|
---|
[1db5673] | 297 | else throw new Exception("Username is null.");
|
---|
[d23bf72] | 298 | }
|
---|
[1454207] | 299 | }
|
---|
| 300 | }
|
---|