diff --git a/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/lecture/LectureController.java b/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/lecture/LectureController.java index d0b58998f53fb867b317cdb3214829e24b8ede4d..80b4f8e154f831ff52fc87745e0596c9f57b8c66 100644 --- a/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/lecture/LectureController.java +++ b/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/lecture/LectureController.java @@ -2,6 +2,8 @@ package org.fuseri.modulelanguageschool.lecture; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; import jakarta.validation.Valid; import org.fuseri.model.dto.lecture.LectureCreateDto; import org.fuseri.model.dto.lecture.LectureDto; @@ -38,6 +40,10 @@ public class LectureController { */ @ApiOperation(value = "Create a new lecture") @PostMapping + @ApiResponses(value = { + @ApiResponse(code = 201, message = "The lecture has been successfully created"), + @ApiResponse(code = 400, message = "The request body is invalid") + }) public ResponseEntity<LectureDto> create(@Valid @RequestBody LectureCreateDto lecture) { LectureDto lectureDto = lectureFacade.create(lecture); return ResponseEntity.status(HttpStatus.CREATED).body(lectureDto); @@ -51,6 +57,10 @@ public class LectureController { */ @ApiOperation(value = "Retrieve a lecture by its ID") @GetMapping("find/{courseId}") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "The lecture has been found"), + @ApiResponse(code = 404, message = "The lecture with the specified ID does not exist") + }) public ResponseEntity<LectureDto> find(@PathVariable Long courseId) { LectureDto lectureDto = lectureFacade.findById(courseId); return ResponseEntity.ok(lectureDto); @@ -64,6 +74,10 @@ public class LectureController { */ @ApiOperation(value = "Retrieve a list of lectures for the corresponding course") @GetMapping("/findByCourse") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "The list of lectures has been found"), + @ApiResponse(code = 404, message = "The course with the specified ID does not exist") + }) public ResponseEntity<List<LectureDto>> findByCourse(@Valid @RequestParam Long courseId) { return ResponseEntity.ok(lectureFacade.findAll(courseId)); } @@ -76,6 +90,11 @@ public class LectureController { */ @ApiOperation(value = "Update an existing lecture") @PutMapping("/update/{id}") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "The lecture has been successfully updated"), + @ApiResponse(code = 400, message = "The request body is invalid"), + @ApiResponse(code = 404, message = "The lecture with the specified ID does not exist") + }) public ResponseEntity<LectureDto> update(@PathVariable Long id, @Valid @RequestBody LectureCreateDto lecture) { return ResponseEntity.ok(lectureFacade.update(id, lecture)); } @@ -87,6 +106,10 @@ public class LectureController { */ @ApiOperation(value = "Delete a lecture by its ID") @DeleteMapping("/delete/{id}") + @ApiResponses(value = { + @ApiResponse(code = 204, message = "The lecture has been successfully deleted"), + @ApiResponse(code = 404, message = "The lecture with the specified ID does not exist") + }) public ResponseEntity<Void> delete(@PathVariable Long id) { lectureFacade.delete(id); return ResponseEntity.noContent().build(); @@ -102,6 +125,11 @@ public class LectureController { */ @ApiOperation(value = "Add lecturer to the existing lecture") @PatchMapping("/setLecturer/{id}") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "The lecture has been successfully updated"), + @ApiResponse(code = 400, message = "The request body is invalid"), + @ApiResponse(code = 404, message = "The lecture with the specified ID does not exist") + }) public ResponseEntity<LectureDto> setLecturer(@PathVariable Long id, @RequestBody UserDto lecturerDto) { return ResponseEntity.ok(lectureFacade.setLecturer(id, lecturerDto)); } @@ -115,6 +143,11 @@ public class LectureController { */ @ApiOperation(value = "Add student to the existing lecture") @PatchMapping("/enrol/{id}") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "The lecture has been successfully updated"), + @ApiResponse(code = 400, message = "The request body is invalid"), + @ApiResponse(code = 404, message = "The lecture with the specified ID does not exist") + }) public ResponseEntity<LectureDto> enrol(@PathVariable Long id, @RequestBody UserDto student) { return ResponseEntity.ok(lectureFacade.enrol(id, student)); } @@ -128,6 +161,11 @@ public class LectureController { */ @ApiOperation(value = "Remove student from the existing lecture") @PatchMapping("/expel/{id}") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "The lecture has been successfully updated"), + @ApiResponse(code = 400, message = "The request body is invalid"), + @ApiResponse(code = 404, message = "The lecture with the specified ID does not exist") + }) public ResponseEntity<LectureDto> expel(@PathVariable Long id, @RequestBody UserDto student) { return ResponseEntity.ok(lectureFacade.expel(id, student)); } diff --git a/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/lecture/LectureService.java b/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/lecture/LectureService.java index 657438858c1f7727f64523b612bfe0e7eb4f88c9..a3d1ea7f96aaa79883689189331e88977aede3eb 100644 --- a/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/lecture/LectureService.java +++ b/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/lecture/LectureService.java @@ -1,12 +1,13 @@ package org.fuseri.modulelanguageschool.lecture; -import org.fuseri.modulelanguageschool.common.ResourceNotFoundException; import org.fuseri.modulelanguageschool.course.Language; import org.fuseri.modulelanguageschool.course.ProficiencyLevel; import org.fuseri.modulelanguageschool.user.User; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.server.ResponseStatusException; import java.util.List; import java.util.Optional; @@ -29,7 +30,8 @@ public class LectureService { @Transactional(readOnly = true) public Lecture findById(Long id) { return lectureRepository.findById(id) - .orElseThrow(() -> new ResourceNotFoundException("Lecture with id: " + id + " was not found.")); + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, + "Lecture with id: " + id + " was not found.")); } @Transactional(readOnly = true) @@ -50,7 +52,8 @@ public class LectureService { lecture.setStudents(newLecture.getStudents()); return lectureRepository.save(lecture); } else { - throw new ResourceNotFoundException("Lecture with id: " + id + " was not found."); + throw new ResponseStatusException(HttpStatus.NOT_FOUND, + "Lecture with id: " + id + " was not found."); } } @@ -59,14 +62,17 @@ public class LectureService { lectureRepository.deleteById(id); } + @Transactional public List<Lecture> findAll(Language language) { return lectureRepository.findAllByLang(language); } + @Transactional public List<Lecture> findAll(Language language, ProficiencyLevel proficiencyLevel) { return lectureRepository.findAllByLangProf(language, proficiencyLevel); } + @Transactional public Lecture enrol(Long id, User student) { Optional<Lecture> optionalLecture = lectureRepository.findById(id); if (optionalLecture.isPresent()) { @@ -74,10 +80,12 @@ public class LectureService { lecture.enrolStudent(student); return lectureRepository.save(lecture); } else { - throw new ResourceNotFoundException("Lecture with id: " + id + " was not found."); + throw new ResponseStatusException(HttpStatus.NOT_FOUND, + "Lecture with id: " + id + " was not found."); } } + @Transactional public Lecture expel(Long id, User student) { Optional<Lecture> optionalLecture = lectureRepository.findById(id); if (optionalLecture.isPresent()) { @@ -85,10 +93,12 @@ public class LectureService { lecture.expelStudent(student); return lectureRepository.save(lecture); } else { - throw new ResourceNotFoundException("Lecture with id: " + id + " was not found."); + throw new ResponseStatusException(HttpStatus.NOT_FOUND, + "Lecture with id: " + id + " was not found."); } } + @Transactional public Lecture setLecturer(Long id, User lecturer) { Optional<Lecture> optionalLecture = lectureRepository.findById(id); if (optionalLecture.isPresent()) { @@ -96,7 +106,8 @@ public class LectureService { lecture.setLecturer(lecturer); return lectureRepository.save(lecture); } else { - throw new ResourceNotFoundException("Lecture with id: " + id + " was not found."); + throw new ResponseStatusException(HttpStatus.NOT_FOUND, + "Lecture with id: " + id + " was not found."); } } } diff --git a/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/lecture/LectureServiceTest.java b/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/lecture/LectureServiceTest.java index a1114eeac448e889e097835a1427f10557c13f23..bbb681e5ccbef75466b32349f9c24fd6a648a52f 100644 --- a/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/lecture/LectureServiceTest.java +++ b/application/module-language-school/src/test/java/org/fuseri/modulelanguageschool/lecture/LectureServiceTest.java @@ -1,6 +1,5 @@ package org.fuseri.modulelanguageschool.lecture; -import org.fuseri.modulelanguageschool.common.ResourceNotFoundException; import org.fuseri.modulelanguageschool.course.Course; import org.fuseri.modulelanguageschool.course.Language; import org.fuseri.modulelanguageschool.course.ProficiencyLevel; @@ -12,6 +11,7 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.web.server.ResponseStatusException; import java.time.LocalDateTime; import java.util.*; @@ -68,7 +68,7 @@ final class LectureServiceTest { void notFoundById() { when(lectureRepository.findById(anyLong())).thenReturn(Optional.empty()); - Assertions.assertThrows(ResourceNotFoundException.class, () -> lectureService.findById(anyLong())); + Assertions.assertThrows(ResponseStatusException.class, () -> lectureService.findById(anyLong())); } @@ -100,7 +100,7 @@ final class LectureServiceTest { Long id = 1L; when(lectureRepository.findById(anyLong())).thenReturn(Optional.empty()); - Assertions.assertThrows(ResourceNotFoundException.class, () -> lectureService.update(id, lecture)); + Assertions.assertThrows(ResponseStatusException.class, () -> lectureService.update(id, lecture)); } @Test @@ -153,7 +153,7 @@ final class LectureServiceTest { Long id = 1L; when(lectureRepository.findById(anyLong())).thenReturn(Optional.empty()); - Assertions.assertThrows(ResourceNotFoundException.class, () -> lectureService.enrol(id, user)); + Assertions.assertThrows(ResponseStatusException.class, () -> lectureService.enrol(id, user)); } @Test @@ -174,6 +174,6 @@ final class LectureServiceTest { Long id = 1L; when(lectureRepository.findById(anyLong())).thenReturn(Optional.empty()); - Assertions.assertThrows(ResourceNotFoundException.class, () -> lectureService.expel(id, user)); + Assertions.assertThrows(ResponseStatusException.class, () -> lectureService.expel(id, user)); } }