From 8398c5ada89ad7032cdf0b8de55dfd0726d42307 Mon Sep 17 00:00:00 2001 From: RouxAntoine Date: Sun, 29 Sep 2024 19:50:04 +0200 Subject: [PATCH] feature: create clean architecture package structure --- .../java/tk/antoine/roux/domain/UseCases.java | 12 +++++++ .../tk/antoine/roux/domain/model/Either.java | 36 +++++++++++++++++++ .../roux/domain/model/exception/.gitkeep | 0 .../tk/antoine/roux/domain/usecases/.gitkeep | 0 .../antoine/roux/infrastructure/Manager.java | 27 ++++++++++++++ .../antoine/roux/infrastructure/in/.gitkeep | 0 .../antoine/roux/infrastructure/out/.gitkeep | 0 7 files changed, 75 insertions(+) create mode 100644 src/main/java/tk/antoine/roux/domain/UseCases.java create mode 100644 src/main/java/tk/antoine/roux/domain/model/Either.java create mode 100644 src/main/java/tk/antoine/roux/domain/model/exception/.gitkeep create mode 100644 src/main/java/tk/antoine/roux/domain/usecases/.gitkeep create mode 100644 src/main/java/tk/antoine/roux/infrastructure/Manager.java create mode 100644 src/main/java/tk/antoine/roux/infrastructure/in/.gitkeep create mode 100644 src/main/java/tk/antoine/roux/infrastructure/out/.gitkeep 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