public IActionResult UpdateStudent(string id, [FromBody] StudentForUpdateDTO student) { if (!ModelState.IsValid) { return(BadRequest(student)); } var selectedStudent = _unitOfWork.StudentRepository.GetStudentById(id); if (selectedStudent == null) { return(NotFound()); } Mapper.Map(student, selectedStudent); try { _unitOfWork.StudentRepository.UpdateStudent(selectedStudent); } catch (Exception) { throw new Exception($"Cannot Update Student ID:{id}"); } return(NoContent()); }
public async Task PartialUpdateShouldReturnNotFoundWhenIdNotExist() { var student = new StudentForUpdateDTO(); student.Name = "Update Name"; student.BirthDate = "22/12/2016"; var response = await _client.PatchAsJsonAsync("/api/students/5ssss", student); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); }
public void ShouldReturnNotFoundWhenUpdateStudentNotExist() { var controller = new StudentsController(_unitOfWork, _urlHelper); var student = new StudentForUpdateDTO(); student.Name = "UpdateName"; var sut = controller.UpdateStudent("kjh1", student); Assert.IsType <NotFoundResult>(sut); }
public async Task FullUpdateShouldReturnNoContentWhenSuccess() { var student = new StudentForUpdateDTO(); student.Name = "Update Name"; var response = await _client.PutAsJsonAsync("/api/students/5", student); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); //var returnedSession = await response.Content.ReadAsJsonAsync<List<object>>(); //Assert.True(_randomStudent.Single(x => x.Id == "5").Name.Equals("Update Name")); //Assert.Null(_randomStudent.Single(x => x.Id == "5").BirthDate); }
public void ShouldReturnBadRequestWhenUpdateModelStateError() { var controller = new StudentsController(_unitOfWork, _urlHelper); var student = new StudentForUpdateDTO(); student.Name = "UpdateName"; controller.ModelState.AddModelError("error", "some error"); var sut = controller.UpdateStudent("1", student); var badRequestResult = Assert.IsType <BadRequestObjectResult>(sut); var result = Assert.IsType <StudentForUpdateDTO>(badRequestResult.Value); Assert.True(string.IsNullOrWhiteSpace(result.BirthDate)); }
public void ShouldReturnNoContentWhenUpdateSuccess() { var controller = new StudentsController(_unitOfWork, _urlHelper); var student = new StudentForUpdateDTO(); student.Name = "UpdateName"; student.BirthDate = "22/01/2015"; var sut = controller.UpdateStudent("1", student); var updateSudent = _randomStudent.Single(x => x.Id == "1"); var mapUpdateStudentWithDto = Mapper.Map <StudentDTO>(updateSudent); Assert.IsType <NoContentResult>(sut); Assert.Equal(student.Name, mapUpdateStudentWithDto.Name); Assert.True(student.BirthDate.Equals(mapUpdateStudentWithDto.BirthDate)); }