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 <ProcessResult <string> > SubmitPhoto(SubmitSinglePhotoViewModel viewModel) { ProcessResult <string> result = new ProcessResult <string>(); ProcessResult <bool> categoryStatus = await CategoryIsInRequiredStatus(viewModel.CategoryId, CategoryStatusCodes.SubmittingPhotos); if (!categoryStatus.Success) { result.AddError(categoryStatus.ErrorMessage); } if (!viewModel.FileType.ToLower().StartsWith("image")) { result.AddError("Silly thing. Only an image file type is allowed"); } if (result.Success) { Photograph photo = await _db.Photographs .FirstOrDefaultAsync(p => p.Category.CategoryId == viewModel.CategoryId && p.UserCategoryPhotoNumber == viewModel.PhotoNumber && p.Photographer.UserName == User.Identity.Name); SubmitSinglePhotoViewModelMapper mapper = new SubmitSinglePhotoViewModelMapper(); ProcessResult <bool> saveResult = null; if (photo == null) { ApplicationUser user = await _userManager.FindByNameAsync(User.Identity.Name); if (user == null) { result.AddError("Unable to find this user"); } PhotoCategory category = await _db.PhotoCategories.FirstOrDefaultAsync(c => c.CategoryId == viewModel.CategoryId); if (category == null) { result.AddError("Unable to find this category"); } if (result.Success) { photo = mapper.BuildRepositoryModel(viewModel, user, category); saveResult = _db.SaveAdditions(photo); } } else { photo = mapper.BuildRepositoryModel(viewModel, photo); saveResult = _db.SaveUpdates(photo); } if (result.Success) { if (saveResult.Success) { result.SetResult(GetImageString(photo.SmallImage)); } else { result.AddError(saveResult.ErrorMessage); } } } return(result); }