[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();
|
---|
| 67 |
|
---|
| 68 | List<Medicine> medicines = _repository.GetMedicines().ToList();
|
---|
| 69 |
|
---|
| 70 | List<Medicine> PHMedicines = medicines.Where(x => x.Id == phead.Medicines.Select(x => x.MedicineId).Single()).ToList();
|
---|
| 71 |
|
---|
| 72 | List<PharmacyHeadMedicine> list = new List<PharmacyHeadMedicine>();
|
---|
[68454c6] | 73 |
|
---|
[db484c9] | 74 |
|
---|
[1db5673] | 75 |
|
---|
[db484c9] | 76 | if (!pharmacyHead.Medicines.Equals(PHMedicines))
|
---|
[1db5673] | 77 | {
|
---|
[db484c9] | 78 | //phead.Medicines = pharmacyHead.Medicines;
|
---|
| 79 | if (pharmacyHead.Medicines.Count() == 0)
|
---|
[68454c6] | 80 | {
|
---|
[db484c9] | 81 | phead.Medicines = null;
|
---|
| 82 | int PHMId = phead.Medicines.Select(x => x.Id).Single();
|
---|
| 83 | int phId = phead.Medicines.Select(x => x.PheadId).Single();
|
---|
| 84 | int medId = phead.Medicines.Select(x => x.MedicineId).Single();
|
---|
[68454c6] | 85 | _iPHRepo.DeletePHMedicine(PHMId, phId, medId);
|
---|
| 86 | return;
|
---|
| 87 | }
|
---|
[db484c9] | 88 | foreach (var med in pharmacyHead.Medicines)
|
---|
[1db5673] | 89 | {
|
---|
[8e74e2f] | 90 |
|
---|
[db484c9] | 91 | PharmacyHeadMedicine PHMObj = phead.Medicines.Select(x => new PharmacyHeadMedicine
|
---|
| 92 | {
|
---|
| 93 | Id = x.Id,
|
---|
| 94 | PheadId = x.PheadId,
|
---|
| 95 | Head = x.Head,
|
---|
| 96 | MedicineId = x.MedicineId,
|
---|
| 97 | Medicine = x.Medicine
|
---|
| 98 | }).Where(x => !x.Medicine.Equals(med)).Single();
|
---|
| 99 | if (PHMObj == null || PHMObj == default)
|
---|
| 100 | break;
|
---|
| 101 | if (PHMObj.MedicineId == med.Id)
|
---|
| 102 | list.Add(PHMObj);
|
---|
[8e74e2f] | 103 |
|
---|
| 104 | }
|
---|
[1db5673] | 105 |
|
---|
[db484c9] | 106 | phead.Medicines = list;
|
---|
| 107 |
|
---|
[1db5673] | 108 | await _iPHRepo.UpdatePharmacyHead(phead);
|
---|
| 109 | }
|
---|
[db484c9] | 110 | PharmacyHead head = new PharmacyHead()
|
---|
| 111 | {
|
---|
| 112 | Name = pharmacyHead.Name,
|
---|
| 113 | Email = pharmacyHead.Email,
|
---|
| 114 | Password = pharmacyHead.Password,
|
---|
| 115 | Pharmacies = pharmacyHead.Pharmacies,
|
---|
| 116 | Medicines = list
|
---|
| 117 | };
|
---|
| 118 | if (!phead.Equals(head))
|
---|
[8e74e2f] | 119 | {
|
---|
[db484c9] | 120 | await _iPHRepo.UpdatePharmacyHead(head);
|
---|
[8e74e2f] | 121 | }
|
---|
[1db5673] | 122 | else throw new Exception("Cannot update pharmacy head since there was no changes.");
|
---|
| 123 | }
|
---|
[1454207] | 124 | else throw new Exception("PharmacyHead has a null value.");
|
---|
| 125 | }
|
---|
[db484c9] | 126 | public async Task<bool> Add(PharmacyHeadDto pharmacyHead)
|
---|
[6f203af] | 127 | {
|
---|
| 128 | if (pharmacyHead != null)
|
---|
| 129 | {
|
---|
[db484c9] | 130 | PharmacyHead head = new PharmacyHead()
|
---|
| 131 | {
|
---|
| 132 | Name = pharmacyHead.Name,
|
---|
| 133 | Email = pharmacyHead.Email,
|
---|
| 134 | Password = pharmacyHead.Password,
|
---|
| 135 | Pharmacies = null,
|
---|
| 136 | Medicines = null
|
---|
| 137 | };
|
---|
| 138 | await _iPHRepo.Add(head);
|
---|
[6f203af] | 139 | return true;
|
---|
| 140 | }
|
---|
| 141 | return false;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | public async Task<bool> Remove(int id)
|
---|
| 145 | {
|
---|
| 146 | PharmacyHead Phead = await _iPHRepo.GetPharmacyHeadByIdAsync(id);
|
---|
| 147 | if (Phead != null && id >= 0)
|
---|
| 148 | {
|
---|
| 149 | Phead.DeletedOn = DateTime.UtcNow;
|
---|
| 150 | await _iPHRepo.Remove(Phead);
|
---|
| 151 | return true;
|
---|
| 152 | }
|
---|
| 153 | return false;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | public async Task<bool> RemoveClaimingRequest(int id)
|
---|
| 157 | {
|
---|
| 158 | if (id >= 0)
|
---|
| 159 | {
|
---|
| 160 | await _iPHRepo.RemoveClaimingRequest(id);
|
---|
| 161 | return true;
|
---|
| 162 | }
|
---|
| 163 | return false;
|
---|
| 164 | }
|
---|
[d23bf72] | 165 |
|
---|
[db484c9] | 166 | public PharmacyHeadDto GetPharmacyHead(string userName)
|
---|
[d23bf72] | 167 | {
|
---|
| 168 | if (userName != null)
|
---|
| 169 | {
|
---|
[1db5673] | 170 | var Phead = _iPHRepo.GetPharmacyHeadByUserName(userName);
|
---|
| 171 | List<PharmacyHeadMedicine> PHMedicines = _iPHRepo.GetPharmacyHeadMedicines(userName);
|
---|
| 172 | List<Medicine> Medicines = _repository.GetMedicines().ToList();
|
---|
[db484c9] | 173 | List<Medicine> MedicineList = new List<Medicine>();
|
---|
[1db5673] | 174 |
|
---|
| 175 | var user = _repository.GetRole(userName);
|
---|
| 176 |
|
---|
| 177 |
|
---|
| 178 | if (user.UserRole.ToString().Equals("Admin"))
|
---|
| 179 | {
|
---|
[db484c9] | 180 | List<Pharmacy> pharmacies = new List<Pharmacy>();
|
---|
| 181 | pharmacies = Phead.Pharmacies;
|
---|
| 182 | var Admin = new PharmacyHeadDto()
|
---|
[1db5673] | 183 | {
|
---|
| 184 | Id = user.Id,
|
---|
| 185 | Email = user.Email,
|
---|
| 186 | Name = user.Name,
|
---|
[8e74e2f] | 187 | Password = user.Password,
|
---|
[db484c9] | 188 | Pharmacies = pharmacies
|
---|
[1db5673] | 189 | };
|
---|
| 190 |
|
---|
| 191 | return Admin;
|
---|
| 192 | }
|
---|
[db484c9] | 193 | else
|
---|
[1db5673] | 194 | {
|
---|
| 195 | foreach (var med in Medicines)
|
---|
| 196 | {
|
---|
[db484c9] | 197 | var PHMObj = Phead.Medicines.Where(x => x.MedicineId == med.Id).SingleOrDefault();
|
---|
[8e74e2f] | 198 | if (PHMObj == null)
|
---|
[1db5673] | 199 | {
|
---|
[8e74e2f] | 200 | continue;
|
---|
| 201 | }
|
---|
[1db5673] | 202 | if (PHMObj.MedicineId == med.Id)
|
---|
| 203 | {
|
---|
[8e74e2f] | 204 | var medicine = new Medicine()
|
---|
| 205 | {
|
---|
| 206 | Id = med.Id,
|
---|
| 207 | Name = med.Name,
|
---|
| 208 | Strength = med.Strength,
|
---|
| 209 | Form = med.Form,
|
---|
| 210 | WayOfIssuing = med.WayOfIssuing,
|
---|
| 211 | Manufacturer = med.Manufacturer,
|
---|
| 212 | Price = med.Price,
|
---|
| 213 | Packaging = med.Packaging
|
---|
| 214 | };
|
---|
[db484c9] | 215 | MedicineList.Add(medicine);
|
---|
[1db5673] | 216 | }
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[db484c9] | 220 | PharmacyHeadDto pharmacyHead = new PharmacyHeadDto()
|
---|
[1db5673] | 221 | {
|
---|
| 222 | Id = Phead.Id,
|
---|
[db484c9] | 223 | Medicines = MedicineList,
|
---|
| 224 | Pharmacies = Phead.Pharmacies,
|
---|
[1db5673] | 225 | Email = Phead.Email,
|
---|
| 226 | Name = Phead.Name,
|
---|
| 227 | Password = Phead.Password
|
---|
| 228 | };
|
---|
[db484c9] | 229 | return pharmacyHead;
|
---|
[d23bf72] | 230 | }
|
---|
[1db5673] | 231 | else throw new Exception("Username is null.");
|
---|
[d23bf72] | 232 | }
|
---|
[1454207] | 233 | }
|
---|
| 234 | }
|
---|