コード例 #1
0
        public async Task Delete(int id)
        {
            var ingredient = await _ingredientsRepository.Get(id);

            if (ingredient == null)
            {
                throw new NotFoundException($"Ingredient with id: {id} not found");
            }

            var userId = _userContextService.GetUserId;

            if (userId is null || userId != ingredient.UserId)
            {
                throw new ForbidException();
            }

            var authorizationResult = await _authorizationService.AuthorizeAsync(_userContextService.User, ingredient,
                                                                                 new IngredientOperationRequirement(ResourceOperation.Delete));

            if (!authorizationResult.Succeeded)
            {
                throw new BadRequestException(
                          "You cannot update this recipe, because another" +
                          " user use this ingredient in his recipe");
            }

            await _ingredientsRepository.Delete(id);
        }
コード例 #2
0
            public async Task <Result> Handle(DeleteIngredientCommand command)
            {
                try
                {
                    var ingredient = await _ingredientsRepository.Get(command.Slug);

                    if (ingredient == null)
                    {
                        return(Result.Fail("Ingredient not found."));
                    }

                    await _ingredientsRepository.Delete(ingredient);
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!_ingredientsRepository.Exists(command.Slug))
                    {
                        return(Result.Fail("Recipe not found."));
                    }
                    else
                    {
                        throw;
                    }
                }
                catch (DbUpdateException /* ex */)
                {
                    return(Result.Fail("Delete failed. Try again, and if the problem persists " +
                                       "see your system administrator."));
                }

                return(Result.Ok());
            }
コード例 #3
0
        public async Task <IActionResult> Delete(int id)
        {
            if (await _repo.Delete <Ingredient>(id))
            {
                return(Ok());
            }

            return(BadRequest("This ingredient doesn't exist."));
        }
コード例 #4
0
        public async Task <IActionResult> DeleteRecipe(Guid id)
        {
            await _recipesRepository.Delete(id);

            foreach (var ingredient in await _ingredientsRepository.GetByRecipe(id))
            {
                await _ingredientsRepository.Delete(ingredient.Id);
            }

            foreach (var instruction in await _instructionsRepository.GetByRecipe(id))
            {
                await _instructionsRepository.Delete(instruction.Id);
            }

            return(Ok());
        }