コード例 #1
0
        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());
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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));
        }
コード例 #6
0
        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));
        }