public async Task <IActionResult> UpdateRecipe([FromRoute] int id, [FromBody] CreateUpdateIngredientDto receivedIngredient, CancellationToken cancellation) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var ingredient = await db.Ingredients.FindAsync(id, cancellation); if (ingredient == null) { return(NotFound()); } cancellation.ThrowIfCancellationRequested(); mapper.Map(receivedIngredient, ingredient); await db.SaveChangesAsync(cancellation); return(NoContent()); } catch (OperationCanceledException) { logger.LogInformation($"{nameof(IngredientController)}::{nameof(UpdateRecipe)} got cancelled"); throw; } }
public async Task <IActionResult> AddIngredient([FromBody] CreateUpdateIngredientDto receivedIngredient, CancellationToken cancellation) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (await db.Ingredients.AnyAsync(r => r.Name == receivedIngredient.Name, cancellation)) { return(BadRequest("already exists")); } cancellation.ThrowIfCancellationRequested(); var ingredient = mapper.Map <Ingredient>(receivedIngredient); db.Ingredients.Add(ingredient); await db.SaveChangesAsync(cancellation); return(CreatedAtAction(nameof(GetById), new { id = ingredient.Id }, ingredient)); } catch (OperationCanceledException) { logger.LogInformation($"{nameof(IngredientController)}::{nameof(AddIngredient)} got cancelled"); throw; } }