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)); }
public async Task <IngredientDto> Create(CreateIngredientModel ingredient) { var mappedIngredient = _mapper.Map <Ingredient>(ingredient); var newIngredient = await _ingredientRepository.AddAsync(mappedIngredient); return(_mapper.Map <IngredientDto>(newIngredient)); }
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); }
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}")); } }
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}")); } }
public async Task <Ingredient> AddIngredientAsync(Ingredient ingredient) { return(await _ingredientRepository.AddAsync(ingredient)); }