Ignore:
Timestamp:
02/07/21 16:25:56 (3 years ago)
Author:
Dimitar Slezenkovski <dslezenkovski@…>
Branches:
master
Children:
e0cdea2
Parents:
ac51326
Message:

Fix create new user bug.

Location:
FarmatikoServices/Services
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • FarmatikoServices/Services/PHService.cs

    rac51326 r0a694bb  
    7373
    7474               
    75 
    76                 if (!pharmacyHead.Medicines.Equals(PHMedicines))
    77                 {
    78                     //phead.Medicines = pharmacyHead.Medicines;
    79                     if (pharmacyHead.Medicines.Count() == 0)
     75                if (pharmacyHead.Medicines != null || pharmacyHead.Medicines.Count() > 0)
     76                    if (!pharmacyHead.Medicines.Equals(PHMedicines))
    8077                    {
    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();
    85                         _iPHRepo.DeletePHMedicine(PHMId, phId, medId);
    86                         return;
     78                        //phead.Medicines = pharmacyHead.Medicines;
     79                        if (pharmacyHead.Medicines.Count() == 0)
     80                        {
     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();
     85                            _iPHRepo.DeletePHMedicine(PHMId, phId, medId);
     86                            return;
     87                        }
     88                        foreach (var med in pharmacyHead.Medicines)
     89                        {
     90
     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);
     103
     104                        }
     105
     106                        phead.Medicines = list;
     107
     108                        await _iPHRepo.UpdatePharmacyHead(phead);
     109
    87110                    }
    88                     foreach (var med in pharmacyHead.Medicines)
    89                     {
    90 
    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);
    103 
    104                     }
    105 
    106                     phead.Medicines = list;
    107 
    108                     await _iPHRepo.UpdatePharmacyHead(phead);
     111               
     112                if (pharmacyHead.Pharmacies != null || pharmacyHead.Pharmacies.Count() > 0)
     113                {
     114                    phead.Pharmacies = pharmacyHead.Pharmacies;
    109115                }
    110116                PharmacyHead head = new PharmacyHead()
     
    112118                    Name = pharmacyHead.Name,
    113119                    Email = pharmacyHead.Email,
    114                     Password = pharmacyHead.Password,
    115                     Pharmacies = pharmacyHead.Pharmacies,
    116                     Medicines = list
     120                    Password = pharmacyHead.Password
    117121                };
    118122                if (!phead.Equals(head))
  • FarmatikoServices/Services/Service.cs

    rac51326 r0a694bb  
    4848            List<MedicineDTO> list = new List<MedicineDTO>();
    4949            var listPHMedicines = await _repository.GetAllPHMedicines();
    50             foreach(var med in Medicines)
    51             {
    52                 var heads = listPHMedicines.Where(x => x.MedicineId == med.Id).Select(x => x.Head).ToList();
    53                 List<string> headNames = new List<string>();
     50            List<string> headNames = new List<string>();
     51            List<PharmacyHead> heads = new List<PharmacyHead>();
     52            foreach (var med in Medicines)
     53            {
     54                var meds = listPHMedicines.Where(x => x.MedicineId == med.Id).ToList();
     55                if (meds != null)
     56                {
     57                    heads = meds.Select(x => x.Head).ToList();
     58                }
    5459                headNames = heads.Select(x => x.Name).ToList();
    5560                MedicineDTO medicine = new MedicineDTO()
     
    8994                    Location = pharm.Location,
    9095                    Address = pharm.Address,
    91                     WorkAllTime = pharm.WorkAllTime,
    92                     HeadName = pharm.PharmacyHead.Name?? ""
     96                    WorkAllTime = pharm.WorkAllTime
    9397                };
     98                if (pharm.PharmacyHead != null)
     99                {
     100                    pharmacyDTO.HeadName = pharm.PharmacyHead.Name;
     101                }
     102
    94103                pharmacies.Add(pharmacyDTO);
    95104            }
     
    165174
    166175        // Ovaa kontrola ja ima samo admin
    167         public User MakeUser(PharmacyHead head)
    168         {
    169            
    170            
     176        public async Task<User> MakeUser(PharmacyHead head)
     177        {
    171178            User user = new User()
    172179            {
     
    176183                UserRole = User.Role.PharmacyHead
    177184            };
     185            bool Success = await _repository.AddUser(user);
     186            if (!Success)
     187                return null;
    178188            return user;
    179189        }
    180         public async Task AddPharmacyHead(PharmacyHead pharmacyHead)
     190        public async Task<bool> AddPharmacyHead(PharmacyHeadDto pharmacyHead)
    181191        {
    182192            if (pharmacyHead != null)
    183193            {
    184                 var user = MakeUser(pharmacyHead);
    185                 await _repository.AddUser(user);               
    186                 await _repository.AddPharmacyHead(pharmacyHead);
     194                var phead = new PharmacyHead()
     195                {
     196                    Name = pharmacyHead.Name,
     197                    Email = pharmacyHead.Email,
     198                    Password = pharmacyHead.Password
     199                };
     200                var user = await MakeUser(phead);
     201                if (user is null)
     202                {
     203                    return false;
     204                }
     205                var users = _repository.GetUsers();
     206                var thisUser = users.Where(usr => usr.Value.Email.Equals(user.Email)).Select(x => x.Value).FirstOrDefault();
     207                User user1 = new User()
     208                {
     209                    Name = thisUser.Name,
     210                    Password = thisUser.Password,
     211                    Email = thisUser.Email,
     212                    UserRole = thisUser.UserRole
     213                };
     214                phead.User = user1;
     215                await _repository.AddPharmacyHead(phead);
     216                return true;
    187217            }
    188             else throw new Exception("PharmacyHead is null");
     218            else throw new Exception("PharmacyHeadDto is null");
    189219        }
    190220        //za json(Sys updater)
Note: See TracChangeset for help on using the changeset viewer.