public async Task <IActionResult> UpdateRecipeAsync([FromBody] UpdateRecipeRequest request) { await _mediator.Send(new UpdateRecipeCommand( request.RecipeId, request.Name, request.Description, request.Category, request.RequiredTime, request.LevelOfDifficulty )); return(Ok()); }
public async Task <RecipeDetailsResponse> UpdateById(int recipeId, UpdateRecipeRequest request) { using var client = CreateClient(); var result = await client.PutJsonAsync($"/v1/recipe/{recipeId}", request); if (result.StatusCode == HttpStatusCode.NotFound) { throw new RecipeNotFoundException(); } return(await result.Content.ReadAsAsync <RecipeDetailsResponse>()); }
public async Task <IActionResult> Put(int id, [FromBody] UpdateRecipeRequest model) { try { model.Id = id; await Mediator.Send(model); return(Ok()); } catch (Exception e) { return(BadRequest(e.Message)); } }
public async void UpdateRecipe_WillReturn200() { var body = new { name = "My updated recipe", author = "*****@*****.**", style = "IPA", batchSize = 20.5 }; var request = new UpdateRecipeRequest { Name = "My updated recipe", Author = "*****@*****.**", Style = "IPA", BatchSize = 20.5 }; _mockProviderService .Given("There is a recipe with id 1") .UponReceiving("PUT to update recipe") .With(new ProviderServiceRequest { Method = HttpVerb.Put, Path = "/v1/recipe/1", Body = body, Headers = new Dictionary <string, object> { { "Content-Type", "application/json; charset=utf-8" }, } }) .WillRespondWith(new ProviderServiceResponse { Status = 200, Headers = new Dictionary <string, object> { { "Content-Type", "application/json" } }, Body = body }); var consumer = CreateConsumer(); var result = await consumer.UpdateById(1, request); Assert.Equal(request.Name, result.Name); _mockProviderService.VerifyInteractions(); }
public void Update(UpdateRecipeRequest model) { DataProvider.ExecuteNonQuery(GetConnection, "dbo.Recipes_UpdateById" , inputParamMapper : delegate(SqlParameterCollection paramCollection) { paramCollection.AddWithValue("@id", model.Id); paramCollection.AddWithValue("@name", model.Name); paramCollection.AddWithValue("@description", model.Description); paramCollection.AddWithValue("@directions", model.Directions); paramCollection.AddWithValue("@preptime", model.PrepTime); paramCollection.AddWithValue("@totaltime", model.TotalTime); paramCollection.AddWithValue("@numberofservings", model.NumberOfServings); paramCollection.AddWithValue("@mediaid", model.MediaId); } ); }
public HttpResponseMessage UpdateRecipe(UpdateRecipeRequest model, int recipeid) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } model.Id = recipeid; //Update Recipe _RecipesService.Update(model); // Insert new ingredients in recipe edit mode if (model.Ingredients != null) { _IngredientsService.InsertIngredient(model.Ingredients, model.Id); } return(Request.CreateResponse(HttpStatusCode.OK)); }
public async Task <IActionResult> Update([FromBody] UpdateRecipeRequest recipeRequest) { if (recipeRequest.Name == null || recipeRequest.Description == null) { return(BadRequest(new { error = "Null variables [Name and Description] are not allowed" })); } if (await _recipeService.GetRecipeByIdAsync((int)recipeRequest.Id) == null) { return(BadRequest(new { error = "Recipe not found!" })); } var recipe = await _recipeService.GetRecipeByIdAsync((int)recipeRequest.Id); recipe.Name = recipeRequest.Name; recipe.Description = recipeRequest.Description; await _recipeService.UpdateRecipeAsync(recipe); return(Ok(recipe)); }
public ActionResult <RecipeDto> UpdateRecipe([Required] Guid id, [FromBody] UpdateRecipeRequest request) { Recipe recipe = context.Recipes.SingleOrDefault(x => x.Id == id); if (recipe == null) { return(this.NotFound()); } recipe.Name = request.Name; recipe.OriginalGravity = request.OriginalGravity; recipe.FinalGravity = request.FinalGravity; recipe.BatchSize = request.BatchSize; recipe.BoilSize = request.BoilSize; recipe.Author = request.Author; context.Recipes.Update(recipe); context.SaveChanges(); return(recipe.ToDto()); }
public async Task <Recipe> UpdateRecipe(Recipe recipe) { var recipeId = recipe.Id ?? throw new InvalidOperationException("Can't update recipe without id"); var request = new UpdateRecipeRequest { Name = recipe.Name, Author = recipe.Author, Style = recipe.Style, BatchSize = recipe.BatchSize }; var result = await _recipeServiceClient.UpdateById(recipeId, request); await _recipeDetailsCache.Clear(recipeId.ToString()); return(new Recipe { Id = result.Id, Name = result.Name, Author = result.Author, Style = recipe.Style, BatchSize = recipe.BatchSize, Hops = result.Hops.Select(h => new Hop { Id = h.Id, Name = h.Name, Quantity = h.Quantity, Time = h.Time }), Fermentables = result.Fermentables.Select(f => new Fermentable { Id = f.Id, Name = f.Name, Quantity = f.Quantity, Color = f.Color }) }); }
public async Task <IActionResult> Edit([FromRoute] Guid id, [FromBody] UpdateRecipeRequest request) { var recipe = await repository.GetRecipe(id); if (recipe == null) { throw new RestException(HttpStatusCode.NotFound, new { Recipe = GlobalConstants.NOT_FOUND }); } /* recipe.Title = request.Title; * recipe.Summary = request.Summary; * recipe.Directions = request.Directions; * recipe.Ingredients = request.Ingredients; * recipe.NutritionInfo = request.NutritionInfo; */ _mapper.Map(request, recipe); if (await repository.SaveAll()) { return(Ok()); } throw new Exception(GlobalConstants.ERROR_SAVING_CHANGES); }
public IActionResult Update([FromBody] UpdateRecipeRequest request, [FromHeader] User user) { recipeService.UpdateRecipes(request); return(ActionUtils.Success("Recipe has been updated")); }
public void UpdateRecipes(UpdateRecipeRequest request) { if (request == null) { throw new ActionFailedException("invalid request body"); } if (request.Id == 0) { throw new ActionFailedException("id cannot be empty"); } if (string.IsNullOrWhiteSpace(request.Title)) { throw new ActionFailedException("title cannot be empty"); } if (string.IsNullOrWhiteSpace(request.Text)) { throw new ActionFailedException("title cannot be empty"); } request.Title = request.Title.Trim(); request.Text = request.Text.Trim(); if (request.Title.Length < 8) { throw new ActionFailedException("title is too short"); } if (request.Text.Length < 20) { throw new ActionFailedException("recipe details is too short"); } if (request.Portion <= 0) { throw new ActionFailedException("portion cannot be smaller than 1"); } if (request.Portion > 20) { throw new ActionFailedException("portion is too large"); } if (request.PrepareTime <= 0) { throw new ActionFailedException("prepare time cannot be smaller than 1"); } if (request.PrepareTime > 600) { throw new ActionFailedException("prepare time is too large"); } if (request.Ingredients == null) { throw new ActionFailedException("ingredients cannot be empty"); } if (request.Steps == null) { throw new ActionFailedException("steps cannot be empty"); } foreach (var item in request.Ingredients) { if (string.IsNullOrWhiteSpace(item)) { throw new ActionFailedException("ingredient items cannot be empty"); } } foreach (var item in request.Steps) { if (string.IsNullOrWhiteSpace(item)) { throw new ActionFailedException("step items cannot be empty"); } } Recipe recipe = recipeContext.Recipes.Include(x => x.RecipeTags).FirstOrDefault(x => x.Id == request.Id); if (recipe == null) { throw new ActionFailedException("invalid recipe"); } if (request.Tags == null) { request.Tags = new string[0]; } recipe.RecipeTags.Clear(); recipe.Title = request.Title; recipe.Text = request.Text; recipe.Ingredients = JsonConvert.SerializeObject(request.Ingredients); recipe.Steps = JsonConvert.SerializeObject(request.Steps); recipe.ModifiedTime = DateTime.Now; recipe.Portion = request.Portion; recipe.PrepareTime = request.PrepareTime; if (request.Photos != null) { recipe.Photos = JsonConvert.SerializeObject(request.Photos); } recipe.RecipeTags = new List <RecipeTag>(); foreach (var item in request.Tags) { if (string.IsNullOrWhiteSpace(item)) { throw new ActionFailedException("tags cannot be empty"); } var tagText = item.ToLower(); Tag tag = recipeContext.Tags.FirstOrDefault(x => x.Text == tagText); if (tag == null) { tag = new Tag(); tag.Text = tagText; recipeContext.Tags.Add(tag); } recipe.RecipeTags.Add(new RecipeTag { Recipe = recipe, Tag = tag }); } //TO-DO Recipe.User //TO-DO Photo ve Tag için add remove endpoint recipeContext.SaveChanges(); }
public async Task <ActionResult <RecipeResponse> > UpdateRecipeAsync(UpdateRecipeRequest request, [FromServices] Command <UpdateRecipeRequest, RecipeResponse> command) => await command.ExecuteAsync(request);