public async Task <int> Update(RecipeModel recipe) { var recipeEntity = _mapper.Map <Recipe>(recipe); return(await _recipeRepository.Update(recipeEntity)); }
public async Task <Unit> Handle(UpdateDishCommand request, CancellationToken cancellationToken) { var user = await _userAuth.GetCurrentUser(); if (user == null) { throw new RestException(HttpStatusCode.Unauthorized, new { User = "******" }); } var dishCategory = await _dishCategory.GetDishCategory(request.DishCategoryId, user.Id); if (dishCategory == null) { throw new RestException(HttpStatusCode.NotFound, new { DishCategory = "Not found" }); } var dish = await _dish.GetDish(request.DishId, user.Id); if (dish == null) { throw new RestException(HttpStatusCode.NotFound, new { Dish = "Not found" }); } if (await _dish.IsDishExitsWithTitle(request.Title, user.Id)) { throw new RestException(HttpStatusCode.BadRequest, new { DishTitle = "Already exist" }); } bool haveIngredient = false; if (request.UpdateIngredients != null && request.UpdateIngredients.Count > 0) { haveIngredient = true; foreach (var test in request.UpdateIngredients) { if (!await _ingredient.IsIngredientExitById(test.IngredientId, user.Id)) { throw new RestException(HttpStatusCode.NotFound, new { Ingredient = "Not found" }); } //to check right combination of Ingredient and Dish if (!await _recipe.IsIdsExitInRecipeIngredient(test.IngredientId, request.DishId)) { throw new RestException(HttpStatusCode.NotFound, new { DishIngredient = "Not exist" }); } } } var updateDish = new Dish { Title = request.Title ?? dish.Title, Description = request.Description ?? dish.Description, DishCategoryId = request.DishCategoryId, }; var success = await _dish.Update(user.Id, request.DishId, updateDish); if (!success) { throw new Exception("Problem saving changes"); } if (haveIngredient) { foreach (var updateIngredient in request.UpdateIngredients) { var ingredientIdentityId = 0; var ingredientFromDB = await _ingredient.GetIngredient(user.Id, updateIngredient.IngredientId); if (ingredientFromDB != null) { var updateIngredent = new IngredientDto { Name = updateIngredient.Name ?? ingredientFromDB.Name, Description = updateIngredient.Description ?? ingredientFromDB.Description }; ingredientIdentityId = await _ingredient.Update(user.Id, updateIngredient.IngredientId, updateIngredent); if (ingredientIdentityId <= 0) { throw new Exception("Problem saving changes ingredients"); } } if (!string.IsNullOrWhiteSpace(updateIngredient.Amount)) { var newMeasurementResult = await _recipe.Update(request.DishId, updateIngredient.IngredientId, updateIngredient.Amount); if (newMeasurementResult <= 0) { throw new Exception("Problem saving changes with new dish list"); } } } } if (success) { return(Unit.Value); } throw new Exception("Problem saving changes"); }