Skip to content

Commit

Permalink
feat: StudentRequestDTO & StudentService refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
HunorTotBagi committed Aug 6, 2024
1 parent 2d7cd23 commit d81235a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,8 +30,8 @@ public class StudentController {
protected StudentService studentService;

@PostMapping
public ResponseEntity<StudentEntity> createStudent(@Valid @RequestBody StudentDTO studentDTOBody) {
return new ResponseEntity<>(studentService.createStudent(studentDTOBody), HttpStatus.OK);
public ResponseEntity<StudentEntity> createStudent(@Valid @RequestBody StudentRequestDTO studentRequestDTO) {
return new ResponseEntity<>(studentService.createStudent(studentRequestDTO), HttpStatus.OK);
}

@GetMapping("/student")
Expand All @@ -47,18 +47,17 @@ public ResponseEntity<Iterable<StudentEntity>> getAllStudentsForParent() {
}

@GetMapping("/{id}")
public ResponseEntity<StudentEntity> getStudentById(@PathVariable String id) {
return new ResponseEntity<>(studentService.getStudentById(id), HttpStatus.OK);
public ResponseEntity<StudentEntity> getStudentById(@PathVariable Integer studentId) {
return new ResponseEntity<>(studentService.getStudentById(studentId), HttpStatus.OK);
}

@PutMapping("/{id}")
public ResponseEntity<StudentEntity> updateStudent(@PathVariable String id,
@RequestBody StudentDTO studentDTOBody) {
return new ResponseEntity<>(studentService.updateStudent(id, studentDTOBody), HttpStatus.OK);
public ResponseEntity<StudentEntity> updateStudent(@PathVariable Integer studentId, @RequestBody StudentRequestDTO studentRequestDTO) {
return new ResponseEntity<>(studentService.updateStudent(studentId, studentRequestDTO), HttpStatus.OK);
}

@DeleteMapping("/{id}")
public ResponseEntity<StudentEntity> deleteStudent(@PathVariable String id) {
return new ResponseEntity<>(studentService.deleteStudent(id), HttpStatus.OK);
public ResponseEntity<StudentEntity> deleteStudent(@PathVariable Integer studentId) {
return new ResponseEntity<>(studentService.deleteStudent(studentId), HttpStatus.OK);
}
}
10 changes: 5 additions & 5 deletions src/main/java/com/electric_diary/services/StudentService.java
Original file line number Diff line number Diff line change
@@ -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<StudentEntity> 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);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -67,61 +58,44 @@ public Iterable<StudentEntity> 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<StudentEntity> 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<StudentEntity> 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));
}
}

0 comments on commit d81235a

Please sign in to comment.