public IActionResult Patch(string id, TaskPatchDto item) { if (string.IsNullOrEmpty(id)) { return(BadRequest()); } var task = _dbContext.TaskItems.Find(id); if (task == null) { return(NotFound()); } if (item.ChangedProperties.Contains(nameof(item.Id))) { task.Id = item.Id; } if (item.ChangedProperties.Contains(nameof(item.Title))) { task.Title = item.Title; } if (item.ChangedProperties.Contains(nameof(item.IsComplete))) { task.IsComplete = item.IsComplete; } _dbContext.TaskItems.Update(task); _dbContext.SaveChanges(); return(NoContent()); }
public async Task AspNetCoreApiSampleTaskResourcePatch_WhenModifyOneProperty_ReturnsExpectedObject() { //Arrange var created = await _restRunner.CreateResourceAsync <TaskPatchDto>(); dynamic patch1 = new { IsComplete = true }; dynamic patch2 = new { Title = (null as string) }; var expected1 = new TaskPatchDto() { Id = created.Id, Title = created.Title, IsComplete = true }; var expected2 = new TaskPatchDto() { Id = created.Id, Title = null, IsComplete = true }; //Act/Assert await _restRunner.PatchResourceAsync(created.Id, patch1); await _restRunner.GetResourceByIdAsync(created.Id, expected1); await _restRunner.PatchResourceAsync(created.Id, patch2); await _restRunner.GetResourceByIdAsync(created.Id, expected2); await DeleteAsync($"/{ResourceEndpoint}/{created.Id}"); }
public void PatchTask(int id, [FromBody] TaskPatchDto patch) { if (patch.newStatus != null) { var task = _serverContext.Tasks.First(t => t.Id == id); task.Status = patch.newStatus.Value; _serverContext.SaveChanges(); } }