public ValidationResult ValidateRecipeUpdateInfo(RecipeUpdateInfoDto recipeUpdateInfoDto) { var result = new ValidationResult { Succeeded = true }; var message = new StringBuilder(); if (recipeUpdateInfoDto.Name.IsNullOrEmpty()) { result.Succeeded = false; message.AppendLine("Recipe name cannot be empty!"); } if (recipeUpdateInfoDto.FermentableIngredients.Count == 0) { result.Succeeded = false; message.AppendLine("No fermentables in recipe!"); } if (recipeUpdateInfoDto.HopIngredients.Count == 0) { result.Succeeded = false; message.AppendLine("No hops in recipe!"); } if (!result.Succeeded) { result.Message = message.ToString(); } return(result); }
public ActionResult UpdateRecipe([FromRoute] string recipeId, RecipeUpdateInfoDto recipeUpdateInfoDto) { try { _logger.LogInformation($"Entering {nameof(UpdateRecipe)}"); var userId = GetUserId(); var recipe = _homebrewingDbService.GetRecipe(recipeId); if (recipe == null) { return(new BadRequestResult()); } if (recipe.UserId != userId) { return(new ForbidResult()); } _homebrewingDbService.UpdateRecipe(recipeId, recipeUpdateInfoDto, userId); return(Ok()); } catch (ArgumentException e) { _logger.LogError(e.ToString()); return(new BadRequestObjectResult(new { message = e.Message })); } catch (InvalidOperationException e) { _logger.LogError(e.ToString()); return(new BadRequestObjectResult(new { message = e.Message })); } }
private RecipeProjectedOutcome GetRecipeProjectedOutcome(string recipeId, RecipeUpdateInfoDto recipeUpdateInfoDto, string userId) { var originalRecipe = GetRecipe(recipeId, userId); var brcFermentableIngredients = GetFermentableIngredients(recipeUpdateInfoDto.FermentableIngredients).ToList(); var brcHopIngredients = GetHopIngredients(recipeUpdateInfoDto.HopIngredients).ToList(); var yeast = GetYeast(originalRecipe.YeastId); var recipeProjectedOutcome = _recipeService.GetRecipeProjectedOutcome(originalRecipe.Size, brcFermentableIngredients, brcHopIngredients, yeast, recipeUpdateInfoDto.ExtractionEfficiency); return(recipeProjectedOutcome); }
public bool UpdateRecipe(string recipeId, RecipeUpdateInfoDto recipeUpdateInfoDto, string userId) { if (!IsIdValid(recipeId)) { throw new ArgumentException("Invalid parameter", nameof(recipeId)); } var validationResult = _recipeValidator.ValidateRecipeUpdateInfo(recipeUpdateInfoDto); if (!validationResult.Succeeded) { throw new InvalidOperationException(validationResult.Message); } var recipeProjectedOutcome = GetRecipeProjectedOutcome(recipeId, recipeUpdateInfoDto, userId); var recipeCollection = _database.GetCollection <Recipe>(RecipeCollectionName); var filter = Builders <Recipe> .Filter.Eq(r => r.Id, recipeId) & Builders <Recipe> .Filter.Eq(r => r.UserId, userId); var update = Builders <Recipe> .Update.Set(r => r.Name, recipeUpdateInfoDto.Name) .Set(r => r.ProjectedOutcome, recipeProjectedOutcome) .PullFilter(r => r.FermentableIngredients, r => true) .PullFilter(r => r.HopIngredients, r => true); var updateResult = recipeCollection.UpdateOne(filter, update); var fermentableIngredients = recipeUpdateInfoDto.FermentableIngredients .Select(f => _mapper.Map <FermentableIngredient>(f)) .OrderByDescending(f => f.Amount) .ToList(); var hopIngredients = recipeUpdateInfoDto.HopIngredients.Select(h => _mapper.Map <HopIngredient>(h)) .OrderByDescending(h => h.BoilAdditionTime).ToList(); var updateIngredients = Builders <Recipe> .Update .PushEach(r => r.FermentableIngredients, fermentableIngredients) .PushEach(r => r.HopIngredients, hopIngredients); recipeCollection.UpdateOne(filter, updateIngredients); return(updateResult.IsAcknowledged && updateResult.ModifiedCount > 0); }