diff --git a/hexagonal-architecure/my-poc-hexagonal/pom.xml b/hexagonal-architecure/my-poc-hexagonal/pom.xml index 8321ab6..2556cc3 100644 --- a/hexagonal-architecure/my-poc-hexagonal/pom.xml +++ b/hexagonal-architecure/my-poc-hexagonal/pom.xml @@ -27,6 +27,13 @@ h2 runtime + + + org.projectlombok + lombok + 1.18.20 + provided + org.springframework.boot spring-boot-starter-test diff --git a/hexagonal-architecure/my-poc-hexagonal/src/main/java/com/example/hexagonal/hexademo/domain/Student.java b/hexagonal-architecure/my-poc-hexagonal/src/main/java/com/example/hexagonal/hexademo/domain/Student.java new file mode 100644 index 0000000..6fba5e4 --- /dev/null +++ b/hexagonal-architecure/my-poc-hexagonal/src/main/java/com/example/hexagonal/hexademo/domain/Student.java @@ -0,0 +1,72 @@ +package com.example.hexagonal.hexademo.domain; + +import java.util.UUID; + +public class Student { + + private final UUID id; + private final String firstName; + private final String lastName; + private final Long age; + + private Student(Builder builder) { + id = builder.id; + firstName = builder.firstName; + lastName = builder.lastName; + age = builder.age; + } + + public UUID getId() { + return id; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public Long getAge() { + return age; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private UUID id; + private String firstName; + private String lastName; + private Long age; + + Builder() { + } + + public Builder id(UUID id) { + this.id = id; + return this; + } + + public Builder firstName(String firstName) { + this.firstName = firstName; + return this; + } + + public Builder lastName(String lastName) { + this.lastName = lastName; + return this; + } + + public Builder age(Long age) { + this.age = age; + return this; + } + + public Student build() { + return new Student(this); + } + } +} diff --git a/hexagonal-architecure/my-poc-hexagonal/src/main/java/com/example/hexagonal/hexademo/domain/usecases/StudentUseCase.java b/hexagonal-architecure/my-poc-hexagonal/src/main/java/com/example/hexagonal/hexademo/domain/usecases/StudentUseCase.java new file mode 100644 index 0000000..b7fb21a --- /dev/null +++ b/hexagonal-architecure/my-poc-hexagonal/src/main/java/com/example/hexagonal/hexademo/domain/usecases/StudentUseCase.java @@ -0,0 +1,10 @@ +package com.example.hexagonal.hexademo.domain.usecases; + +import com.example.hexagonal.hexademo.domain.Student; + +import java.util.List; + +public interface StudentUseCase { + + public List getAllStudents(); +} diff --git a/hexagonal-architecure/my-poc-hexagonal/src/main/java/com/example/hexagonal/hexademo/infrastracture/primary/StudentController.java b/hexagonal-architecure/my-poc-hexagonal/src/main/java/com/example/hexagonal/hexademo/infrastracture/primary/StudentController.java new file mode 100644 index 0000000..bad6111 --- /dev/null +++ b/hexagonal-architecure/my-poc-hexagonal/src/main/java/com/example/hexagonal/hexademo/infrastracture/primary/StudentController.java @@ -0,0 +1,26 @@ +package com.example.hexagonal.hexademo.infrastracture.primary; + +import com.example.hexagonal.hexademo.domain.Student; +import com.example.hexagonal.hexademo.domain.usecases.StudentUseCase; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/api/students") +public class StudentController { + + private final StudentUseCase studentUseCase; + + public StudentController(StudentUseCase studentUseCase) { + this.studentUseCase = studentUseCase; + } + + @RequestMapping("/get-all") + public ResponseEntity> getAll() { + List studentList = studentUseCase.getAllStudents(); + return ResponseEntity.ok().body(StudentDTO.from(studentList)); + } +} diff --git a/hexagonal-architecure/my-poc-hexagonal/src/main/java/com/example/hexagonal/hexademo/infrastracture/primary/StudentDTO.java b/hexagonal-architecure/my-poc-hexagonal/src/main/java/com/example/hexagonal/hexademo/infrastracture/primary/StudentDTO.java new file mode 100644 index 0000000..1e1fbe7 --- /dev/null +++ b/hexagonal-architecure/my-poc-hexagonal/src/main/java/com/example/hexagonal/hexademo/infrastracture/primary/StudentDTO.java @@ -0,0 +1,57 @@ +package com.example.hexagonal.hexademo.infrastracture.primary; + +import com.example.hexagonal.hexademo.domain.Student; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; + +@Builder +@AllArgsConstructor +@Getter +@Setter +public class StudentDTO { + + private final UUID id; + private final String firstName; + private final String lastName; + private final Long age; + + public static Student to(StudentDTO studentDTO) { + return Student + .builder() + .id(studentDTO.getId()) + .firstName(studentDTO.getFirstName()) + .lastName(studentDTO.getLastName()) + .age(studentDTO.getAge()) + .build(); + } + + public static StudentDTO from(Student student) { + return StudentDTO + .builder() + .id(student.getId()) + .firstName(student.getFirstName()) + .lastName(student.getLastName()) + .age(student.getAge()) + .build(); + } + + public static List to(List studentDTOList) { + return studentDTOList + .stream() + .map(StudentDTO::to) + .collect(Collectors.toList()); + } + + public static List from(List studentDTOList) { + return studentDTOList + .stream() + .map(StudentDTO::from) + .collect(Collectors.toList()); + } +}