From 5979f72f1e9720e857ed8b4daef4cf410738b3a0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Pokorn=C3=BD?= <xpokorn8@fi.muni.cz>
Date: Sun, 16 Apr 2023 00:25:25 +0200
Subject: [PATCH] adding proper LectureController apiResponses

---
 .../lecture/LectureController.java            | 38 +++++++++++++++++++
 .../lecture/LectureService.java               | 23 ++++++++---
 .../lecture/LectureServiceTest.java           | 10 ++---
 3 files changed, 60 insertions(+), 11 deletions(-)

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 d0b58998..80b4f8e1 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 65743885..a3d1ea7f 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 a1114eea..bbb681e5 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));
     }
 }
-- 
GitLab