From d81235a9bfb14634280369abb7fbb417caac013c Mon Sep 17 00:00:00 2001 From: Hunor Tot-Bagi Date: Tue, 6 Aug 2024 13:46:20 +0200 Subject: [PATCH] feat: StudentRequestDTO & StudentService refactor --- .../DTO/Request/StudentRequestDTO.java | 16 ++++ .../controllers/StudentController.java | 19 ++-- .../services/StudentService.java | 10 +- .../services/impl/StudentServiceImpl.java | 92 +++++++------------ 4 files changed, 63 insertions(+), 74 deletions(-) create mode 100644 src/main/java/com/electric_diary/DTO/Request/StudentRequestDTO.java diff --git a/src/main/java/com/electric_diary/DTO/Request/StudentRequestDTO.java b/src/main/java/com/electric_diary/DTO/Request/StudentRequestDTO.java new file mode 100644 index 0000000..b693291 --- /dev/null +++ b/src/main/java/com/electric_diary/DTO/Request/StudentRequestDTO.java @@ -0,0 +1,16 @@ +package com.electric_diary.DTO.Request; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +public class StudentRequestDTO { + private String firstName; + private String lastName; + private Integer classId; + private Integer parentId; + private Integer userId; +} diff --git a/src/main/java/com/electric_diary/controllers/StudentController.java b/src/main/java/com/electric_diary/controllers/StudentController.java index 3f53dbd..2993a40 100644 --- a/src/main/java/com/electric_diary/controllers/StudentController.java +++ b/src/main/java/com/electric_diary/controllers/StudentController.java @@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import com.electric_diary.DTO.StudentDTO; +import com.electric_diary.DTO.Request.StudentRequestDTO; import com.electric_diary.entities.StudentEntity; import com.electric_diary.security.Views; import com.electric_diary.services.StudentService; @@ -30,8 +30,8 @@ public class StudentController { protected StudentService studentService; @PostMapping - public ResponseEntity createStudent(@Valid @RequestBody StudentDTO studentDTOBody) { - return new ResponseEntity<>(studentService.createStudent(studentDTOBody), HttpStatus.OK); + public ResponseEntity createStudent(@Valid @RequestBody StudentRequestDTO studentRequestDTO) { + return new ResponseEntity<>(studentService.createStudent(studentRequestDTO), HttpStatus.OK); } @GetMapping("/student") @@ -47,18 +47,17 @@ public ResponseEntity> getAllStudentsForParent() { } @GetMapping("/{id}") - public ResponseEntity getStudentById(@PathVariable String id) { - return new ResponseEntity<>(studentService.getStudentById(id), HttpStatus.OK); + public ResponseEntity getStudentById(@PathVariable Integer studentId) { + return new ResponseEntity<>(studentService.getStudentById(studentId), HttpStatus.OK); } @PutMapping("/{id}") - public ResponseEntity updateStudent(@PathVariable String id, - @RequestBody StudentDTO studentDTOBody) { - return new ResponseEntity<>(studentService.updateStudent(id, studentDTOBody), HttpStatus.OK); + public ResponseEntity updateStudent(@PathVariable Integer studentId, @RequestBody StudentRequestDTO studentRequestDTO) { + return new ResponseEntity<>(studentService.updateStudent(studentId, studentRequestDTO), HttpStatus.OK); } @DeleteMapping("/{id}") - public ResponseEntity deleteStudent(@PathVariable String id) { - return new ResponseEntity<>(studentService.deleteStudent(id), HttpStatus.OK); + public ResponseEntity deleteStudent(@PathVariable Integer studentId) { + return new ResponseEntity<>(studentService.deleteStudent(studentId), HttpStatus.OK); } } diff --git a/src/main/java/com/electric_diary/services/StudentService.java b/src/main/java/com/electric_diary/services/StudentService.java index f413f38..ef17e39 100644 --- a/src/main/java/com/electric_diary/services/StudentService.java +++ b/src/main/java/com/electric_diary/services/StudentService.java @@ -1,16 +1,16 @@ package com.electric_diary.services; -import com.electric_diary.DTO.StudentDTO; +import com.electric_diary.DTO.Request.StudentRequestDTO; import com.electric_diary.entities.StudentEntity; public interface StudentService { - public StudentEntity createStudent(StudentDTO studentDTOBody); + public StudentEntity createStudent(StudentRequestDTO studentRequestDTO); public Iterable getAllStudents(); - public StudentEntity getStudentById(String id); + public StudentEntity getStudentById(Integer studentId); - public StudentEntity updateStudent(String id, StudentDTO studentDTOBody); + public StudentEntity updateStudent(Integer studentId, StudentRequestDTO studentRequestDTO); - public StudentEntity deleteStudent(String id); + public StudentEntity deleteStudent(Integer studentId); } diff --git a/src/main/java/com/electric_diary/services/impl/StudentServiceImpl.java b/src/main/java/com/electric_diary/services/impl/StudentServiceImpl.java index 53230fd..0cd61bd 100644 --- a/src/main/java/com/electric_diary/services/impl/StudentServiceImpl.java +++ b/src/main/java/com/electric_diary/services/impl/StudentServiceImpl.java @@ -1,10 +1,8 @@ package com.electric_diary.services.impl; -import java.util.Optional; - import org.springframework.stereotype.Service; -import com.electric_diary.DTO.StudentDTO; +import com.electric_diary.DTO.Request.StudentRequestDTO; import com.electric_diary.entities.ClassEntity; import com.electric_diary.entities.ParentEntity; import com.electric_diary.entities.StudentEntity; @@ -38,21 +36,14 @@ public StudentServiceImpl(final StudentRepository studentRepository, final Class } @Override - public StudentEntity createStudent(StudentDTO studentDTOBody) { - String classId = studentDTOBody.getClassId(); - String parentId = studentDTOBody.getParentId(); - String userId = studentDTOBody.getUserId(); - - ClassEntity newClass = classRepository.findById(Integer.parseInt(classId)) - .orElseThrow(() -> new NotFoundException("Class", classId)); - ParentEntity parent = parentRepository.findById(Integer.parseInt(parentId)) - .orElseThrow(() -> new NotFoundException("Parent", parentId)); - UserEntity user = userRepository.findById(Integer.parseInt(userId)) - .orElseThrow(() -> new NotFoundException("User", userId)); - + public StudentEntity createStudent(StudentRequestDTO studentRequestDTO) { + ClassEntity newClass = getClassById(studentRequestDTO.getClassId()); + ParentEntity parent = getParentById(studentRequestDTO.getParentId()); + UserEntity user = getUserById(studentRequestDTO.getUserId()); + StudentEntity student = new StudentEntity(); - student.setFirstName(studentDTOBody.getFirstName()); - student.setLastName(studentDTOBody.getLastName()); + student.setFirstName(studentRequestDTO.getFirstName()); + student.setLastName(studentRequestDTO.getLastName()); student.setNewClass(newClass); student.setParent(parent); student.setUser(user); @@ -67,61 +58,44 @@ public Iterable getAllStudents() { } @Override - public StudentEntity getStudentById(String id) { - try { - int studentId = Integer.parseInt(id); - StudentEntity student = studentRepository.findById(studentId) - .orElseThrow(() -> new NotFoundException("Student", id)); - return student; - } catch (NumberFormatException e) { - throw new NumberFormatException(); - } + public StudentEntity getStudentById(Integer studentId) { + return studentRepository.findById(studentId).orElseThrow(() -> new NotFoundException("Student", studentId)); } @Override - public StudentEntity updateStudent(String id, StudentDTO studentDTOBody) { - String classId = studentDTOBody.getClassId(); - String parentId = studentDTOBody.getParentId(); - String userId = studentDTOBody.getUserId(); - - ClassEntity newClass = classRepository.findById(Integer.parseInt(classId)) - .orElseThrow(() -> new NotFoundException("Class", classId)); - ParentEntity parent = parentRepository.findById(Integer.parseInt(parentId)) - .orElseThrow(() -> new NotFoundException("Parent", parentId)); - UserEntity user = userRepository.findById(Integer.parseInt(userId)) - .orElseThrow(() -> new NotFoundException("User", userId)); - - Optional optionalStudent = studentRepository.findById(Integer.parseInt(id)); - - if (!optionalStudent.isPresent()) - throw new NotFoundException("Student", id); - - StudentEntity student = optionalStudent.get(); - student.setFirstName(studentDTOBody.getFirstName()); - student.setLastName(studentDTOBody.getLastName()); + public StudentEntity updateStudent(Integer studentId, StudentRequestDTO studentRequestDTO) { + StudentEntity student = getStudentById(studentId); + + ClassEntity newClass = getClassById(studentRequestDTO.getClassId()); + ParentEntity parent = getParentById(studentRequestDTO.getParentId()); + UserEntity user = getUserById(studentRequestDTO.getUserId()); + + student.setFirstName(studentRequestDTO.getFirstName()); + student.setLastName(studentRequestDTO.getLastName()); student.setNewClass(newClass); student.setParent(parent); student.setUser(user); studentRepository.save(student); + return student; } @Override - public StudentEntity deleteStudent(String id) { - int studentId; - try { - studentId = Integer.parseInt(id); - } catch (NumberFormatException e) { - throw new NumberFormatException(); - } + public StudentEntity deleteStudent(Integer studentId) { + StudentEntity student = getStudentById(studentId); + studentRepository.delete(student); + return student; + } - Optional optionalStudent = studentRepository.findById(studentId); + private ClassEntity getClassById(Integer classId) { + return classRepository.findById(classId).orElseThrow(() -> new NotFoundException("User", classId)); + } - if (!optionalStudent.isPresent()) - throw new NotFoundException("Student", id); + public ParentEntity getParentById(Integer parentId) { + return parentRepository.findById(parentId).orElseThrow(() -> new NotFoundException("Parent", parentId)); + } - StudentEntity student = optionalStudent.get(); - studentRepository.delete(student); - return student; + private UserEntity getUserById(Integer userId) { + return userRepository.findById(userId).orElseThrow(() -> new NotFoundException("User", userId)); } }