Exemplo n.º 1
0
        public async Task <Response <Ingredient> > Handle(CreateIngredientCommand request, CancellationToken cancellationToken)
        {
            var ingredientEntity = new Ingredient()
            {
                Id = Guid.NewGuid()
            };

            if (string.IsNullOrEmpty(_appContext.UserName))
            {
                return(new Response <Ingredient>("Responsável pela operação não informado!"));
            }

            if (ingredientEntity.Cost > ingredientEntity.Price)
            {
                return(new Response <Ingredient>("O custo do ingrediente não pode ser maior que o preço de venda!"));
            }

            ingredientEntity.Name      = request.Name;
            ingredientEntity.Price     = request.Price;
            ingredientEntity.Cost      = request.Cost;
            ingredientEntity.Created   = DateTime.Now;
            ingredientEntity.CreatedBy = _appContext.UserName;

            await _repository.AddAsync(ingredientEntity);

            await _unitOfWork.CompleteAsync();

            return(new Response <Ingredient>(ingredientEntity));
        }
Exemplo n.º 2
0
        public async Task <IngredientDto> Create(CreateIngredientModel ingredient)
        {
            var mappedIngredient = _mapper.Map <Ingredient>(ingredient);
            var newIngredient    = await _ingredientRepository.AddAsync(mappedIngredient);

            return(_mapper.Map <IngredientDto>(newIngredient));
        }
Exemplo n.º 3
0
        public async Task <int> Handle(CreateIngredientCommand request, CancellationToken cancellationToken)
        {
            try
            {
                cancellationToken.ThrowIfCancellationRequested();
                await Task.Delay(1000, cancellationToken);
            }
            catch (Exception ex) when(ex is TaskCanceledException)
            {
                throw new TaskCanceledException("The user has cancelled the task!");
            }


            var ingredient = new IngredientsFromShop
            {
                Id    = request.Id,
                Name  = request.Name,
                Price = request.Price
            };
            await repository.AddAsync(ingredient);



            return(ingredient.Id);
        }
        public async Task <int> Handle(CreateIngredientCommand request, CancellationToken cancellation)
        {
            var ingredient = new IngredientsFromProduct
            {
                Id   = request.Id,
                Name = request.Name
            };
            await repository.AddAsync(ingredient);



            return(ingredient.Id);
        }
Exemplo n.º 5
0
        public async Task <IngredientResponse> SaveAsync(Ingredient ingredient)
        {
            try
            {
                await _ingredientRepository.AddAsync(ingredient);

                await _unitOfWork.CompleteAsync();

                return(new IngredientResponse(ingredient));
            }
            catch (Exception ex)
            {
                return(new IngredientResponse($"An error occurred when saving the ingredient: {ex.Message}"));
            }
        }
Exemplo n.º 6
0
        public async Task <IngredientResponse> SaveAsync(Ingredient ingredient, int recipeId)
        {
            var existingRecipe = await _recipeRepository.FindById(recipeId);

            if (existingRecipe == null)
            {
                return(new IngredientResponse("Recipe not found"));
            }
            ingredient.Recipe = existingRecipe;
            try
            {
                await _ingredientRepository.AddAsync(ingredient);

                await _unitOfWork.CompleteAsync();

                return(new IngredientResponse(ingredient));
            }
            catch (Exception ex)
            {
                return(new IngredientResponse($"An error ocurred while saving the Ingredient: {ex.Message}"));
            }
        }
Exemplo n.º 7
0
 public async Task <Ingredient> AddIngredientAsync(Ingredient ingredient)
 {
     return(await _ingredientRepository.AddAsync(ingredient));
 }