Ignore:
Timestamp:
11/14/20 12:27:30 (3 years ago)
Author:
DimitarSlezenkovski <dslezenkovski@…>
Branches:
master
Children:
68454c6
Parents:
ad60966
Message:

Fix bugs, add some more

File:
1 edited

Legend:

Unmodified
Added
Removed
  • FarmatikoServices/Services/PHService.cs

    rad60966 r1db5673  
    1313    {
    1414        private readonly IPHRepo _iPHRepo;
    15         public PHService(IPHRepo iPHRepo)
     15        private readonly IRepository _repository;
     16        public PHService(IPHRepo iPHRepo, IRepository repository)
    1617        {
    1718            _iPHRepo = iPHRepo;
     19            _repository = repository;
    1820        }
    1921
     
    5557
    5658
     59
    5760        public async Task UpdatePharmacyHead(PharmacyHead pharmacyHead)
    5861        {
    5962            if (pharmacyHead != null)
    60                 await _iPHRepo.UpdatePharmacyHead(pharmacyHead);
     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            }
    6197            else throw new Exception("PharmacyHead has a null value.");
    6298        }
     
    93129        }
    94130
    95         public PharmacyHead GetPharmacyHead(string userName)
     131        public object GetPharmacyHead(string userName)
    96132        {
    97133            if (userName != null)
    98134            {
    99                 return _iPHRepo.GetPharmacyHeadByUserName(userName);
    100             }
    101             return default;
     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;
     202            }
     203            else throw new Exception("Username is null.");
    102204        }
    103205    }
Note: See TracChangeset for help on using the changeset viewer.