File tree Expand file tree Collapse file tree 5 files changed +172
-0
lines changed
hexagonal-architecure/my-poc-hexagonal
src/main/java/com/example/hexagonal/hexademo Expand file tree Collapse file tree 5 files changed +172
-0
lines changed Original file line number Diff line number Diff line change 27
27
<artifactId >h2</artifactId >
28
28
<scope >runtime</scope >
29
29
</dependency >
30
+
31
+ <dependency >
32
+ <groupId >org.projectlombok</groupId >
33
+ <artifactId >lombok</artifactId >
34
+ <version >1.18.20</version >
35
+ <scope >provided</scope >
36
+ </dependency >
30
37
<dependency >
31
38
<groupId >org.springframework.boot</groupId >
32
39
<artifactId >spring-boot-starter-test</artifactId >
Original file line number Diff line number Diff line change
1
+ package com .example .hexagonal .hexademo .domain ;
2
+
3
+ import java .util .UUID ;
4
+
5
+ public class Student {
6
+
7
+ private final UUID id ;
8
+ private final String firstName ;
9
+ private final String lastName ;
10
+ private final Long age ;
11
+
12
+ private Student (Builder builder ) {
13
+ id = builder .id ;
14
+ firstName = builder .firstName ;
15
+ lastName = builder .lastName ;
16
+ age = builder .age ;
17
+ }
18
+
19
+ public UUID getId () {
20
+ return id ;
21
+ }
22
+
23
+ public String getFirstName () {
24
+ return firstName ;
25
+ }
26
+
27
+ public String getLastName () {
28
+ return lastName ;
29
+ }
30
+
31
+ public Long getAge () {
32
+ return age ;
33
+ }
34
+
35
+ public static Builder builder () {
36
+ return new Builder ();
37
+ }
38
+
39
+ public static class Builder {
40
+ private UUID id ;
41
+ private String firstName ;
42
+ private String lastName ;
43
+ private Long age ;
44
+
45
+ Builder () {
46
+ }
47
+
48
+ public Builder id (UUID id ) {
49
+ this .id = id ;
50
+ return this ;
51
+ }
52
+
53
+ public Builder firstName (String firstName ) {
54
+ this .firstName = firstName ;
55
+ return this ;
56
+ }
57
+
58
+ public Builder lastName (String lastName ) {
59
+ this .lastName = lastName ;
60
+ return this ;
61
+ }
62
+
63
+ public Builder age (Long age ) {
64
+ this .age = age ;
65
+ return this ;
66
+ }
67
+
68
+ public Student build () {
69
+ return new Student (this );
70
+ }
71
+ }
72
+ }
Original file line number Diff line number Diff line change
1
+ package com .example .hexagonal .hexademo .domain .usecases ;
2
+
3
+ import com .example .hexagonal .hexademo .domain .Student ;
4
+
5
+ import java .util .List ;
6
+
7
+ public interface StudentUseCase {
8
+
9
+ public List <Student > getAllStudents ();
10
+ }
Original file line number Diff line number Diff line change
1
+ package com .example .hexagonal .hexademo .infrastracture .primary ;
2
+
3
+ import com .example .hexagonal .hexademo .domain .Student ;
4
+ import com .example .hexagonal .hexademo .domain .usecases .StudentUseCase ;
5
+ import org .springframework .http .ResponseEntity ;
6
+ import org .springframework .web .bind .annotation .RequestMapping ;
7
+ import org .springframework .web .bind .annotation .RestController ;
8
+
9
+ import java .util .List ;
10
+
11
+ @ RestController
12
+ @ RequestMapping ("/api/students" )
13
+ public class StudentController {
14
+
15
+ private final StudentUseCase studentUseCase ;
16
+
17
+ public StudentController (StudentUseCase studentUseCase ) {
18
+ this .studentUseCase = studentUseCase ;
19
+ }
20
+
21
+ @ RequestMapping ("/get-all" )
22
+ public ResponseEntity <List <StudentDTO >> getAll () {
23
+ List <Student > studentList = studentUseCase .getAllStudents ();
24
+ return ResponseEntity .ok ().body (StudentDTO .from (studentList ));
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+ package com .example .hexagonal .hexademo .infrastracture .primary ;
2
+
3
+ import com .example .hexagonal .hexademo .domain .Student ;
4
+ import lombok .AllArgsConstructor ;
5
+ import lombok .Builder ;
6
+ import lombok .Getter ;
7
+ import lombok .Setter ;
8
+
9
+ import java .util .List ;
10
+ import java .util .UUID ;
11
+ import java .util .stream .Collectors ;
12
+
13
+ @ Builder
14
+ @ AllArgsConstructor
15
+ @ Getter
16
+ @ Setter
17
+ public class StudentDTO {
18
+
19
+ private final UUID id ;
20
+ private final String firstName ;
21
+ private final String lastName ;
22
+ private final Long age ;
23
+
24
+ public static Student to (StudentDTO studentDTO ) {
25
+ return Student
26
+ .builder ()
27
+ .id (studentDTO .getId ())
28
+ .firstName (studentDTO .getFirstName ())
29
+ .lastName (studentDTO .getLastName ())
30
+ .age (studentDTO .getAge ())
31
+ .build ();
32
+ }
33
+
34
+ public static StudentDTO from (Student student ) {
35
+ return StudentDTO
36
+ .builder ()
37
+ .id (student .getId ())
38
+ .firstName (student .getFirstName ())
39
+ .lastName (student .getLastName ())
40
+ .age (student .getAge ())
41
+ .build ();
42
+ }
43
+
44
+ public static List <Student > to (List <StudentDTO > studentDTOList ) {
45
+ return studentDTOList
46
+ .stream ()
47
+ .map (StudentDTO ::to )
48
+ .collect (Collectors .toList ());
49
+ }
50
+
51
+ public static List <StudentDTO > from (List <Student > studentDTOList ) {
52
+ return studentDTOList
53
+ .stream ()
54
+ .map (StudentDTO ::from )
55
+ .collect (Collectors .toList ());
56
+ }
57
+ }
You can’t perform that action at this time.
0 commit comments