public async Task <IActionResult> Add(CategoryViewModel viewModel) { if (viewModel == null) { ModelState.AddModelError(string.Empty, "View model is null"); } if (ModelState.IsValid) { ProcessResult <bool> categoryUniqueResult = await CategoryViewModelIsValid(viewModel); ModelState.AddProcessResultErrors(categoryUniqueResult); } if (ModelState.IsValid) { CategoryViewModelMapper mapper = new CategoryViewModelMapper(); PhotoCategory repositoryModel = mapper.BuildRepositoryModel(viewModel, null); ProcessResult <bool> saveResult = _db.SaveAdditions(repositoryModel); ModelState.AddProcessResultErrors(saveResult); } if (ModelState.IsValid) { return(RedirectToAction("Index")); } else { CategoryViewModelMapper mapper = new CategoryViewModelMapper(); viewModel = mapper.BuildViewModelDropdownCriteria(viewModel); return(View(viewModel)); } }
public async Task <ActionResult> Edit(CategoryViewModel viewModel) { if (viewModel == null) { ModelState.AddModelError(string.Empty, "View model is null"); } if (ModelState.IsValid) { ProcessResult <bool> categoryUniqueResult = await CategoryViewModelIsValid(viewModel, viewModel.CategoryId); ModelState.AddProcessResultErrors(categoryUniqueResult); } if (ModelState.IsValid) { ProcessResult <PhotoCategory> categoryProcessResult = await GetPhotoCategory(viewModel.CategoryId); ModelState.AddProcessResultErrors(categoryProcessResult); if (categoryProcessResult.Success) { CategoryViewModelMapper mapper = new CategoryViewModelMapper(); ProcessResult <List <Photograph> > photosToUpdateResult = new ProcessResult <List <Photograph> >(); if ((viewModel.StatusCode == CategoryStatusCodes.Judging || viewModel.StatusCode == CategoryStatusCodes.Completed) && categoryProcessResult.Result.StatusCode != viewModel.StatusCode) { photosToUpdateResult = await GetPhotoNameUpdatesAsync(viewModel.CategoryId, viewModel.PhotoNamingThemeCode); } // Save photo naming changes ProcessResult <bool> saveResult = new ProcessResult <bool>(); if (photosToUpdateResult.Success && (photosToUpdateResult.Result?.Count ?? 0) > 0) { saveResult = _db.SaveUpdates(photosToUpdateResult.Result.ToArray()); } // Save category changes if (photosToUpdateResult.Success && saveResult.Success) { PhotoCategory repositoryModel = mapper.BuildRepositoryModel(viewModel, categoryProcessResult.Result); saveResult = _db.SaveUpdates(repositoryModel); } ModelState.AddProcessResultErrors(photosToUpdateResult); ModelState.AddProcessResultErrors(saveResult); } } if (ModelState.IsValid) { return(RedirectToAction("Index")); } else { CategoryViewModelMapper mapper = new CategoryViewModelMapper(); viewModel = mapper.BuildViewModelDropdownCriteria(viewModel); return(View(viewModel)); } }