public async Task <UserProfileEditViewModel> BuildProfileEditViewModel(ApplicationUser user) { UserProfileEditViewModel userProfileEditViewModel = new UserProfileEditViewModel(); userProfileEditViewModel.UserId = user.Id; userProfileEditViewModel.Description = user.Description; userProfileEditViewModel.Carer = user.Carer; if (user.Carer) { List <ServiceRateViewModel> serviceRateViewModelList = new List <ServiceRateViewModel>(); List <ServiceType> serviceTypes = (List <ServiceType>) await CommandFactory.CreateCommand(CommandFactory.GET_SERVICE_TYPES, _dbContext).Execute(); foreach (ServiceType service in serviceTypes) { ServiceTypeViewModel serviceTypeViewModel = new ServiceTypeViewModel(); ServiceRateViewModel serviceRateViewModel = new ServiceRateViewModel(); ServiceRate serviceRate = user.ServiceRates.FirstOrDefault(rate => rate.ServiceType.Id == service.Id); //if (!user.ServiceRates.Any(rate => rate.ServiceType == service)) if (serviceRate == null) { serviceTypeViewModel.Id = service.Id; serviceTypeViewModel.Name = service.Name; serviceRateViewModel.ServiceType = serviceTypeViewModel; serviceRateViewModel.Rate = 0; } else { serviceTypeViewModel.Id = service.Id; serviceTypeViewModel.Name = service.Name; serviceRateViewModel.ServiceType = serviceTypeViewModel; serviceRateViewModel.Rate = serviceRate.Rate; } serviceRateViewModelList.Add(serviceRateViewModel); } userProfileEditViewModel.ServiceRates = serviceRateViewModelList; } return(userProfileEditViewModel); }
public static ServiceRateModel ToServiceRateModel(this ServiceRate rate) { return(new ServiceRateModel(rate.ServiceName, (ServiceModel.TimeStep)rate.RateTimeStep, rate.TimeStamp, rate.Rate)); }
public async Task <int> UpdateUserProfile(UserProfileEditViewModel userProfileEditViewModel, ApplicationUser uneditedUser) { if (userProfileEditViewModel.ProfilePhoto != null) { string fileName = ""; string filePath = ""; fileName = Path.GetFileName(userProfileEditViewModel.ProfilePhoto.FileName); filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\Images\\User_Images\\", fileName); try { using (FileStream fileStream = new FileStream(filePath, FileMode.Create)) { await userProfileEditViewModel.ProfilePhoto.CopyToAsync(fileStream); } uneditedUser.PhotoFolderSource = Path.Combine("~/Images/User_Images/", fileName); } catch (Exception e) { return(0); } } if (uneditedUser.Description != userProfileEditViewModel.Description) { uneditedUser.Description = userProfileEditViewModel.Description; } if (userProfileEditViewModel.Carer == true) { foreach (ServiceRateViewModel serviceRateViewModel in userProfileEditViewModel.ServiceRates) { ServiceRate serviceRateTest = uneditedUser.ServiceRates.FirstOrDefault(rate => rate.ServiceType.Id == serviceRateViewModel.ServiceType.Id); if (serviceRateTest == null) { ServiceRate newServiceRate = new ServiceRate(); ServiceType serviceType = new ServiceType(); newServiceRate.Rate = serviceRateViewModel.Rate; newServiceRate.User = uneditedUser; newServiceRate.ServiceType = serviceType; newServiceRate.ServiceType.Id = serviceRateViewModel.ServiceType.Id; newServiceRate.ServiceType.Name = serviceRateViewModel.ServiceType.Name; uneditedUser.ServiceRates.Add(newServiceRate); } else { serviceRateTest.Rate = serviceRateViewModel.Rate; } foreach (ServiceRate serviceRate in uneditedUser.ServiceRates) { if (serviceRate.ServiceType.Id == serviceRateViewModel.ServiceType.Id) { serviceRate.Rate = serviceRateViewModel.Rate; } } } } int updateResult = (int)await CommandFactory.CreateCommand(CommandFactory.UPDATE_USER, uneditedUser, _dbContext).Execute(); return(updateResult); }