public async Task <IActionResult> EditProjectType(int id) { ProjectType projectType = await featureUnitOfWorkRepository.projectTypeRepository.GetProjectType(id); IEnumerable <Feature> features = featureUnitOfWorkRepository.featureRepository.GetAllFeatures(); IEnumerable <Feature> selectedFeatures = projectType.ProjectTypeFeatures.Select(x => new Feature() { FeatureId = x.Feature.FeatureId, FeatureText = x.Feature.FeatureText }); var selectList = new List <SelectListItem>(); foreach (Feature feature in features) { selectList.Add(new SelectListItem( feature.FeatureText, feature.FeatureId.ToString(), selectedFeatures.Select(x => x.FeatureId).Contains(feature.FeatureId) )); } var vm = new EditProjectTypeViewModel() { ProjectTypeId = projectType.ProjectTypeId, ProjectTypeName = projectType.ProjectTypeName, DaysToDeliver = projectType.DaysToDeliver, Rate = projectType.Rate, StarQuality = projectType.StarQuality, Features = selectList }; return(View(vm)); }
public async Task <IActionResult> EditProjectType(EditProjectTypeViewModel vm) { List <SelectListItem> itemsInSelectList = new List <SelectListItem>(); try { ProjectType projectType; projectType = await featureUnitOfWorkRepository.projectTypeRepository.GetProjectType(vm.ProjectTypeId); if (projectType == null) { throw new ProjectTypeNotFoundException("The requested Project Type does not exist", vm.ProjectTypeId); } var retrievedFeatures = featureUnitOfWorkRepository.featureRepository.GetAllFeatures(); var AllFeaturesInprojectType = projectType.ProjectTypeFeatures.Select(x => new Feature() { FeatureId = x.Feature.FeatureId, FeatureText = x.Feature.FeatureText }); foreach (var item in retrievedFeatures) { itemsInSelectList.Add(new SelectListItem { Value = item.FeatureId.ToString(), Text = item.FeatureText, Selected = AllFeaturesInprojectType.Select(x => x.FeatureId).Contains(item.FeatureId) }); } if (!ModelState.IsValid) { vm.Features = itemsInSelectList; return(View(vm)); } // update according to the View Model projectType.ProjectTypeName = vm.ProjectTypeName; projectType.Rate = vm.Rate; projectType.StarQuality = vm.StarQuality; var selectedFeatures = vm.SelectedFeatures; var existingFeatures = projectType.ProjectTypeFeatures.Select(x => x.FeatureId).ToList(); var existingFeaturesString = new List <string>(); existingFeatures.ForEach(x => existingFeaturesString.Add(x.ToString())); var toAdd = selectedFeatures.Except(existingFeaturesString).ToList(); var toRemove = existingFeaturesString.Except(selectedFeatures).ToList(); // using toAdd and toRemove projectType.ProjectTypeFeatures = projectType.ProjectTypeFeatures.Where(x => !toRemove.Contains(x.FeatureId.ToString())).ToList(); foreach (var item in toAdd) { if (!Int32.TryParse(item, out int resultId)) { ModelState.AddModelError(string.Empty, "Someone might've interfered with the inspect element :))"); return(View()); } projectType.ProjectTypeFeatures.Add(new ProjectTypeFeature() { FeatureId = resultId, ProjectTypeId = projectType.ProjectTypeId }); } featureUnitOfWorkRepository.Save(); actionFeedback.SuccessUnsuccess = true; logger.LogInformation($"A Project Type with Id: {projectType.ProjectTypeId} was successfully edited"); return(RedirectToAction("index")); } catch (ProjectTypeNotFoundException ex) { actionFeedback.SuccessUnsuccess = false; logger.LogError(ex, $"Unsuccessful operation. User {User.Identity.Name} requested ProjectTypeId: {ex.ProjectTypeId}. {ex.Message}"); return(RedirectToAction("Index")); } catch (Exception ex) { vm.Features = itemsInSelectList; ModelState.AddModelError(string.Empty, "Unsuccessful operation. A strange error occured. Try again later."); logger.LogError(ex, $"Unsuccessful operation. User {User.Identity.Name} requested ProjectTypeId: {vm.ProjectTypeId}. The task was unsuccessful. Message: {ex.Message}"); return(View(vm)); } }