public IngredientDTO AddNewIngredient(PostIngredientDTO ingredientDTO, string userId) { var newIngredient = _ingredientFactory.Create(ingredientDTO); newIngredient.UserId = userId; _uow.Ingredients.Add(newIngredient); _uow.SaveChanges(); return(_ingredientFactory.Create(newIngredient)); }
public async Task <IngredientBasicDTO> AddAsync(string name, string unit) { var doesIngredientExist = await context.Ingredients.AnyAsync(c => c.Name.ToLower() == name.ToLower()); if (doesIngredientExist) { throw new ArgumentException(OutputConstants.IngredientAlreadyExists); } var ingredient = ingredientFactory.Create(name, unit); context.Ingredients.Add(ingredient); await context.SaveChangesAsync(); var dbIngredient = await context.Ingredients .Select(i => new IngredientBasicDTO { Id = ingredient.Id, Name = ingredient.Name, Unit = ingredient.Unit }) .FirstAsync(i => i.Name == name); return(dbIngredient); }
private async Task <Ingredient> CreateIngredient(string ingredient) { Ingredient ingredientModel = null; if (!string.IsNullOrWhiteSpace(ingredient)) { ingredientModel = await ingredientFactory .Create() .GetIngredientAsync(ingredient.ToLower()); } return(ingredientModel); }
public async Task <Result> Handle(CreateIngredientCommand command) { foreach (var validator in _validators) { var validationResult = validator.Validate(command); if (validationResult.IsFailure) { return(validationResult); } } _ingredientFactory.Create( command.Id, command.Name, command.Allergens, command.Requirements, command.Shares); _eventPublisher.Rise(); return(Result.Ok()); }