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