Compare commits
32 Commits
user
...
dependabot
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
601f1c2fea | ||
|
|
e2b77ab1d0 | ||
|
|
967a349b4c | ||
|
|
42b08c8995 | ||
|
|
58a9a0f95b | ||
|
|
9b0c37ffd7 | ||
|
|
53939113f2 | ||
|
|
a9fd37ec5a | ||
|
|
18d1038a18 | ||
|
|
f071d557ff | ||
|
|
b7db201e7b | ||
|
|
36316de3f7 | ||
|
|
428d281e38 | ||
|
|
62960ef444 | ||
|
|
19ce80db76 | ||
|
|
8a1e7ae20e | ||
|
|
d2b7d08072 | ||
|
|
2e5858d8a9 | ||
|
|
5e221dd427 | ||
|
|
e5e4767a9c | ||
|
|
8dc64f2c79 | ||
|
|
ba246cf0c9 | ||
|
|
a557dea9a9 | ||
|
|
b2f9c7f56f | ||
|
|
c13e60f42b | ||
|
|
58978def54 | ||
|
|
6b0aeb4141 | ||
|
|
273dfed81e | ||
|
|
62c683fb63 | ||
|
|
943693bd1d | ||
|
|
dbe12c918f | ||
|
|
6f4c938a85 |
@@ -8,4 +8,4 @@ RUN mvn -B -f pom.xml clean package -DskipTests
|
||||
FROM openjdk:11-jdk-slim
|
||||
COPY --from=build /workspace/target/*.jar app.jar
|
||||
EXPOSE 8080
|
||||
ENTRYPOINT ["java","-jar","app.jar"]
|
||||
ENTRYPOINT ["java","-jar","app.jar", "--spring.profiles.active=prod"]
|
||||
17
README.md
17
README.md
@@ -19,14 +19,23 @@ In the terminal run the following command:
|
||||
$ docker-compose up
|
||||
```
|
||||
|
||||
#### Using Maven
|
||||
#### Using Maven (with H2 or local Postgres database)
|
||||
|
||||
First make sure that you adjust the configuration file - `src/main/resources/application.yml` with connection details to your database.
|
||||
First compile an application:
|
||||
|
||||
Then, in the terminal run the following command:
|
||||
```console
|
||||
$ mvn clean package
|
||||
$ mvn spring-boot:run
|
||||
```
|
||||
|
||||
Then, you have two options either run it with H2 database or with local Postgres database. For first approach just run:
|
||||
|
||||
```console
|
||||
$ mvn spring-boot:run
|
||||
```
|
||||
|
||||
For a second option, check in the configuration file - `src/main/resources/application.yml` for profile *local-postgres* if connection details are correct and if so, run the command:
|
||||
```console
|
||||
$ mvn spring-boot:run -P local-postgres
|
||||
```
|
||||
|
||||
#### Inside IntelliJ (with H2 or Postgres database)
|
||||
|
||||
99
pom.xml
99
pom.xml
@@ -6,7 +6,7 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.6.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>io.wkrzywiec.hexagonal</groupId>
|
||||
@@ -17,14 +17,14 @@
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
<spring-cloud.version>Hoxton.M3</spring-cloud.version>
|
||||
<surefire.and.failsafe.report.dir>target/test-report</surefire.and.failsafe.report.dir>
|
||||
<code.coverage.exclusions>
|
||||
**/io/wkrzywiec/hexagonal/library/borrowing/model/**,
|
||||
**/io/wkrzywiec/hexagonal/library/email/model/**,
|
||||
**/io/wkrzywiec/hexagonal/library/inventory/model/**,
|
||||
**/io/wkrzywiec/hexagonal/library/user/model/**,
|
||||
**/io/wkrzywiec/hexagonal/library/infrastructure/repository/*Entity.java
|
||||
**/io/wkrzywiec/hexagonal/library/domain/borrowing/core/model/**,
|
||||
**/io/wkrzywiec/hexagonal/library/domain/email/core/model/**,
|
||||
**/io/wkrzywiec/hexagonal/library/domain/inventory/core/model/**,
|
||||
**/io/wkrzywiec/hexagonal/library/domain/user/core/model/**,
|
||||
**/io/wkrzywiec/hexagonal/library/**/*Entity.java,
|
||||
**/io/wkrzywiec/hexagonal/library/LibraryHexagonalApplication.java
|
||||
</code.coverage.exclusions>
|
||||
</properties>
|
||||
|
||||
@@ -33,6 +33,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
@@ -58,11 +62,6 @@
|
||||
<artifactId>jackson-module-jaxb-annotations</artifactId>
|
||||
<version>2.11.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vavr</groupId>
|
||||
<artifactId>vavr</artifactId>
|
||||
<version>0.10.3</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
@@ -73,7 +72,7 @@
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>42.2.12</version>
|
||||
<version>42.4.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.liquibase</groupId>
|
||||
@@ -92,16 +91,28 @@
|
||||
<version>1.2.32</version>
|
||||
</dependency>
|
||||
|
||||
<!-- TEST dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.spockframework</groupId>
|
||||
<artifactId>spock-core</artifactId>
|
||||
<version>1.3-groovy-2.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.spockframework</groupId>
|
||||
<artifactId>spock-spring</artifactId>
|
||||
<version>1.3-groovy-2.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.rest-assured</groupId>
|
||||
<artifactId>rest-assured</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.tngtech.archunit</groupId>
|
||||
@@ -109,10 +120,7 @@
|
||||
<version>0.13.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.rest-assured</groupId>
|
||||
<artifactId>rest-assured</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -145,8 +153,25 @@
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<reportsDirectory>${surefire.and.failsafe.report.dir}</reportsDirectory>
|
||||
<includes>
|
||||
<include>**/*Spec.java</include>
|
||||
<include>**/*Test.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.gmavenplus</groupId>
|
||||
<artifactId>gmavenplus-plugin</artifactId>
|
||||
<version>1.10.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>addTestSources</goal>
|
||||
<goal>compileTests</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>jacoco-maven-plugin</artifactId>
|
||||
@@ -191,6 +216,33 @@
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>default</id>
|
||||
<activation>
|
||||
<activeByDefault>true</activeByDefault>
|
||||
</activation>
|
||||
<properties>
|
||||
<active-profiles>default</active-profiles>
|
||||
</properties>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>prod</id>
|
||||
<activation>
|
||||
<activeByDefault>false</activeByDefault>
|
||||
</activation>
|
||||
<properties>
|
||||
<active-profiles>prod</active-profiles>
|
||||
</properties>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>local-postgres</id>
|
||||
<activation>
|
||||
<activeByDefault>false</activeByDefault>
|
||||
</activation>
|
||||
<properties>
|
||||
<active-profiles>local-postgres</active-profiles>
|
||||
</properties>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>component-test</id>
|
||||
<build>
|
||||
@@ -237,6 +289,7 @@
|
||||
<sonar.sources>.</sonar.sources>
|
||||
<sonar.inclusions>src/main/java/**,src/main/resources/**</sonar.inclusions>
|
||||
<sonar.exclusions>${code.coverage.exclusions}</sonar.exclusions>
|
||||
<sonat.tests>src/test/groovy,src/test/java</sonat.tests>
|
||||
<sonar.projectKey>wkrzywiec_library-hexagonal</sonar.projectKey>
|
||||
<sonar.organization>wkrzywiec</sonar.organization>
|
||||
<sonar.coverage.jacoco.xmlReportPaths>target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.wkrzywiec.hexagonal.library.BookTestData;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.BookReservationCommand;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.infrastructure.BookRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class MakeReservationComponentTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private BookRepository bookRepository;
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
private String baseURL;
|
||||
|
||||
@BeforeEach
|
||||
public void init() {
|
||||
this.baseURL = "http://localhost:" + port;
|
||||
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Reserve available book")
|
||||
@Sql({"/book-and-user.sql", "/available-book.sql"})
|
||||
@Sql(scripts = "/clean-database.sql", executionPhase = AFTER_TEST_METHOD)
|
||||
public void givenBookIsAvailable_thenMakeReservation_thenBookIsReserved() {
|
||||
//given
|
||||
Long homoDeusBookId = jdbcTemplate.queryForObject(
|
||||
"SELECT id FROM book WHERE title = ?",
|
||||
Long.class,
|
||||
BookTestData.homoDeusBookTitle());
|
||||
|
||||
Long activeUserId = jdbcTemplate.queryForObject(
|
||||
"SELECT id FROM user WHERE email = ?",
|
||||
Long.class,
|
||||
"john.doe@test.com");
|
||||
|
||||
BookReservationCommand reservationCommand =
|
||||
BookReservationCommand.builder()
|
||||
.bookId(homoDeusBookId )
|
||||
.userId(activeUserId)
|
||||
.build();
|
||||
|
||||
//when
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(reservationCommand)
|
||||
.when()
|
||||
.post( baseURL + "/reservations")
|
||||
.prettyPeek()
|
||||
.then();
|
||||
|
||||
Long reservationId = jdbcTemplate.queryForObject(
|
||||
"SELECT id FROM reserved WHERE book_id = ?",
|
||||
Long.class,
|
||||
homoDeusBookId);
|
||||
|
||||
assertTrue(reservationId > 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.wkrzywiec.hexagonal.library.DatabaseHelper;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public abstract class BaseComponentTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
protected String baseURL;
|
||||
|
||||
@Autowired
|
||||
protected JdbcTemplate jdbcTemplate;
|
||||
protected DatabaseHelper databaseHelper;
|
||||
|
||||
@BeforeEach
|
||||
public void init() {
|
||||
this.baseURL = "http://localhost:" + port;
|
||||
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
|
||||
databaseHelper = new DatabaseHelper(jdbcTemplate);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.BaseComponentTest;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.application.model.BookStatus;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.application.model.ChangeBookStatusRequest;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.BorrowBookCommand;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;
|
||||
|
||||
public class BorrowBookComponentTest extends BaseComponentTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Borrow reserved book")
|
||||
@Sql({"/book-and-user.sql", "/reserved-book.sql"})
|
||||
@Sql(scripts = "/clean-database.sql", executionPhase = AFTER_TEST_METHOD)
|
||||
public void givenBookIsReserved_thenBorrowIt_thenBookIsBorrowed() {
|
||||
//given
|
||||
Long homoDeusBookId = databaseHelper.getHomoDeusBookId();
|
||||
Long activeUserId = databaseHelper.getJohnDoeUserId();
|
||||
|
||||
ChangeBookStatusRequest borrowRequest =
|
||||
ChangeBookStatusRequest.builder()
|
||||
.userId(activeUserId)
|
||||
.status(BookStatus.BORROWED)
|
||||
.build();
|
||||
|
||||
//when
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(borrowRequest)
|
||||
.when()
|
||||
.patch( baseURL + "/books/" + homoDeusBookId + "/status")
|
||||
.prettyPeek()
|
||||
.then();
|
||||
|
||||
Long borrowId = databaseHelper.getPrimaryKeyOfBorrowedByBookId(homoDeusBookId);
|
||||
assertTrue(borrowId > 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.BaseComponentTest;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.application.model.BookStatus;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.application.model.ChangeBookStatusRequest;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.GiveBackBookCommand;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;
|
||||
|
||||
public class GiveBackBookComponentTest extends BaseComponentTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Give back borrowed book")
|
||||
@Sql({"/book-and-user.sql", "/borrowed-book.sql"})
|
||||
@Sql(scripts = "/clean-database.sql", executionPhase = AFTER_TEST_METHOD)
|
||||
public void givenBookIsBorrowed_thenGiveBackIt_thenItIsAvailable() {
|
||||
//given
|
||||
Long homoDeusBookId = databaseHelper.getHomoDeusBookId();
|
||||
Long activeUserId = databaseHelper.getJohnDoeUserId();
|
||||
|
||||
ChangeBookStatusRequest giveBackRequest =
|
||||
ChangeBookStatusRequest.builder()
|
||||
.userId(activeUserId)
|
||||
.status(BookStatus.AVAILABLE)
|
||||
.build();
|
||||
|
||||
//when
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(giveBackRequest)
|
||||
.when()
|
||||
.patch( baseURL + "/books/" + homoDeusBookId + "/status")
|
||||
.prettyPeek()
|
||||
.then();
|
||||
|
||||
Long bookId = databaseHelper.getPrimaryKeyOfAvailableByBookBy(homoDeusBookId);
|
||||
|
||||
assertEquals(homoDeusBookId, bookId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.BaseComponentTest;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.application.model.BookStatus;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.application.model.ChangeBookStatusRequest;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.BookReservationCommand;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.infrastructure.BookRepository;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;
|
||||
|
||||
public class MakeReservationComponentTest extends BaseComponentTest {
|
||||
|
||||
@Autowired
|
||||
private BookRepository bookRepository;
|
||||
|
||||
@Test
|
||||
@DisplayName("Reserve available book")
|
||||
@Sql({"/book-and-user.sql", "/available-book.sql"})
|
||||
@Sql(scripts = "/clean-database.sql", executionPhase = AFTER_TEST_METHOD)
|
||||
public void givenBookIsAvailable_thenMakeReservation_thenBookIsReserved() {
|
||||
//given
|
||||
Long homoDeusBookId = databaseHelper.getHomoDeusBookId();
|
||||
Long activeUserId = databaseHelper.getJohnDoeUserId();
|
||||
|
||||
ChangeBookStatusRequest reservationRequest =
|
||||
ChangeBookStatusRequest.builder()
|
||||
.userId(activeUserId)
|
||||
.status(BookStatus.RESERVED)
|
||||
.build();
|
||||
|
||||
//when
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(reservationRequest)
|
||||
.when()
|
||||
.patch( baseURL + "/books/" + homoDeusBookId + "/status")
|
||||
.prettyPeek()
|
||||
.then();
|
||||
|
||||
Long reservationId = databaseHelper.getPrimaryKeyOfReservationByBookId(homoDeusBookId);
|
||||
assertTrue(reservationId > 0);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,12 @@
|
||||
package io.wkrzywiec.hexagonal.library.inventory;
|
||||
package io.wkrzywiec.hexagonal.library.domain.inventory;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import io.wkrzywiec.hexagonal.library.BookTestData;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.AddNewBookCommand;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import io.wkrzywiec.hexagonal.library.domain.BaseComponentTest;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.AddNewBookCommand;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
@@ -19,22 +14,7 @@ import static org.hamcrest.Matchers.greaterThan;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class AddNewBookComponentTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbc;
|
||||
|
||||
private String baseURL;
|
||||
|
||||
@BeforeEach
|
||||
public void init(){
|
||||
this.baseURL = "http://localhost:" + port;
|
||||
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
|
||||
}
|
||||
public class AddNewBookComponentTest extends BaseComponentTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Search for a new book in Google Books")
|
||||
@@ -73,17 +53,10 @@ public class AddNewBookComponentTest {
|
||||
.then();
|
||||
|
||||
//then
|
||||
Long savedBookId = jdbc.queryForObject(
|
||||
"SELECT id FROM book WHERE book_external_id = ?",
|
||||
Long.class,
|
||||
BookTestData.homoDeusBookGoogleId());
|
||||
|
||||
Long savedBookId = databaseHelper.getHomoDeusBookId();
|
||||
assertTrue(savedBookId > 0);
|
||||
|
||||
Long availableBookId = jdbc.queryForObject(
|
||||
"SELECT id FROM available WHERE book_id = ?",
|
||||
Long.class,
|
||||
savedBookId);
|
||||
Long availableBookId = databaseHelper.getPrimaryKeyOfAvailableByBookBy(savedBookId);
|
||||
|
||||
assertTrue(availableBookId > 0);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.user;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.BaseComponentTest;
|
||||
import io.wkrzywiec.hexagonal.library.domain.user.core.model.AddUserCommand;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;
|
||||
|
||||
public class AddNewUserComponentTest extends BaseComponentTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Create new user")
|
||||
@Sql(scripts = "/clean-database.sql", executionPhase = AFTER_TEST_METHOD)
|
||||
public void shouldCreateNewUser(){
|
||||
//given
|
||||
AddUserCommand addUserCommand = AddUserCommand.builder()
|
||||
.firstName("John")
|
||||
.lastName("Doe")
|
||||
.email("john.doe@test.com")
|
||||
.build();
|
||||
|
||||
//when
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(addUserCommand)
|
||||
.when()
|
||||
.post( baseURL + "/users")
|
||||
.prettyPeek()
|
||||
.then();
|
||||
|
||||
//then
|
||||
Long savedUserId = databaseHelper.getJohnDoeUserId();
|
||||
assertTrue(savedUserId > 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
package io.wkrzywiec.hexagonal.library.user;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.wkrzywiec.hexagonal.library.user.model.AddUserCommand;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class AddNewUserComponentTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
private String baseURL;
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
@BeforeEach
|
||||
public void init(){
|
||||
baseURL = "http://localhost:" + port;
|
||||
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Create new user")
|
||||
@Sql(scripts = "/clean-database.sql", executionPhase = AFTER_TEST_METHOD)
|
||||
public void shouldCreateNewUser(){
|
||||
//given
|
||||
AddUserCommand addUserCommand = AddUserCommand.builder()
|
||||
.firstName("John")
|
||||
.lastName("Doe")
|
||||
.email("john.doe@test.com")
|
||||
.build();
|
||||
|
||||
//when
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(addUserCommand)
|
||||
.when()
|
||||
.post( baseURL + "/users")
|
||||
.prettyPeek()
|
||||
.then();
|
||||
|
||||
//then
|
||||
Long savedUserId = jdbcTemplate.queryForObject(
|
||||
"SELECT id FROM user WHERE email = ?",
|
||||
Long.class,
|
||||
"john.doe@test.com");
|
||||
|
||||
assertTrue(savedUserId > 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package io.wkrzywiec.hexagonal.library;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.BorrowingFacade;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.infrastructure.BorrowingDatabaseAdapter;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.infrastructure.SpringBorrowingEventPublisherAdapter;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.ports.incoming.MakeBookAvailable;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.ports.outgoing.BorrowingDatabase;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.ports.outgoing.BorrowingEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
public class BorrowingDomainConfig {
|
||||
|
||||
@Bean
|
||||
public BorrowingDatabase borrowingDatabase(JdbcTemplate jdbcTemplate) {
|
||||
return new BorrowingDatabaseAdapter(jdbcTemplate);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BorrowingEventPublisher borrowingEventPublisher(ApplicationEventPublisher applicationEventPublisher){
|
||||
return new SpringBorrowingEventPublisherAdapter(applicationEventPublisher);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MakeBookAvailable makeBookAvailable(BorrowingDatabase database, BorrowingEventPublisher borrowingEventPublisher) {
|
||||
return new BorrowingFacade(database, borrowingEventPublisher);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package io.wkrzywiec.hexagonal.library;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.email.EmailFacade;
|
||||
import io.wkrzywiec.hexagonal.library.email.infrastructure.EmailDatabaseAdapter;
|
||||
import io.wkrzywiec.hexagonal.library.email.infrastructure.SendGridEmailSender;
|
||||
import io.wkrzywiec.hexagonal.library.email.ports.incoming.SendReservationConfirmation;
|
||||
import io.wkrzywiec.hexagonal.library.email.ports.outgoing.EmailSender;
|
||||
import io.wkrzywiec.hexagonal.library.email.ports.outgoing.EmailDatabase;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
public class EmailDomainConfig {
|
||||
|
||||
@Bean
|
||||
public EmailSender emailSender() {
|
||||
return new SendGridEmailSender();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public EmailDatabase libraryDatabase(JdbcTemplate jdbcTemplate){
|
||||
return new EmailDatabaseAdapter(jdbcTemplate);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SendReservationConfirmation sendReservationConfirmation(EmailSender emailSender, EmailDatabase database){
|
||||
return new EmailFacade(emailSender, database);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,17 @@
|
||||
package io.wkrzywiec.hexagonal.library;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.infrastructure.BorrowingDomainConfig;
|
||||
import io.wkrzywiec.hexagonal.library.infrastructure.EmailDomainConfig;
|
||||
import io.wkrzywiec.hexagonal.library.infrastructure.InventoryDomainConfig;
|
||||
import io.wkrzywiec.hexagonal.library.infrastructure.LibraryHexagonalConfig;
|
||||
import io.wkrzywiec.hexagonal.library.infrastructure.UserDomainConfig;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
@Import({
|
||||
LibraryHexagonalConfig.class,
|
||||
InventoryDomainConfig.class,
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package io.wkrzywiec.hexagonal.library;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.user.UserFacade;
|
||||
import io.wkrzywiec.hexagonal.library.user.infrastructure.UserDatabaseAdapter;
|
||||
import io.wkrzywiec.hexagonal.library.user.infrastructure.UserRepository;
|
||||
import io.wkrzywiec.hexagonal.library.user.ports.incoming.AddNewUser;
|
||||
import io.wkrzywiec.hexagonal.library.user.ports.outgoing.UserDatabase;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
public class UserDomainConfig {
|
||||
|
||||
@Bean
|
||||
public UserDatabase userDatabase(UserRepository userRepository){
|
||||
return new UserDatabaseAdapter(userRepository);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AddNewUser addNewUser(UserDatabase userDatabase){
|
||||
return new UserFacade(userDatabase);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package io.wkrzywiec.hexagonal.library.application;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/")
|
||||
public class LibraryHexagonalController {
|
||||
|
||||
@GetMapping("")
|
||||
public String getAppRoot(){
|
||||
return "Library Hexagonal REST API";
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.ActiveUser;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.BookReservedEvent;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.MakeBookAvailableCommand;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.ReservationDetails;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.exception.ActiveUserNotFoundException;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.AvailableBook;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.exception.AvailableBookNotFoundExeption;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.BookReservationCommand;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.ReservedBook;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.ports.incoming.MakeBookAvailable;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.ports.incoming.ReserveBook;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.ports.outgoing.BorrowingDatabase;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.ports.outgoing.BorrowingEventPublisher;
|
||||
|
||||
public class BorrowingFacade implements MakeBookAvailable, ReserveBook {
|
||||
|
||||
private final BorrowingDatabase database;
|
||||
private final BorrowingEventPublisher eventPublisher;
|
||||
|
||||
public BorrowingFacade(BorrowingDatabase database, BorrowingEventPublisher eventPublisher) {
|
||||
this.database = database;
|
||||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(MakeBookAvailableCommand bookAvailableCommand) {
|
||||
database.setBookAvailable(bookAvailableCommand.getBookId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long handle(BookReservationCommand bookReservation) {
|
||||
AvailableBook availableBook =
|
||||
database.getAvailableBook(bookReservation.getBookId())
|
||||
.orElseThrow(() -> new AvailableBookNotFoundExeption(bookReservation.getBookId()));
|
||||
|
||||
ActiveUser activeUser =
|
||||
database.getActiveUser(bookReservation.getUserId())
|
||||
.orElseThrow(() -> new ActiveUserNotFoundException(bookReservation.getUserId()));
|
||||
|
||||
ReservedBook reservedBook = activeUser.reserve(availableBook);
|
||||
ReservationDetails reservationDetails = database.save(reservedBook);
|
||||
eventPublisher.publish(new BookReservedEvent(reservationDetails));
|
||||
return reservationDetails.getReservationId().getIdAsLong();
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.application;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.BookReservationCommand;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.ports.incoming.ReserveBook;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/reservations")
|
||||
@RequiredArgsConstructor
|
||||
public class ReservationController {
|
||||
|
||||
private final ReserveBook reserveBook;
|
||||
|
||||
@PostMapping("")
|
||||
public ResponseEntity<String> makeReservation(@RequestBody BookReservationCommand reservationCommand){
|
||||
Long reservationId = reserveBook.handle(reservationCommand);
|
||||
return new ResponseEntity<>("Reservation has been made with an id " + reservationId, HttpStatus.CREATED);
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.infrastructure;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.ActiveUser;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.AvailableBook;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.ReservationDetails;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.ReservationId;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.ReservedBook;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.ports.outgoing.BorrowingDatabase;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class BorrowingDatabaseAdapter implements BorrowingDatabase {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Override
|
||||
public void setBookAvailable(Long bookId) {
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO available (book_id) VALUES (?)",
|
||||
bookId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<AvailableBook> getAvailableBook(Long bookId) {
|
||||
try {
|
||||
return Optional.ofNullable(
|
||||
jdbcTemplate.queryForObject(
|
||||
"SELECT book_id FROM available WHERE book_id = ?",
|
||||
AvailableBook.class,
|
||||
bookId));
|
||||
} catch (DataAccessException exception) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ActiveUser> getActiveUser(Long userId) {
|
||||
try {
|
||||
jdbcTemplate.queryForObject(
|
||||
"SELECT id FROM public.user as u WHERE u.id = ?",
|
||||
Long.class,
|
||||
userId);
|
||||
} catch (DataAccessException exception) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
List<ReservedBook> reservedBooksByUser = getReservedBooksByUser(userId);
|
||||
return Optional.of(new ActiveUser(userId, reservedBooksByUser));
|
||||
}
|
||||
|
||||
private List<ReservedBook> getReservedBooksByUser(Long userId) {
|
||||
try {
|
||||
return jdbcTemplate.queryForList(
|
||||
"SELECT book_id FROM reserved WHERE reserved.user_id = ?",
|
||||
ReservedBook.class,
|
||||
userId
|
||||
);
|
||||
} catch (DataAccessException exception){
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReservationDetails save(ReservedBook reservedBook) {
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO reserved (book_id, user_id) VALUES (?, ?)",
|
||||
reservedBook.getIdAsLong(),
|
||||
reservedBook.getAssignedUserIdAsLong());
|
||||
|
||||
ReservationId reservationId = jdbcTemplate.queryForObject(
|
||||
"SELECT id FROM reserved WHERE book_id = ?",
|
||||
ReservationId.class,
|
||||
reservedBook.getIdAsLong());
|
||||
return new ReservationDetails(reservationId, reservedBook);
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.model;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.exception.TooManyBooksAssignedToUserException;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@EqualsAndHashCode
|
||||
public class ActiveUser {
|
||||
|
||||
private final Long id;
|
||||
private final List<ReservedBook> reservedBooks;
|
||||
|
||||
public ActiveUser(Long id, List<ReservedBook> reservedBooks) {
|
||||
this.id = id;
|
||||
this.reservedBooks = reservedBooks;
|
||||
}
|
||||
|
||||
public ReservedBook reserve(AvailableBook availableBook){
|
||||
if (reservedBooks.size() < 3){
|
||||
ReservedBook reservedBook = new ReservedBook(availableBook.getIdAsLong(), id);
|
||||
reservedBooks.add(reservedBook);
|
||||
return reservedBook;
|
||||
} else {
|
||||
throw new TooManyBooksAssignedToUserException(id);
|
||||
}
|
||||
}
|
||||
|
||||
public Long getIdAsLong(){
|
||||
return id;
|
||||
}
|
||||
|
||||
public List<ReservedBook> getReservedBookList(){
|
||||
return reservedBooks;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.model;
|
||||
|
||||
interface Book {
|
||||
Long getIdAsLong();
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.model;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@EqualsAndHashCode
|
||||
public class ReservedBook implements Book {
|
||||
|
||||
private final Long bookId;
|
||||
private final Long userId;
|
||||
|
||||
public ReservedBook(Long bookId, Long userId) {
|
||||
this.bookId = bookId;
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getIdAsLong() {
|
||||
return bookId;
|
||||
}
|
||||
|
||||
public Long getAssignedUserIdAsLong(){
|
||||
return userId;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.ports.incoming;
|
||||
|
||||
public interface BorrowBook {
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.ports.incoming;
|
||||
|
||||
public interface CancelReservation {
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.ports.incoming;
|
||||
|
||||
public interface GiveBackBook {
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.ports.incoming;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.MakeBookAvailableCommand;
|
||||
|
||||
public interface MakeBookAvailable {
|
||||
void handle(MakeBookAvailableCommand bookAvailableCommand);
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.ports.incoming;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.BookReservationCommand;
|
||||
|
||||
public interface ReserveBook {
|
||||
Long handle(BookReservationCommand bookReservation);
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.ports.outgoing;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.ActiveUser;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.AvailableBook;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.ReservationDetails;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.ReservedBook;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface BorrowingDatabase {
|
||||
void setBookAvailable(Long bookId);
|
||||
Optional<AvailableBook> getAvailableBook(Long bookId);
|
||||
Optional<ActiveUser> getActiveUser(Long userId);
|
||||
ReservationDetails save(ReservedBook reservedBook);
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.ports.outgoing;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.BookReservedEvent;
|
||||
|
||||
public interface BorrowingEventPublisher {
|
||||
public void publish(BookReservedEvent event);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.application;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.application.model.ChangeBookStatusRequest;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.BookReservationCommand;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.BorrowBookCommand;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.GiveBackBookCommand;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.incoming.BorrowBook;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.incoming.GiveBackBook;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.incoming.ReserveBook;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/books")
|
||||
@RequiredArgsConstructor
|
||||
public class BorrowingDomainController {
|
||||
|
||||
@Qualifier("GiveBackBook")
|
||||
private final GiveBackBook giveBackBook;
|
||||
|
||||
@Qualifier("ReserveBook")
|
||||
private final ReserveBook reserveBook;
|
||||
|
||||
@Qualifier("BorrowBook")
|
||||
private final BorrowBook borrowBook;
|
||||
|
||||
@PatchMapping("/{id}/status")
|
||||
public ResponseEntity<String> borrowBook(@PathVariable("id") Long bookId, @RequestBody ChangeBookStatusRequest request){
|
||||
switch (request.getStatus()){
|
||||
case AVAILABLE:
|
||||
giveBackBook.handle(new GiveBackBookCommand(bookId, request.getUserId()));
|
||||
return new ResponseEntity<>("Book with an id " + bookId + " was returned", HttpStatus.OK);
|
||||
case RESERVED:
|
||||
Long reservationId = reserveBook.handle(new BookReservationCommand(bookId, request.getUserId()));
|
||||
return new ResponseEntity<>("Reservation has been made with an id " + reservationId, HttpStatus.OK);
|
||||
case BORROWED:
|
||||
borrowBook.handle(new BorrowBookCommand(bookId, request.getUserId()));
|
||||
return new ResponseEntity<>("Book with an id " + bookId + " was borrowed", HttpStatus.OK);
|
||||
default:
|
||||
return new ResponseEntity<>("Book can't have status: " + request.getStatus(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.application;
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.application;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.MakeBookAvailableCommand;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.ports.incoming.MakeBookAvailable;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.NewBookWasAddedEvent;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.MakeBookAvailableCommand;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.incoming.MakeBookAvailable;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.NewBookWasAddedEvent;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -11,6 +12,7 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
public class NewBookWasAddedEventHandler {
|
||||
|
||||
@Qualifier("MakeBookAvailable")
|
||||
private final MakeBookAvailable makeBookAvailable;
|
||||
|
||||
@EventListener
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.application;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.incoming.CancelOverdueReservations;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class OverdueReservationScheduler {
|
||||
|
||||
@Qualifier("CancelOverdueReservations")
|
||||
private final CancelOverdueReservations overdueReservations;
|
||||
|
||||
@Scheduled(fixedRate = 60 * 1000)
|
||||
public void checkOverdueReservations(){
|
||||
overdueReservations.cancelOverdueReservations();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.application.model;
|
||||
|
||||
public enum BookStatus {
|
||||
AVAILABLE, RESERVED, BORROWED
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.application.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class ChangeBookStatusRequest {
|
||||
|
||||
private BookStatus status;
|
||||
private Long userId;
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.ActiveUser;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.AvailableBook;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.BookReservationCommand;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.BookReservedEvent;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.BorrowBookCommand;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.BorrowedBook;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.GiveBackBookCommand;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.MakeBookAvailableCommand;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.OverdueReservation;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.ReservationDetails;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.ReservedBook;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.exception.ActiveUserNotFoundException;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.exception.AvailableBookNotFoundExeption;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.exception.BorrowedBookNotFoundException;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.exception.ReservedBookNotFoundException;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.incoming.BorrowBook;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.incoming.CancelOverdueReservations;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.incoming.GiveBackBook;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.incoming.MakeBookAvailable;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.incoming.ReserveBook;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.outgoing.BorrowingDatabase;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.outgoing.BorrowingEventPublisher;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class BorrowingFacade implements MakeBookAvailable, ReserveBook, CancelOverdueReservations, BorrowBook, GiveBackBook {
|
||||
|
||||
private final BorrowingDatabase database;
|
||||
private final BorrowingEventPublisher eventPublisher;
|
||||
|
||||
public BorrowingFacade(BorrowingDatabase database, BorrowingEventPublisher eventPublisher) {
|
||||
this.database = database;
|
||||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(MakeBookAvailableCommand bookAvailableCommand) {
|
||||
database.save(new AvailableBook(bookAvailableCommand.getBookId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long handle(BookReservationCommand bookReservation) {
|
||||
AvailableBook availableBook =
|
||||
database.getAvailableBook(bookReservation.getBookId())
|
||||
.orElseThrow(() -> new AvailableBookNotFoundExeption(bookReservation.getBookId()));
|
||||
|
||||
ActiveUser activeUser =
|
||||
database.getActiveUser(bookReservation.getUserId())
|
||||
.orElseThrow(() -> new ActiveUserNotFoundException(bookReservation.getUserId()));
|
||||
|
||||
ReservedBook reservedBook = activeUser.reserve(availableBook);
|
||||
ReservationDetails reservationDetails = database.save(reservedBook);
|
||||
eventPublisher.publish(new BookReservedEvent(reservationDetails));
|
||||
return reservationDetails.getReservationId().getIdAsLong();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelOverdueReservations() {
|
||||
List<OverdueReservation> overdueReservationList = database.findReservationsForMoreThan(3L);
|
||||
overdueReservationList.forEach(
|
||||
overdueBook -> database.save(new AvailableBook(overdueBook.getBookIdentificationAsLong())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(BorrowBookCommand borrowBookCommand) {
|
||||
ActiveUser activeUser =
|
||||
database.getActiveUser(borrowBookCommand.getUserId())
|
||||
.orElseThrow(() -> new ActiveUserNotFoundException(borrowBookCommand.getUserId()));
|
||||
ReservedBook reservedBook =
|
||||
database.getReservedBook(borrowBookCommand.getBookId())
|
||||
.orElseThrow(() -> new ReservedBookNotFoundException(borrowBookCommand.getBookId()));
|
||||
|
||||
BorrowedBook borrowedBook = activeUser.borrow(reservedBook);
|
||||
database.save(borrowedBook);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(GiveBackBookCommand command) {
|
||||
BorrowedBook borrowedBook =
|
||||
database.getBorrowedBook(command.getBookId())
|
||||
.orElseThrow(() -> new BorrowedBookNotFoundException(command.getBookId()));
|
||||
ActiveUser activeUser =
|
||||
database.getActiveUser(command.getUserId())
|
||||
.orElseThrow(() -> new ActiveUserNotFoundException(command.getUserId()));
|
||||
|
||||
AvailableBook availableBook = activeUser.giveBack(borrowedBook);
|
||||
database.save(availableBook);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.exception.TooManyBooksAssignedToUserException;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@EqualsAndHashCode
|
||||
public class ActiveUser {
|
||||
|
||||
private final Long id;
|
||||
private final List<ReservedBook> reservedBooks;
|
||||
private final List<BorrowedBook> borrowedBooks;
|
||||
|
||||
public ActiveUser(Long id, List<ReservedBook> reservedBooks, List<BorrowedBook> borrowedBooks) {
|
||||
this.id = id;
|
||||
this.reservedBooks = reservedBooks;
|
||||
this.borrowedBooks = borrowedBooks;
|
||||
}
|
||||
|
||||
public ReservedBook reserve(AvailableBook availableBook){
|
||||
if (hasUserNotReachedLimitOfBooks()){
|
||||
ReservedBook reservedBook = new ReservedBook(availableBook.getIdAsLong(), id);
|
||||
reservedBooks.add(reservedBook);
|
||||
return reservedBook;
|
||||
} else {
|
||||
throw new TooManyBooksAssignedToUserException(id);
|
||||
}
|
||||
}
|
||||
|
||||
public BorrowedBook borrow(ReservedBook reservedBook) {
|
||||
if (hasUserNotReachedLimitOfBooks()){
|
||||
BorrowedBook borrowedBook = new BorrowedBook(reservedBook.getIdAsLong(), id);
|
||||
borrowedBooks.add(borrowedBook);
|
||||
return borrowedBook;
|
||||
} else {
|
||||
throw new TooManyBooksAssignedToUserException(id);
|
||||
}
|
||||
}
|
||||
|
||||
public AvailableBook giveBack(BorrowedBook borrowedBook) {
|
||||
boolean isBookRemovedFromUserAccount = borrowedBooks.removeIf(book -> book.equals(borrowedBook));
|
||||
if (isBookRemovedFromUserAccount){
|
||||
return new AvailableBook(borrowedBook.getIdAsLong());
|
||||
} else {
|
||||
throw new IllegalArgumentException("User with an id: " + id + " didn't borrow book with an id: " + borrowedBook.getIdAsLong());
|
||||
}
|
||||
}
|
||||
|
||||
public Long getIdAsLong(){
|
||||
return id;
|
||||
}
|
||||
|
||||
public List<ReservedBook> getReservedBookList(){
|
||||
return reservedBooks;
|
||||
}
|
||||
|
||||
public List<BorrowedBook> getBorrowedBookList() {
|
||||
return borrowedBooks;
|
||||
}
|
||||
|
||||
private boolean hasUserNotReachedLimitOfBooks(){
|
||||
return reservedBooks.size() + borrowedBooks.size() < 3;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
|
||||
|
||||
interface Book {
|
||||
Long getIdAsLong();
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
@Builder
|
||||
public class BorrowBookCommand {
|
||||
private Long bookId;
|
||||
private Long userId;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@EqualsAndHashCode
|
||||
public class BorrowedBook implements Book {
|
||||
|
||||
private Long bookId;
|
||||
private Long userId;
|
||||
@EqualsAndHashCode.Exclude
|
||||
private Instant borrowedDate;
|
||||
|
||||
public BorrowedBook(Long bookId, Long userId) {
|
||||
this.bookId = bookId;
|
||||
this.userId = userId;
|
||||
this.borrowedDate = Instant.now();
|
||||
}
|
||||
|
||||
public BorrowedBook(Long bookId, Long userId, Instant borrowedDate) {
|
||||
this.bookId = bookId;
|
||||
this.userId = userId;
|
||||
this.borrowedDate = borrowedDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getIdAsLong() {
|
||||
return bookId;
|
||||
}
|
||||
|
||||
public Long getAssignedUserIdAsLong(){
|
||||
return userId;
|
||||
}
|
||||
|
||||
public Instant getBorrowedDateAsInstant(){
|
||||
return borrowedDate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class DueDate {
|
||||
private final Instant timeStamp;
|
||||
|
||||
public Instant asInstant(){
|
||||
return timeStamp;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
@Builder
|
||||
public class GiveBackBookCommand {
|
||||
private Long bookId;
|
||||
private Long userId;
|
||||
}
|
||||
@@ -1,10 +1,8 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
@@ -0,0 +1,13 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class OverdueReservation {
|
||||
private Long reservationId;
|
||||
private Long bookIdentification;
|
||||
|
||||
public Long getBookIdentificationAsLong() {
|
||||
return bookIdentification;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@EqualsAndHashCode
|
||||
public class ReservedBook implements Book {
|
||||
|
||||
private final Long bookId;
|
||||
private final Long userId;
|
||||
private final Instant reservedDate;
|
||||
|
||||
public ReservedBook(Long bookId, Long userId) {
|
||||
this.bookId = bookId;
|
||||
this.userId = userId;
|
||||
this.reservedDate = Instant.now();
|
||||
}
|
||||
|
||||
public ReservedBook(Long bookId, Long userId, Instant reservedDate) {
|
||||
this.bookId = bookId;
|
||||
this.userId = userId;
|
||||
this.reservedDate = reservedDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getIdAsLong() {
|
||||
return bookId;
|
||||
}
|
||||
|
||||
public Long getAssignedUserIdAsLong(){
|
||||
return userId;
|
||||
}
|
||||
|
||||
public Instant getReservedDateAsInstant(){
|
||||
return reservedDate;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.model.exception;
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.exception;
|
||||
|
||||
public class ActiveUserNotFoundException extends RuntimeException {
|
||||
public ActiveUserNotFoundException(Long bookId){
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.model.exception;
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.exception;
|
||||
|
||||
public class AvailableBookNotFoundExeption extends RuntimeException {
|
||||
public AvailableBookNotFoundExeption(Long bookId){
|
||||
@@ -0,0 +1,10 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.exception;
|
||||
|
||||
public class BorrowedBookNotFoundException extends RuntimeException {
|
||||
public BorrowedBookNotFoundException(Long bookId) {
|
||||
super("There is no borrowed book with an ID: " + bookId,
|
||||
null,
|
||||
false,
|
||||
false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.exception;
|
||||
|
||||
public class ReservedBookNotFoundException extends RuntimeException {
|
||||
public ReservedBookNotFoundException(Long bookId) {
|
||||
super("There is no reserved book with an ID: " + bookId,
|
||||
null,
|
||||
false,
|
||||
false);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.model.exception;
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.exception;
|
||||
|
||||
public class TooManyBooksAssignedToUserException extends RuntimeException {
|
||||
public TooManyBooksAssignedToUserException(Long userId){
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.incoming;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.BorrowBookCommand;
|
||||
|
||||
public interface BorrowBook {
|
||||
void handle(BorrowBookCommand borrowBookCommand);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.incoming;
|
||||
|
||||
public interface CancelOverdueReservations {
|
||||
void cancelOverdueReservations();
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.incoming;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.GiveBackBookCommand;
|
||||
|
||||
public interface GiveBackBook {
|
||||
void handle(GiveBackBookCommand command);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.incoming;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.MakeBookAvailableCommand;
|
||||
|
||||
public interface MakeBookAvailable {
|
||||
void handle(MakeBookAvailableCommand bookAvailableCommand);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.incoming;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.BookReservationCommand;
|
||||
|
||||
public interface ReserveBook {
|
||||
Long handle(BookReservationCommand bookReservation);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.outgoing;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.ActiveUser;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.AvailableBook;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.BorrowedBook;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.DueDate;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.OverdueReservation;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.ReservationDetails;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.ReservedBook;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface BorrowingDatabase {
|
||||
void save(AvailableBook availableBook);
|
||||
ReservationDetails save(ReservedBook reservedBook);
|
||||
void save(BorrowedBook borrowedBook);
|
||||
Optional<AvailableBook> getAvailableBook(Long bookId);
|
||||
Optional<ActiveUser> getActiveUser(Long userId);
|
||||
List<OverdueReservation> findReservationsForMoreThan(Long days);
|
||||
Optional<ReservedBook> getReservedBook(Long bookId);
|
||||
Optional<BorrowedBook> getBorrowedBook(Long bookId);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.outgoing;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.BookReservedEvent;
|
||||
|
||||
public interface BorrowingEventPublisher {
|
||||
public void publish(BookReservedEvent event);
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.infrastructure;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.BorrowedBook;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.infrastructure.entity.OverdueReservationEntity;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.ActiveUser;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.AvailableBook;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.DueDate;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.OverdueReservation;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.ReservationDetails;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.ReservationId;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.ReservedBook;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.outgoing.BorrowingDatabase;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.infrastructure.mapper.BorrowedBookRowMapper;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.infrastructure.mapper.ReservedBookRowMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class BorrowingDatabaseAdapter implements BorrowingDatabase {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Override
|
||||
public void save(AvailableBook availableBook) {
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO available (book_id) VALUES (?)",
|
||||
availableBook.getIdAsLong());
|
||||
|
||||
jdbcTemplate.update(
|
||||
"DELETE FROM reserved WHERE book_id = ?",
|
||||
availableBook.getIdAsLong());
|
||||
|
||||
jdbcTemplate.update(
|
||||
"DELETE FROM borrowed WHERE book_id = ?",
|
||||
availableBook.getIdAsLong());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<AvailableBook> getAvailableBook(Long bookId) {
|
||||
try {
|
||||
return Optional.ofNullable(
|
||||
jdbcTemplate.queryForObject(
|
||||
"SELECT book_id FROM available WHERE book_id = ?",
|
||||
AvailableBook.class,
|
||||
bookId));
|
||||
} catch (DataAccessException exception) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ActiveUser> getActiveUser(Long userId) {
|
||||
try {
|
||||
jdbcTemplate.queryForObject(
|
||||
"SELECT id FROM public.library_user as u WHERE u.id = ?",
|
||||
Long.class,
|
||||
userId);
|
||||
} catch (DataAccessException exception) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
List<ReservedBook> reservedBooksByUser = getReservedBooksByUser(userId);
|
||||
List<BorrowedBook> borrowedBooksByUser = getBorrowedBooksByUser(userId);
|
||||
return Optional.of(new ActiveUser(userId, reservedBooksByUser, borrowedBooksByUser));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReservationDetails save(ReservedBook reservedBook) {
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO reserved (book_id, user_id, reserved_date) VALUES (?, ?, ?)",
|
||||
reservedBook.getIdAsLong(),
|
||||
reservedBook.getAssignedUserIdAsLong(),
|
||||
Timestamp.from(reservedBook.getReservedDateAsInstant()));
|
||||
|
||||
jdbcTemplate.update(
|
||||
"DELETE FROM available WHERE book_id = ?",
|
||||
reservedBook.getIdAsLong());
|
||||
|
||||
ReservationId reservationId = jdbcTemplate.queryForObject(
|
||||
"SELECT id FROM reserved WHERE book_id = ?",
|
||||
ReservationId.class,
|
||||
reservedBook.getIdAsLong());
|
||||
return new ReservationDetails(reservationId, reservedBook);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(BorrowedBook borrowedBook) {
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO borrowed (book_id, user_id, borrowed_date) VALUES (?, ?, ?)",
|
||||
borrowedBook.getIdAsLong(),
|
||||
borrowedBook.getAssignedUserIdAsLong(),
|
||||
Timestamp.from(borrowedBook.getBorrowedDateAsInstant()));
|
||||
|
||||
jdbcTemplate.update(
|
||||
"DELETE FROM reserved WHERE book_id = ?",
|
||||
borrowedBook.getIdAsLong());
|
||||
|
||||
jdbcTemplate.update(
|
||||
"DELETE FROM available WHERE book_id = ?",
|
||||
borrowedBook.getIdAsLong());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OverdueReservation> findReservationsForMoreThan(Long days) {
|
||||
List<OverdueReservationEntity> entities = jdbcTemplate.query(
|
||||
"SELECT id AS reservationId, book_id AS bookIdentification FROM reserved WHERE DATEADD(day, ?, reserved_date) > NOW()",
|
||||
new BeanPropertyRowMapper<OverdueReservationEntity>(OverdueReservationEntity.class),
|
||||
days);
|
||||
return entities.stream()
|
||||
.map(entity -> new OverdueReservation(entity.getReservationId(), entity.getBookIdentification()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ReservedBook> getReservedBook(Long bookId) {
|
||||
try {
|
||||
return Optional.ofNullable(
|
||||
jdbcTemplate.queryForObject(
|
||||
"SELECT book_id, user_id, reserved_date FROM reserved WHERE book_id = ?",
|
||||
new ReservedBookRowMapper(),
|
||||
bookId));
|
||||
} catch (DataAccessException exception) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<BorrowedBook> getBorrowedBook(Long bookId) {
|
||||
try {
|
||||
return Optional.ofNullable(
|
||||
jdbcTemplate.queryForObject(
|
||||
"SELECT book_id, user_id, borrowed_date FROM borrowed WHERE book_id = ?",
|
||||
new BorrowedBookRowMapper(),
|
||||
bookId));
|
||||
} catch (DataAccessException exception) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
private List<ReservedBook> getReservedBooksByUser(Long userId) {
|
||||
try {
|
||||
return jdbcTemplate.query(
|
||||
"SELECT book_id, user_id, reserved_date FROM reserved WHERE user_id = ?",
|
||||
new ReservedBookRowMapper(),
|
||||
userId
|
||||
);
|
||||
} catch (DataAccessException exception){
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
private List<BorrowedBook> getBorrowedBooksByUser(Long userId) {
|
||||
try {
|
||||
return jdbcTemplate.query(
|
||||
"SELECT book_id, user_id, borrowed_date FROM borrowed WHERE user_id = ?",
|
||||
new BorrowedBookRowMapper(),
|
||||
userId
|
||||
);
|
||||
} catch (DataAccessException exception){
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.wkrzywiec.hexagonal.library.borrowing.infrastructure;
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.infrastructure;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.BookReservedEvent;
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.ports.outgoing.BorrowingEventPublisher;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.BookReservedEvent;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.outgoing.BorrowingEventPublisher;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -0,0 +1,9 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.infrastructure.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class OverdueReservationEntity {
|
||||
private Long reservationId;
|
||||
private Long bookIdentification;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.infrastructure.mapper;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.BorrowedBook;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
|
||||
public class BorrowedBookRowMapper implements RowMapper<BorrowedBook> {
|
||||
|
||||
@Override
|
||||
public BorrowedBook mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
return new BorrowedBook(
|
||||
rs.getLong("book_id"),
|
||||
rs.getLong("user_id"),
|
||||
rs.getTimestamp("borrowed_date").toInstant()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.borrowing.infrastructure.mapper;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.ReservedBook;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class ReservedBookRowMapper implements RowMapper<ReservedBook> {
|
||||
|
||||
@Override
|
||||
public ReservedBook mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
return new ReservedBook(
|
||||
rs.getLong("book_id"),
|
||||
rs.getLong("user_id"),
|
||||
rs.getTimestamp("reserved_date").toInstant()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package io.wkrzywiec.hexagonal.library.email.application;
|
||||
package io.wkrzywiec.hexagonal.library.domain.email.application;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.borrowing.model.BookReservedEvent;
|
||||
import io.wkrzywiec.hexagonal.library.email.model.SendReservationConfirmationCommand;
|
||||
import io.wkrzywiec.hexagonal.library.email.ports.incoming.SendReservationConfirmation;
|
||||
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.BookReservedEvent;
|
||||
import io.wkrzywiec.hexagonal.library.domain.email.core.model.SendReservationConfirmationCommand;
|
||||
import io.wkrzywiec.hexagonal.library.domain.email.core.ports.incoming.SendReservationConfirmation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.wkrzywiec.hexagonal.library.email;
|
||||
package io.wkrzywiec.hexagonal.library.domain.email.core;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.email.model.EmailAddress;
|
||||
import io.wkrzywiec.hexagonal.library.email.model.ReservationConfirmEmail;
|
||||
import io.wkrzywiec.hexagonal.library.domain.email.core.model.EmailAddress;
|
||||
import io.wkrzywiec.hexagonal.library.domain.email.core.model.ReservationConfirmEmail;
|
||||
|
||||
class EmailCreator {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package io.wkrzywiec.hexagonal.library.email;
|
||||
package io.wkrzywiec.hexagonal.library.domain.email.core;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.email.model.ReservationConfirmEmail;
|
||||
import io.wkrzywiec.hexagonal.library.email.model.SendReservationConfirmationCommand;
|
||||
import io.wkrzywiec.hexagonal.library.email.ports.incoming.SendReservationConfirmation;
|
||||
import io.wkrzywiec.hexagonal.library.email.ports.outgoing.EmailSender;
|
||||
import io.wkrzywiec.hexagonal.library.email.ports.outgoing.EmailDatabase;
|
||||
import io.wkrzywiec.hexagonal.library.domain.email.core.model.ReservationConfirmEmail;
|
||||
import io.wkrzywiec.hexagonal.library.domain.email.core.model.SendReservationConfirmationCommand;
|
||||
import io.wkrzywiec.hexagonal.library.domain.email.core.ports.incoming.SendReservationConfirmation;
|
||||
import io.wkrzywiec.hexagonal.library.domain.email.core.ports.outgoing.EmailSender;
|
||||
import io.wkrzywiec.hexagonal.library.domain.email.core.ports.outgoing.EmailDatabase;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.email.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.email.core.model;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.email.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.email.core.model;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.email.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.email.core.model;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.email.core.ports.incoming;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.email.core.model.SendReservationConfirmationCommand;
|
||||
|
||||
public interface SendReservationConfirmation {
|
||||
void handle(SendReservationConfirmationCommand reservationConfirmationCommand);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.email.ports.outgoing;
|
||||
package io.wkrzywiec.hexagonal.library.domain.email.core.ports.outgoing;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.email.core.ports.outgoing;
|
||||
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.email.core.model.ReservationConfirmEmail;
|
||||
|
||||
public interface EmailSender {
|
||||
void sendReservationConfirmationEmail(ReservationConfirmEmail reservationConfirmEmail);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package io.wkrzywiec.hexagonal.library.email.infrastructure;
|
||||
package io.wkrzywiec.hexagonal.library.domain.email.infrastructure;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.email.ports.outgoing.EmailDatabase;
|
||||
import io.wkrzywiec.hexagonal.library.domain.email.core.ports.outgoing.EmailDatabase;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
@@ -28,7 +28,7 @@ public class EmailDatabaseAdapter implements EmailDatabase {
|
||||
public Optional<String> getUserEmailAddress(Long userId) {
|
||||
try {
|
||||
return Optional.ofNullable(jdbcTemplate.queryForObject(
|
||||
"SELECT email FROM user WHERE id = ?",
|
||||
"SELECT email FROM library_user WHERE id = ?",
|
||||
String.class,
|
||||
userId));
|
||||
} catch (DataAccessException ex){
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.email.infrastructure;
|
||||
package io.wkrzywiec.hexagonal.library.domain.email.infrastructure;
|
||||
|
||||
import com.sendgrid.Method;
|
||||
import com.sendgrid.Request;
|
||||
@@ -6,8 +6,8 @@ import com.sendgrid.SendGrid;
|
||||
import com.sendgrid.helpers.mail.Mail;
|
||||
import com.sendgrid.helpers.mail.objects.Content;
|
||||
import com.sendgrid.helpers.mail.objects.Email;
|
||||
import io.wkrzywiec.hexagonal.library.email.model.ReservationConfirmEmail;
|
||||
import io.wkrzywiec.hexagonal.library.email.ports.outgoing.EmailSender;
|
||||
import io.wkrzywiec.hexagonal.library.domain.email.core.model.ReservationConfirmEmail;
|
||||
import io.wkrzywiec.hexagonal.library.domain.email.core.ports.outgoing.EmailSender;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.wkrzywiec.hexagonal.library.inventory.application;
|
||||
package io.wkrzywiec.hexagonal.library.domain.inventory.application;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.AddNewBookCommand;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.ports.incoming.AddNewBook;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.AddNewBookCommand;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.ports.incoming.AddNewBook;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -1,12 +1,12 @@
|
||||
package io.wkrzywiec.hexagonal.library.inventory;
|
||||
package io.wkrzywiec.hexagonal.library.domain.inventory.core;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.AddNewBookCommand;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.Book;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.NewBookWasAddedEvent;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.ports.incoming.AddNewBook;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.ports.outgoing.InventoryEventPublisher;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.ports.outgoing.GetBookDetails;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.ports.outgoing.InventoryDatabase;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.AddNewBookCommand;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.Book;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.NewBookWasAddedEvent;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.ports.incoming.AddNewBook;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.ports.outgoing.InventoryEventPublisher;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.ports.outgoing.GetBookDetails;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.ports.outgoing.InventoryDatabase;
|
||||
|
||||
|
||||
public class InventoryFacade implements AddNewBook{
|
||||
@@ -1,8 +1,7 @@
|
||||
package io.wkrzywiec.hexagonal.library.inventory.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.inventory.core.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.inventory.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.inventory.core.model;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.inventory.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.inventory.core.model;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.inventory.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.inventory.core.model;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.inventory.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.inventory.core.model;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package io.wkrzywiec.hexagonal.library.inventory.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.inventory.core.model;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Embeddable;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.inventory.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.inventory.core.model;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.inventory.core.ports.incoming;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.AddNewBookCommand;
|
||||
|
||||
public interface AddNewBook {
|
||||
void handle(AddNewBookCommand addNewBookCommand);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.inventory.core.ports.outgoing;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.Book;
|
||||
|
||||
public interface GetBookDetails {
|
||||
Book handle(String bookId);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.inventory.core.ports.outgoing;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.Book;
|
||||
|
||||
public interface InventoryDatabase {
|
||||
Book save(Book book);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.inventory.core.ports.outgoing;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.NewBookWasAddedEvent;
|
||||
|
||||
public interface InventoryEventPublisher {
|
||||
void publishNewBookWasAddedEvent(NewBookWasAddedEvent event);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package io.wkrzywiec.hexagonal.library.inventory.infrastructure;
|
||||
package io.wkrzywiec.hexagonal.library.domain.inventory.infrastructure;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.Book;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.Book;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package io.wkrzywiec.hexagonal.library.inventory.infrastructure;
|
||||
package io.wkrzywiec.hexagonal.library.domain.inventory.infrastructure;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.Author;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.Book;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.BookIdentification;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.Isbn10;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.Isbn13;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.ports.outgoing.GetBookDetails;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.Author;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.Book;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.BookIdentification;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.Isbn10;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.Isbn13;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.ports.outgoing.GetBookDetails;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.wkrzywiec.hexagonal.library.inventory.infrastructure;
|
||||
package io.wkrzywiec.hexagonal.library.domain.inventory.infrastructure;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.Book;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.ports.outgoing.InventoryDatabase;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.Book;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.ports.outgoing.InventoryDatabase;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.wkrzywiec.hexagonal.library.inventory.infrastructure;
|
||||
package io.wkrzywiec.hexagonal.library.domain.inventory.infrastructure;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.NewBookWasAddedEvent;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.ports.outgoing.InventoryEventPublisher;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.NewBookWasAddedEvent;
|
||||
import io.wkrzywiec.hexagonal.library.domain.inventory.core.ports.outgoing.InventoryEventPublisher;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.wkrzywiec.hexagonal.library.user.application;
|
||||
package io.wkrzywiec.hexagonal.library.domain.user.application;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.user.model.AddUserCommand;
|
||||
import io.wkrzywiec.hexagonal.library.user.ports.incoming.AddNewUser;
|
||||
import io.wkrzywiec.hexagonal.library.domain.user.core.model.AddUserCommand;
|
||||
import io.wkrzywiec.hexagonal.library.domain.user.core.ports.incoming.AddNewUser;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -0,0 +1,25 @@
|
||||
package io.wkrzywiec.hexagonal.library.domain.user.core;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.domain.user.core.model.AddUserCommand;
|
||||
import io.wkrzywiec.hexagonal.library.domain.user.core.model.EmailAddress;
|
||||
import io.wkrzywiec.hexagonal.library.domain.user.core.model.User;
|
||||
import io.wkrzywiec.hexagonal.library.domain.user.core.model.UserIdentifier;
|
||||
import io.wkrzywiec.hexagonal.library.domain.user.core.ports.incoming.AddNewUser;
|
||||
import io.wkrzywiec.hexagonal.library.domain.user.core.ports.outgoing.UserDatabase;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class UserFacade implements AddNewUser {
|
||||
|
||||
private final UserDatabase database;
|
||||
|
||||
@Override
|
||||
public UserIdentifier handle(AddUserCommand addUserCommand) {
|
||||
User user = new User(
|
||||
new EmailAddress(addUserCommand.getEmail()),
|
||||
addUserCommand.getFirstName(),
|
||||
addUserCommand.getLastName()
|
||||
);
|
||||
return database.save(user);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.user.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.user.core.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.user.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.user.core.model;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package io.wkrzywiec.hexagonal.library.user.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.user.core.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import javax.persistence.Column;
|
||||
@@ -12,7 +11,7 @@ import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name="user")
|
||||
@Table(name="library_user")
|
||||
@EqualsAndHashCode
|
||||
public class User {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.user.model;
|
||||
package io.wkrzywiec.hexagonal.library.domain.user.core.model;
|
||||
|
||||
|
||||
public class UserIdentifier {
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user