public async Task ReturnNoContentResponseAfterEntityUpdated() { var courseToUpdate = new CourseForUpdateViewModel() { Name = "Philosophy", TeacherName = "Baruch Spinoza", Location = "Building 5 Room 550", Students = new List <CourseStudentForUpdateViewModel>() { new CourseStudentForUpdateViewModel() { StudentId = 3, GPA = 2.5 }, new CourseStudentForUpdateViewModel() { StudentId = 4, GPA = 0 } }, }; var response = await Client.PutAsJsonAsync <CourseForUpdateViewModel>("/api/course/3", courseToUpdate); response.EnsureSuccessStatusCode(); Assert.Equal(HttpStatusCode.NoContent, response.StatusCode); }
private List <CourseStudent> FillCourseStudentForUpdate(CourseForUpdateViewModel coVM, Course course) { var newItems = coVM.Students.GroupBy(x => x.StudentId).Select(x => x.First()).Select(x => new CourseStudent() { CourseId = course.Id, StudentId = x.StudentId.Value, }).ToList(); var currentItems = course.CourseStudents; return(TryUpdateManyToMany(course.CourseStudents.ToList(), currentItems, newItems, x => x.StudentId)); }
public async Task UpdateCourse() { var options = new DbContextOptionsBuilder <UniversityContext>() .UseInMemoryDatabase(databaseName: "Update_to_controller") .Options; using (var context = new UniversityContext(options)) { List <Course> courses = new List <Course>() { new Course { Name = "English", TeacherName = "William Shakespeare", Location = "Building 3 Room 301" }, new Course { Name = "Psychology", TeacherName = "Sigmund Freud", Location = "Building 4 Room 401" }, new Course { Name = "Philosophy", TeacherName = "Friedrich Nietzsche", Location = "Building 5 Room 501" } }; await context.Courses.AddRangeAsync(courses); await context.SaveChangesAsync(); } using (var context = new UniversityContext(options)) { var service = new CourseService(context); var courseViewModelForCreate = new CourseForUpdateViewModel() { Name = "English", TeacherName = "Jacques Lacan", Location = "Building 3 Room 301" }; var ctrl = new CourseController(_mapper, service); var result = await ctrl.Put(context.Courses.First().Id, courseViewModelForCreate); var okResult = Assert.IsType <NoContentResult>(result); Assert.Equal(204, okResult.StatusCode); } }
public async Task ReturnNoContentResponseWhenCourseUpdated() { var newCourseviewModelForCreate = new CourseForUpdateViewModel { TeacherName = "Jacques Marie Émile Lacan" }; _mapper.Setup(m => m.Map <Course>(It.IsAny <CourseForCreateViewModel>())) .Returns(courses.Find(x => x.Id == 2)); var courseCtrl = new CourseController(_mapper.Object, _courseService.Object); var actual = await courseCtrl.Put(2, newCourseviewModelForCreate); var okResult = Assert.IsType <NoContentResult>(actual); }
public async Task <ActionResult> Put(int id, [FromBody] CourseForUpdateViewModel courseViewModelForUpdate) { if (courseViewModelForUpdate == null) { return(BadRequest("Request body is null.")); } var course = await _courseService.GetByIdAsync(id); if (course == null) { return(NotFound("Entity does not exist.")); } _mapper.Map(courseViewModelForUpdate, course); foreach (var item in course.CourseStudents) { item.GPA = courseViewModelForUpdate.Students.FirstOrDefault(x => x.StudentId == item.StudentId).GPA; } if (await _courseService.UpdateAsync(course) > 0) { return(NoContent()); } return(StatusCode(500)); }
public async Task ReturnNotFoundResponseIfEntityDoesNotExistUpdateMethod() { var options = new DbContextOptionsBuilder <UniversityContext>() .UseInMemoryDatabase(databaseName: "Update-exception_controller") .Options; using (var context = new UniversityContext(options)) { var courseViewModelForCreate = new CourseForUpdateViewModel() { Name = "English", TeacherName = "Jacques Lacan", Location = "Building 3 Room 301" }; var service = new CourseService(context); var ctrl = new CourseController(_mapper, service); var actual = await ctrl.Put(1, courseViewModelForCreate); var notFoundObjectResult = Assert.IsType <NotFoundObjectResult>(actual); Assert.Equal(404, notFoundObjectResult.StatusCode); Assert.Equal("Entity does not exist.", notFoundObjectResult.Value); } }