-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementing hexagonal architecture for my own way
- Loading branch information
amir_choubani
committed
May 1, 2023
1 parent
3810f38
commit a7c7a1a
Showing
5 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...itecure/my-poc-hexagonal/src/main/java/com/example/hexagonal/hexademo/domain/Student.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...exagonal/src/main/java/com/example/hexagonal/hexademo/domain/usecases/StudentUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Student> getAllStudents(); | ||
} |
26 changes: 26 additions & 0 deletions
26
...rc/main/java/com/example/hexagonal/hexademo/infrastracture/primary/StudentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<List<StudentDTO>> getAll() { | ||
List<Student> studentList = studentUseCase.getAllStudents(); | ||
return ResponseEntity.ok().body(StudentDTO.from(studentList)); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...gonal/src/main/java/com/example/hexagonal/hexademo/infrastracture/primary/StudentDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Student> to(List<StudentDTO> studentDTOList) { | ||
return studentDTOList | ||
.stream() | ||
.map(StudentDTO::to) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public static List<StudentDTO> from(List<Student> studentDTOList) { | ||
return studentDTOList | ||
.stream() | ||
.map(StudentDTO::from) | ||
.collect(Collectors.toList()); | ||
} | ||
} |