public async Task <ActionResult> Put(int id, [FromBody] StudentForUpdateViewModel studentViewModelForUpdate) { if (studentViewModelForUpdate == null) { return(BadRequest("Request body is null.")); } var student = await _studentService.GetByIdAsync(id); if (student == null) { return(NotFound("Entity does not exist.")); } if (student.Name != studentViewModelForUpdate.Name) { if (await _studentService.FindStudentByName(studentViewModelForUpdate.Name)) { return(BadRequest("The student name is already taken.")); } } _mapper.Map(studentViewModelForUpdate, student); if (await _studentService.UpdateAsync(student) > 0) { return(NoContent()); } return(StatusCode(500)); }
public async Task ReturnNoContentResponseAfterEntityUpdated() { var studentToUpdate = new StudentForUpdateViewModel() { Name = "Scott Hanselman", Age = 22, GPA = 2.4 }; var response = await Client.PutAsJsonAsync <StudentForUpdateViewModel>("/api/Student/3", studentToUpdate); response.EnsureSuccessStatusCode(); Assert.Equal(HttpStatusCode.NoContent, response.StatusCode); }