feature: create clean architecture package structure

This commit is contained in:
RouxAntoine 2024-09-29 19:50:04 +02:00
parent 7d49f3ea0b
commit 8398c5ada8
Signed by: antoine
GPG Key ID: 098FB66FC0475E70
7 changed files with 75 additions and 0 deletions

View File

@ -0,0 +1,12 @@
package tk.antoine.roux.domain;
public interface UseCases<R, T> {
/**
* Every use cases should have one and only one method named execute
* Every use cases should take one argument
* Every use cases should return a value
*/
R execute(T arg);
}

View File

@ -0,0 +1,36 @@
package tk.antoine.roux.domain.model;
public final class Either<T> {
private final Exception error;
private final T success;
private Either(Exception error, T success) {
this.error = error;
this.success = success;
}
public Exception left() {
return error;
}
public T right() {
return success;
}
public static <T> Either<T> right(T success) {
return new Either<>(null, success);
}
public static <T> Either<T> left(Exception error) {
return new Either<>(error, null);
}
public boolean isLeft() {
return success == null;
}
public boolean isRight() {
return error == null;
}
}

View File

@ -0,0 +1,27 @@
package tk.antoine.roux.infrastructure;
import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.http.ResponseEntity;
import tk.antoine.roux.domain.model.Either;
public interface Manager {
static <T> ResponseEntity<T> eitherToResponseEntity(Either<T> either, String errorMessage) {
ResponseEntity<T> response;
if (either.isLeft()) {
ProblemDetail problemDetail = exceptionToProblemDetail(either, errorMessage);
response = ResponseEntity.of(problemDetail).build();
} else {
response = ResponseEntity.ok(either.right());
}
return response;
}
private static ProblemDetail exceptionToProblemDetail(Either<?> either, String errorMessage) {
ProblemDetail problemDetail = ProblemDetail
.forStatusAndDetail(HttpStatus.BAD_REQUEST, either.left().getMessage());
problemDetail.setTitle(errorMessage);
return problemDetail;
}
}