// PUT: api/TodoItem/5 public IHttpActionResult Put(int id, [FromBody] TodoItemUpdateViewModel value) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } return(Ok(_service.Update(id, value))); }
public async Task <TodoItemGetViewModel> UpdateAsync(int id, TodoItemUpdateViewModel value) { var model = await _repository.GetAsync(x => x.Id.Equals(id)); if (value.Content != null) { model.Content = value.Content; } if (value.IsCompleted != null) { model.IsCompleted = value.IsCompleted.Value; } var item = _repository.UpdateAsync(model); var viewmodel = _mapper.Map <TodoItemGetViewModel>(item); return(viewmodel); }
public void Put_ShouldBeUpdated_WhenPuttingATodoItem() { var controller = new TodoItemController(); var todoItem = new TodoItemUpdateViewModel { Content = "Test Put", IsCompleted = true }; var actionResult = controller.Put(1, todoItem); var response = actionResult as OkNegotiatedContentResult <TodoItemGetViewModel>; Assert.IsNotNull(response); var newTodoItem = response.Content; Assert.AreEqual(newTodoItem.Id, 1); Assert.AreEqual(newTodoItem.Content, "Test Put"); Assert.AreEqual(newTodoItem.IsCompleted, true); }
public TodoItemGetViewModel Update(int id, TodoItemUpdateViewModel value) { var model = _repo.Get(x => x.Id.Equals(id)); if (value.Content != null) { model.Content = value.Content; } if (value.IsCompleted != null) { model.IsCompleted = value.IsCompleted.Value; } _repo.Update(model); return(new TodoItemGetViewModel { Id = model.Id, Content = model.Content, IsCompleted = model.IsCompleted, CreatedAt = model.CreatedAt }); }