feature: create clean architecture package structure
This commit is contained in:
parent
7d49f3ea0b
commit
8398c5ada8
12
src/main/java/tk/antoine/roux/domain/UseCases.java
Normal file
12
src/main/java/tk/antoine/roux/domain/UseCases.java
Normal 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);
|
||||||
|
|
||||||
|
}
|
36
src/main/java/tk/antoine/roux/domain/model/Either.java
Normal file
36
src/main/java/tk/antoine/roux/domain/model/Either.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
27
src/main/java/tk/antoine/roux/infrastructure/Manager.java
Normal file
27
src/main/java/tk/antoine/roux/infrastructure/Manager.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user