Skip to content

Commit

Permalink
implementing hexagonal architecture for my own way
Browse files Browse the repository at this point in the history
  • Loading branch information
amir_choubani committed May 1, 2023
1 parent 3810f38 commit a7c7a1a
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 0 deletions.
7 changes: 7 additions & 0 deletions hexagonal-architecure/my-poc-hexagonal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
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);
}
}
}
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();
}
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));
}
}
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());
}
}

0 comments on commit a7c7a1a

Please sign in to comment.