diff --git a/src/main/java/tk/antoine/roux/domain/UseCases.java b/src/main/java/tk/antoine/roux/domain/UseCases.java new file mode 100644 index 0000000..faa506c --- /dev/null +++ b/src/main/java/tk/antoine/roux/domain/UseCases.java @@ -0,0 +1,12 @@ +package tk.antoine.roux.domain; + +public interface UseCases { + + /** + * 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); + +} diff --git a/src/main/java/tk/antoine/roux/domain/model/Either.java b/src/main/java/tk/antoine/roux/domain/model/Either.java new file mode 100644 index 0000000..20d1272 --- /dev/null +++ b/src/main/java/tk/antoine/roux/domain/model/Either.java @@ -0,0 +1,36 @@ +package tk.antoine.roux.domain.model; + +public final class Either { + 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 Either right(T success) { + return new Either<>(null, success); + } + + public static Either left(Exception error) { + return new Either<>(error, null); + } + + public boolean isLeft() { + return success == null; + } + + public boolean isRight() { + return error == null; + } + +} diff --git a/src/main/java/tk/antoine/roux/domain/model/exception/.gitkeep b/src/main/java/tk/antoine/roux/domain/model/exception/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/tk/antoine/roux/domain/usecases/.gitkeep b/src/main/java/tk/antoine/roux/domain/usecases/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/tk/antoine/roux/infrastructure/Manager.java b/src/main/java/tk/antoine/roux/infrastructure/Manager.java new file mode 100644 index 0000000..10e74c5 --- /dev/null +++ b/src/main/java/tk/antoine/roux/infrastructure/Manager.java @@ -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 ResponseEntity eitherToResponseEntity(Either either, String errorMessage) { + ResponseEntity 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; + } +} diff --git a/src/main/java/tk/antoine/roux/infrastructure/in/.gitkeep b/src/main/java/tk/antoine/roux/infrastructure/in/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/tk/antoine/roux/infrastructure/out/.gitkeep b/src/main/java/tk/antoine/roux/infrastructure/out/.gitkeep new file mode 100644 index 0000000..e69de29