Beispiel #1
0
        public async Task Update(int id, IngredientRequest request)
        {
            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.Update));

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

            ingredient.Name        = request.Name;
            ingredient.Description = request.Description;

            await _ingredientsRepository.Update(ingredient);
        }
Beispiel #2
0
        public IActionResult Update(int id, IngredientForUpdateDto ingredientDto)
        {
            var ingredientToUpdate = _mapper.Map <Ingredient>(ingredientDto);

            _repo.Update(ingredientToUpdate);

            return(NoContent());
        }
        public void Update(Ingredient ingDb)
        {
            var ingredient = ingredientsRepository.GetById(ingDb.Id);

            if (ingredient != null)
            {
                ingredient.Name = ingDb.Name;
            }
            ingredientsRepository.Update(ingredient);
        }
Beispiel #4
0
            public async Task <Result> Handle(UpdateIngredientCommand command)
            {
                var ingredient = new Ingredient(command.Title, command.Description, command.Slug);

                try
                {
                    await _ingredientsRepository.Update(ingredient);
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!_ingredientsRepository.Exists(ingredient.Slug))
                    {
                        return(Result.Fail("Recipe not found."));
                    }
                    else
                    {
                        throw;
                    }
                }

                return(Result.Ok());
            }