Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ENTITIES
- @Entity
- @Table(name = "course")
- @Getter
- @NoArgsConstructor
- public class Course {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Long id;
- @Column(nullable = false, name = "course_name")
- private String courseName;
- @Column(nullable = false, name = "description")
- private String description;
- @OneToMany(mappedBy = "course", cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
- @JsonBackReference
- private Set<Student> courseStudents;
- public void addStudent(Student student){
- if(courseStudents == null)
- courseStudents = new HashSet<>();
- courseStudents.add(student);
- }
- public Course(String courseName, String description) {
- this.courseName = courseName;
- this.description = description;
- }
- }
- @Entity
- @Table(name = "student")
- @NoArgsConstructor
- @Getter
- @AllArgsConstructor
- @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
- public class Student {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Long id;
- @Column(nullable = false)
- private String name;
- @Column(nullable = false)
- private String surname;
- @NaturalId
- private String identificationString;
- @ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.LAZY)
- @JoinColumn(name = "course_id")
- @JsonManagedReference
- private Course course;
- public void setCourse(Course course) {
- this.course = course;
- }
- public Student(String name, String surname, String identificationString) {
- this.name = name;
- this.surname = surname;
- this.identificationString = identificationString;
- }
- public Student(String name, String surname, String identificationString, Course course) {
- this.name = name;
- this.surname = surname;
- this.identificationString = identificationString;
- this.course = course;
- }
- }
- #DTOs
- @Data
- @Getter
- @AllArgsConstructor
- public class GetStudentDTO {
- private Long id;
- private String name;
- private String surname;
- private String identificationString;
- private Course course;
- }
- @Getter
- public class SaveStudentDTO {
- @JsonProperty
- private String name;
- @JsonProperty
- private String surname;
- @JsonProperty
- private String idString;
- @JsonProperty
- private Long idCourse;
- }
- #MAPPER FOR DTO
- @Component
- public class GetStudentMapper {
- public GetStudentDTO toDto(Student student) {
- Long id = student.getId();
- String name = student.getName();
- String surname = student.getSurname();
- String idString = student.getIdentificationString();
- Course course = student.getCourse();
- return new GetStudentDTO(id, name, surname, idString, course);
- }
- public Student toStudent(GetStudentDTO studentDTO) {
- return new Student(studentDTO.getId(), studentDTO.getName(), studentDTO.getSurname(), studentDTO.getIdentificationString(), studentDTO.getCourse());
- }
- }
- #STUDENT SERVICE
- @Service
- public class StudentService {
- StudentRepository studentRepository;
- CourseRepository courseRepository;
- @Autowired
- public StudentService(StudentRepository studentRepository, CourseRepository courseRepository) {
- this.studentRepository = studentRepository;
- this.courseRepository = courseRepository;
- }
- @Transactional
- public Student save(Student student){
- return studentRepository.save(student);
- }
- @Transactional
- public List<Student> findAll(){
- return studentRepository.findAll();
- }
- }
- #CONTROLLER
- @RequestMapping(path = "/students")
- @RestController
- public class StudentController {
- private final StudentService studentService;
- private final CourseService courseService;
- private final GetStudentMapper studentMapper;
- @Autowired
- public StudentController(StudentService studentService, CourseService courseService, GetStudentMapper studentMapper) {
- this.studentService = studentService;
- this.courseService = courseService;
- this.studentMapper = studentMapper;
- }
- @PostMapping(path = "/")
- ResponseEntity<?> addStudent(@RequestBody SaveStudentDTO studentToAdd){
- Optional<Course> course = courseService.findById(studentToAdd.getIdCourse());
- //check if course exists
- if (course.isPresent()){
- Student student = studentService.save(
- new Student(
- studentToAdd.getName(),
- studentToAdd.getSurname(),
- studentToAdd.getIdString(),
- course.get()
- )
- );
- course.get().addStudent(student);
- return ResponseEntity.ofNullable(student);
- }
- return (ResponseEntity<?>) ResponseEntity.badRequest();
- }
- @GetMapping(path = "/")
- ResponseEntity<?> getStudents(){
- List<GetStudentDTO> students = studentService.findAll().stream().map(studentMapper::toDto).toList();
- return ResponseEntity.ok(students);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement