Esempio n. 1
0
        public ActionResult RemovePhoto(RemovePhotoViewModel model)
        {
            string userId = User.Identity.GetUserId();

            _logger.Info("User {0} tries to remove photo {1}", userId, model.PhotoName);

            ActionResult alternativeResult = ErrorIfNotAnOwner(model.UniqueUserName, Errors.AttemptToRemovePhoto, _userService, userId, _logger);

            if (alternativeResult == null)
            {
                alternativeResult = ErrorIfNoPhoto(model.PhotoName, model.UniqueUserName);

                if (alternativeResult == null)
                {
                    var request = new RequestEntity
                    {
                        UniqueUserName = model.UniqueUserName,
                        PhotoName      = model.PhotoName
                    };

                    _photoService.RemovePhoto(request);

                    _logger.Info("Photo {0} successfully deleted", model.PhotoName);
                    TempData["ResultMessage"] = string.Format(SuccessMessages.SuccessfullyRemovedPhoto, model.PhotoName);

                    return(RedirectToAction("ViewWall"));
                }
            }

            return(alternativeResult);
        }
        public async Task <ProcessResult <bool> > RemovePhoto(RemovePhotoViewModel viewModel)
        {
            ProcessResult <bool> result = new ProcessResult <bool>();

            ProcessResult <bool> categoryStatus = await CategoryIsInRequiredStatus(viewModel.CategoryId, CategoryStatusCodes.SubmittingPhotos);

            if (!categoryStatus.Success)
            {
                result.AddError(categoryStatus.ErrorMessage);
            }

            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);

                if (photo != null)
                {
                    result = _db.SaveRemoves(photo);
                }
                else
                {
                    result.AddError("Unable to find a photo with these details");
                }
            }

            return(result);
        }