public async Task UpdateAsync(int id, StepUpdateViewModel model) { try { if (id.Equals(default(int))) { throw new ArgumentNullException(nameof(id)); } if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!id.Equals(model.Id)) { throw new IdsNotIdenticalException(); } var existingElement = await this.Repository.FindAsync(id); var newObject = Mapper.Map(model, existingElement); var updateResult = await Repository.UpdateAsync(newObject); Logger.LogDebug(new EventId(), updateResult ? $"recipe step {id} successfully updated" : $"No need to update step {id}"); } catch (DbUpdateConcurrencyException e) { Logger.LogWarning(new EventId(), e, $"No recipe step with id {id} found"); throw new DataObjectNotFoundException(); } catch (Exception e) { Logger.LogError(new EventId(), e, $"An Error occured while updating a recipe step"); throw new Exception($"An Error occured while updating a recipe step"); } }
public async Task <IActionResult> UpdateStepAsync(int recipeId, int id, [FromBody] StepUpdateViewModel model) { if (await this._service.GetStepsForRecipe(recipeId).AllAsync(x => !x.Id.Equals(id))) { return(NotFound($"No step with the id {id} found for recipe {recipeId}")); } await _service.UpdateAsync(id, model); return(NoContent()); }
public IActionResult UpdateStep(StepUpdateViewModel step) { var dbStep = _db.Steps.Find(step.Id); if (step.Name != dbStep.Name) { dbStep.Name = step.Name; } if (step.Body != dbStep.Body) { dbStep.Body = step.Body; } dbStep.Date = DateTime.Now; dbStep.Article.Date = dbStep.Date; _db.SaveChanges(); return(View(dbStep)); }