Advertisement
Guest User

Untitled

a guest
Aug 5th, 2023
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.26 KB | None | 0 0
  1. # ENTITIES
  2.  
  3.  
  4. @Entity
  5. @Table(name = "course")
  6. @Getter
  7. @NoArgsConstructor
  8. public class Course {
  9.  
  10.     @Id
  11.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  12.     private Long id;
  13.  
  14.     @Column(nullable = false, name = "course_name")
  15.     private String courseName;
  16.  
  17.     @Column(nullable = false, name = "description")
  18.     private String description;
  19.  
  20.  
  21.     @OneToMany(mappedBy = "course", cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
  22.     @JsonBackReference
  23.     private Set<Student> courseStudents;
  24.  
  25.     public void addStudent(Student student){
  26.         if(courseStudents == null)
  27.             courseStudents = new HashSet<>();
  28.  
  29.         courseStudents.add(student);
  30.     }
  31.  
  32.     public Course(String courseName, String description) {
  33.         this.courseName = courseName;
  34.         this.description = description;
  35.     }
  36. }
  37.  
  38.  
  39. @Entity
  40. @Table(name = "student")
  41. @NoArgsConstructor
  42. @Getter
  43. @AllArgsConstructor
  44. @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  45. public class Student {
  46.     @Id
  47.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  48.     private Long id;
  49.  
  50.     @Column(nullable = false)
  51.     private String name;
  52.  
  53.     @Column(nullable = false)
  54.     private String surname;
  55.  
  56.     @NaturalId
  57.     private String identificationString;
  58.  
  59.     @ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.LAZY)
  60.     @JoinColumn(name = "course_id")
  61.     @JsonManagedReference
  62.     private Course course;
  63.  
  64.     public void setCourse(Course course) {
  65.         this.course = course;
  66.     }
  67.  
  68.     public Student(String name, String surname, String identificationString) {
  69.         this.name = name;
  70.         this.surname = surname;
  71.         this.identificationString = identificationString;
  72.     }
  73.  
  74.     public Student(String name, String surname, String identificationString, Course course) {
  75.         this.name = name;
  76.         this.surname = surname;
  77.         this.identificationString = identificationString;
  78.         this.course = course;
  79.     }
  80. }
  81.  
  82.  
  83. #DTOs
  84.  
  85. @Data
  86. @Getter
  87. @AllArgsConstructor
  88. public class GetStudentDTO {
  89.     private Long id;
  90.     private String name;
  91.     private String surname;
  92.     private String identificationString;
  93.     private Course course;
  94. }
  95.  
  96.  
  97. @Getter
  98. public class SaveStudentDTO {
  99.     @JsonProperty
  100.     private String name;
  101.  
  102.     @JsonProperty
  103.     private String surname;
  104.  
  105.     @JsonProperty
  106.     private String idString;
  107.  
  108.     @JsonProperty
  109.     private Long idCourse;
  110. }
  111.  
  112. #MAPPER FOR DTO
  113. @Component
  114. public class GetStudentMapper {
  115.  
  116.     public GetStudentDTO toDto(Student student) {
  117.         Long id = student.getId();
  118.         String name = student.getName();
  119.         String surname = student.getSurname();
  120.         String idString = student.getIdentificationString();
  121.         Course course = student.getCourse();
  122.         return new GetStudentDTO(id, name, surname, idString, course);
  123.     }
  124.  
  125.     public Student toStudent(GetStudentDTO studentDTO) {
  126.         return new Student(studentDTO.getId(), studentDTO.getName(), studentDTO.getSurname(), studentDTO.getIdentificationString(), studentDTO.getCourse());
  127.     }
  128. }
  129.  
  130. #STUDENT SERVICE
  131. @Service
  132. public class StudentService {
  133.     StudentRepository studentRepository;
  134.     CourseRepository courseRepository;
  135.  
  136.     @Autowired
  137.     public StudentService(StudentRepository studentRepository, CourseRepository courseRepository) {
  138.         this.studentRepository = studentRepository;
  139.         this.courseRepository = courseRepository;
  140.     }
  141.  
  142.     @Transactional
  143.     public Student save(Student student){
  144.         return studentRepository.save(student);
  145.     }
  146.  
  147.     @Transactional
  148.     public List<Student> findAll(){
  149.         return studentRepository.findAll();
  150.  
  151.     }
  152. }
  153.  
  154. #CONTROLLER
  155.  
  156. @RequestMapping(path = "/students")
  157. @RestController
  158. public class StudentController {
  159.  
  160.     private final StudentService studentService;
  161.     private final CourseService courseService;
  162.     private final GetStudentMapper studentMapper;
  163.  
  164.     @Autowired
  165.     public StudentController(StudentService studentService, CourseService courseService, GetStudentMapper studentMapper) {
  166.         this.studentService = studentService;
  167.         this.courseService = courseService;
  168.         this.studentMapper = studentMapper;
  169.     }
  170.  
  171.     @PostMapping(path = "/")
  172.     ResponseEntity<?> addStudent(@RequestBody SaveStudentDTO studentToAdd){
  173.  
  174.         Optional<Course> course = courseService.findById(studentToAdd.getIdCourse());
  175.         //check if course exists
  176.         if (course.isPresent()){
  177.             Student student = studentService.save(
  178.                 new Student(
  179.                     studentToAdd.getName(),
  180.                     studentToAdd.getSurname(),
  181.                     studentToAdd.getIdString(),
  182.                     course.get()
  183.                 )
  184.             );
  185.             course.get().addStudent(student);
  186.             return ResponseEntity.ofNullable(student);
  187.         }
  188.  
  189.         return (ResponseEntity<?>) ResponseEntity.badRequest();
  190.     }
  191.  
  192.     @GetMapping(path = "/")
  193.     ResponseEntity<?> getStudents(){
  194.         List<GetStudentDTO> students = studentService.findAll().stream().map(studentMapper::toDto).toList();
  195.         return ResponseEntity.ok(students);
  196.     }
  197. }
  198.  
  199.  
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement