[1454207] | 1 | using FarmatikoData.FarmatikoRepoInterfaces;
|
---|
| 2 | using FarmatikoData.Models;
|
---|
| 3 | using FarmatikoServices.FarmatikoServiceInterfaces;
|
---|
| 4 | using System;
|
---|
| 5 | using System.Collections.Generic;
|
---|
| 6 | using System.Linq;
|
---|
| 7 | using System.Threading.Tasks;
|
---|
| 8 |
|
---|
| 9 | namespace FarmatikoServices.Services
|
---|
| 10 | {
|
---|
| 11 | public class PHService : IPHService
|
---|
| 12 | {
|
---|
| 13 | private readonly IPHRepo _iPHRepo;
|
---|
[1db5673] | 14 | private readonly IRepository _repository;
|
---|
| 15 | public PHService(IPHRepo iPHRepo, IRepository repository)
|
---|
[1454207] | 16 | {
|
---|
| 17 | _iPHRepo = iPHRepo;
|
---|
[1db5673] | 18 | _repository = repository;
|
---|
[1454207] | 19 | }
|
---|
| 20 |
|
---|
[6f203af] | 21 | public async Task<bool> ClaimPharmacy(RequestPharmacyHead pharmacy)
|
---|
[1454207] | 22 | {
|
---|
[6f203af] | 23 | if (pharmacy != null)
|
---|
| 24 | {
|
---|
| 25 | await _iPHRepo.ClaimPharmacy(pharmacy);
|
---|
| 26 | return true;
|
---|
| 27 | }
|
---|
| 28 | return false;
|
---|
[1454207] | 29 | }
|
---|
| 30 |
|
---|
[6f203af] | 31 | public async Task<PharmacyHead> GetPharmacyHeadByIdAsync(int id)
|
---|
[1454207] | 32 | {
|
---|
[6f203af] | 33 | PharmacyHead Phead = null;
|
---|
| 34 | if (id >= 0)
|
---|
| 35 | Phead = await _iPHRepo.GetPharmacyHeadByIdAsync(id);
|
---|
| 36 | if (Phead != null)
|
---|
| 37 | return Phead;
|
---|
| 38 | throw new Exception("No data found.");
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[d23bf72] | 41 | public async Task<IEnumerable<PharmacyHead>> GetPharmacyHeadInfo()
|
---|
[6f203af] | 42 | {
|
---|
| 43 | var PHeads = await _iPHRepo.GetPharmacyHeadInfo();
|
---|
| 44 | if (PHeads != null)
|
---|
| 45 | return PHeads;
|
---|
| 46 | throw new Exception("No Pharmacy heads found.");
|
---|
[1454207] | 47 | }
|
---|
| 48 |
|
---|
| 49 | public async Task<int> Login(PharmacyHead pharmacyHead)
|
---|
| 50 | {
|
---|
[6f203af] | 51 | var PHead = await _iPHRepo.GetPharmacyHeadByIdAsync(pharmacyHead.Id);
|
---|
[1454207] | 52 | if (PHead.Password.Equals(pharmacyHead.Password))
|
---|
| 53 | return PHead.Id;
|
---|
| 54 | return -1;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 |
|
---|
[1db5673] | 58 |
|
---|
[1454207] | 59 | public async Task UpdatePharmacyHead(PharmacyHead pharmacyHead)
|
---|
| 60 | {
|
---|
| 61 | if (pharmacyHead != null)
|
---|
[1db5673] | 62 | {
|
---|
| 63 | var phead = _iPHRepo.GetPharmacyHead(pharmacyHead.Email);
|
---|
| 64 |
|
---|
[8e74e2f] | 65 | /*if (pharmacyHead.PharmaciesList.Count() == 0)
|
---|
| 66 | pharmacyHead.PharmaciesList = null;*/
|
---|
[68454c6] | 67 |
|
---|
[1db5673] | 68 |
|
---|
| 69 | phead.PHMedicineList = _repository.GetPHMedicines(phead.Email);
|
---|
| 70 |
|
---|
[8e74e2f] | 71 | if (phead.MedicineList != pharmacyHead.MedicineList)
|
---|
[1db5673] | 72 | {
|
---|
| 73 | phead.MedicineList = pharmacyHead.MedicineList;
|
---|
| 74 | List<PharmacyHeadMedicine> list = new List<PharmacyHeadMedicine>();
|
---|
[68454c6] | 75 | if (pharmacyHead.MedicineList.Count() == 0)
|
---|
| 76 | {
|
---|
| 77 | phead.MedicineList = null;
|
---|
| 78 | int PHMId = phead.PHMedicineList.Select(x => x.Id).Single();
|
---|
| 79 | int phId = phead.PHMedicineList.Select(x => x.PheadId).Single();
|
---|
| 80 | int medId = phead.PHMedicineList.Select(x => x.MedicineId).Single();
|
---|
| 81 | _iPHRepo.DeletePHMedicine(PHMId, phId, medId);
|
---|
| 82 | return;
|
---|
| 83 | }
|
---|
[8e74e2f] | 84 | List<PharmacyHeadMedicine> PHMList = new List<PharmacyHeadMedicine>();
|
---|
| 85 | if (phead.PHMedicineList == pharmacyHead.PHMedicineList)
|
---|
[1db5673] | 86 | {
|
---|
[8e74e2f] | 87 | foreach (var med in phead.MedicineList)
|
---|
[1db5673] | 88 | {
|
---|
[8e74e2f] | 89 | //list = phead.PHMedicineList.ToList();
|
---|
| 90 |
|
---|
| 91 | var PHMObj = phead.PHMedicineList.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.MedicineId == med.Id).Single();
|
---|
| 99 | if (PHMObj == null || PHMObj == default)
|
---|
| 100 | break;
|
---|
| 101 | if (PHMObj.MedicineId == med.Id)
|
---|
| 102 | list.Add(PHMObj);
|
---|
[1db5673] | 103 |
|
---|
[8e74e2f] | 104 | }
|
---|
| 105 |
|
---|
| 106 | phead.PHMedicineList = list;
|
---|
| 107 | }
|
---|
[1db5673] | 108 |
|
---|
| 109 | await _iPHRepo.UpdatePharmacyHead(phead);
|
---|
| 110 | }
|
---|
[8e74e2f] | 111 | else if (!phead.Equals(pharmacyHead))
|
---|
| 112 | {
|
---|
| 113 | await _iPHRepo.UpdatePharmacyHead(pharmacyHead);
|
---|
| 114 | }
|
---|
[1db5673] | 115 | else throw new Exception("Cannot update pharmacy head since there was no changes.");
|
---|
| 116 | }
|
---|
[1454207] | 117 | else throw new Exception("PharmacyHead has a null value.");
|
---|
| 118 | }
|
---|
[6f203af] | 119 | public async Task<bool> Add(PharmacyHead pharmacyHead)
|
---|
| 120 | {
|
---|
| 121 | if (pharmacyHead != null)
|
---|
| 122 | {
|
---|
| 123 | await _iPHRepo.Add(pharmacyHead);
|
---|
| 124 | return true;
|
---|
| 125 | }
|
---|
| 126 | return false;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | public async Task<bool> Remove(int id)
|
---|
| 130 | {
|
---|
| 131 | PharmacyHead Phead = await _iPHRepo.GetPharmacyHeadByIdAsync(id);
|
---|
| 132 | if (Phead != null && id >= 0)
|
---|
| 133 | {
|
---|
| 134 | Phead.DeletedOn = DateTime.UtcNow;
|
---|
| 135 | await _iPHRepo.Remove(Phead);
|
---|
| 136 | return true;
|
---|
| 137 | }
|
---|
| 138 | return false;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | public async Task<bool> RemoveClaimingRequest(int id)
|
---|
| 142 | {
|
---|
| 143 | if (id >= 0)
|
---|
| 144 | {
|
---|
| 145 | await _iPHRepo.RemoveClaimingRequest(id);
|
---|
| 146 | return true;
|
---|
| 147 | }
|
---|
| 148 | return false;
|
---|
| 149 | }
|
---|
[d23bf72] | 150 |
|
---|
[1db5673] | 151 | public object GetPharmacyHead(string userName)
|
---|
[d23bf72] | 152 | {
|
---|
| 153 | if (userName != null)
|
---|
| 154 | {
|
---|
[1db5673] | 155 | var Phead = _iPHRepo.GetPharmacyHeadByUserName(userName);
|
---|
| 156 | List<PharmacyHeadMedicine> PHMedicines = _iPHRepo.GetPharmacyHeadMedicines(userName);
|
---|
| 157 | List<Medicine> Medicines = _repository.GetMedicines().ToList();
|
---|
| 158 | List<Medicine> PHMedicineList = new List<Medicine>();
|
---|
| 159 |
|
---|
| 160 |
|
---|
| 161 | //var meds = PHMedicines.Where(x => x.Id == Phead.Id).ToList();
|
---|
| 162 | var pharmacies = _iPHRepo.GetPharmacies();
|
---|
| 163 | var PheadPharms = pharmacies.Where(x => x.PheadId == Phead.Id).ToList();
|
---|
| 164 | var user = _repository.GetRole(userName);
|
---|
| 165 |
|
---|
| 166 |
|
---|
| 167 | if (user.UserRole.ToString().Equals("Admin"))
|
---|
| 168 | {
|
---|
[8e74e2f] | 169 | var Admin = new PharmacyHead()
|
---|
[1db5673] | 170 | {
|
---|
| 171 | Id = user.Id,
|
---|
| 172 | Email = user.Email,
|
---|
| 173 | Name = user.Name,
|
---|
[8e74e2f] | 174 | Password = user.Password,
|
---|
[1db5673] | 175 | PharmaciesList = Phead.PharmaciesList
|
---|
| 176 | };
|
---|
| 177 |
|
---|
| 178 | return Admin;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[8e74e2f] | 181 | if (PheadPharms.Count() > 0 || PheadPharms != null)
|
---|
| 182 | Phead.PharmaciesList = PheadPharms;
|
---|
[1db5673] | 183 | else Phead.PharmaciesList = pharmacies;
|
---|
| 184 |
|
---|
[8e74e2f] | 185 | if (Phead.PHMedicineList.Count() > 0 || Phead.PHMedicineList != null)
|
---|
[1db5673] | 186 | {
|
---|
| 187 | foreach (var med in Medicines)
|
---|
| 188 | {
|
---|
[8e74e2f] | 189 |
|
---|
| 190 | var PHMObj = Phead.PHMedicineList.Where(x => x.MedicineId == med.Id).SingleOrDefault();
|
---|
| 191 | if (PHMObj == null)
|
---|
[1db5673] | 192 | {
|
---|
[8e74e2f] | 193 | continue;
|
---|
| 194 | }
|
---|
| 195 | var phm = Phead.MedicineList;
|
---|
[1db5673] | 196 | if (PHMObj.MedicineId == med.Id)
|
---|
| 197 | {
|
---|
[8e74e2f] | 198 | var medicine = new Medicine()
|
---|
| 199 | {
|
---|
| 200 | Id = med.Id,
|
---|
| 201 | Name = med.Name,
|
---|
| 202 | Strength = med.Strength,
|
---|
| 203 | Form = med.Form,
|
---|
| 204 | WayOfIssuing = med.WayOfIssuing,
|
---|
| 205 | Manufacturer = med.Manufacturer,
|
---|
| 206 | Price = med.Price,
|
---|
| 207 | Packaging = med.Packaging
|
---|
| 208 | };
|
---|
[1db5673] | 209 | PHMedicineList.Add(medicine);
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 | Phead.MedicineList = PHMedicineList;
|
---|
| 213 | }
|
---|
| 214 | else
|
---|
| 215 | {
|
---|
| 216 | Phead.MedicineList = Medicines;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | PharmacyHead pharHead = new PharmacyHead()
|
---|
| 220 | {
|
---|
| 221 | Id = Phead.Id,
|
---|
| 222 | MedicineList = Phead.MedicineList,
|
---|
| 223 | PharmaciesList = Phead.PharmaciesList,
|
---|
| 224 | Email = Phead.Email,
|
---|
| 225 | Name = Phead.Name,
|
---|
| 226 | Password = Phead.Password
|
---|
| 227 | };
|
---|
| 228 | return pharHead;
|
---|
[d23bf72] | 229 | }
|
---|
[1db5673] | 230 | else throw new Exception("Username is null.");
|
---|
[d23bf72] | 231 | }
|
---|
[1454207] | 232 | }
|
---|
| 233 | }
|
---|