Example #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);
        }
            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());
            }
Example #3
0
        public IActionResult Get(int page = 0, int pageSize = 10)
        {
            var ingredients = _repo.Get();

            if (ingredients.Any())
            {
                return(new ObjectResult(ingredients));
            }
            return(NoContent());
        }
Example #4
0
        public async Task <PaginatedList <IngredientModel> > Get(SearchModel model)
        {
            var spec = model.ToSpecification <Persistance.Models.Ingredients>();

            var entities = await _repository.Get(spec);

            var count = await _repository.CountAsync();

            return(new PaginatedList <IngredientModel>(
                       model.PageIndex,
                       entities.Count,
                       count,
                       _mapper.Map <IList <IngredientModel> >(entities)));
        }
Example #5
0
 public async Task <Ingredient> Handle(GetIngredientQuery query)
 {
     return(await _ingredientsRepository.Get(query.Slug));
 }
Example #6
0
 public Task <IngredientOnStock> Get(Guid id)
 {
     return(_ingredientsRepository.Get(id));
 }