Compare commits

...

85 Commits

Author SHA1 Message Date
dependabot[bot]
601f1c2fea Bump postgresql from 42.2.12 to 42.4.1
Bumps [postgresql](https://github.com/pgjdbc/pgjdbc) from 42.2.12 to 42.4.1.
- [Release notes](https://github.com/pgjdbc/pgjdbc/releases)
- [Changelog](https://github.com/pgjdbc/pgjdbc/blob/master/CHANGELOG.md)
- [Commits](https://github.com/pgjdbc/pgjdbc/compare/REL42.2.12...REL42.4.1)

---
updated-dependencies:
- dependency-name: org.postgresql:postgresql
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-09-08 01:09:12 +00:00
Wojtek Krzywiec
e2b77ab1d0 Merge pull request #19 from wkrzywiec/spock-tests
Spock depenencies tests
2020-10-13 06:37:00 +02:00
Wojtek Krzywiec
967a349b4c bring back JUnit4 deps 2020-10-13 06:34:26 +02:00
Wojtek Krzywiec
42b08c8995 BorrowingFacadeSpec added 2020-09-20 12:32:04 +02:00
Wojtek Krzywiec
58a9a0f95b add spock dependencies to pom.xml 2020-09-19 12:41:34 +02:00
Wojtek Krzywiec
9b0c37ffd7 Merge branch 'polishing' 2020-06-14 15:09:45 +02:00
Wojtek Krzywiec
53939113f2 fixes 2020-06-14 15:08:53 +02:00
Wojtek Krzywiec
a9fd37ec5a Merge pull request #18 from wkrzywiec/polishing
Polishing
2020-06-08 21:33:59 +02:00
wkrzywiec
18d1038a18 rename user table to library_user plus small type corrections 2020-06-08 21:31:32 +02:00
Wojtek Krzywiec
f071d557ff more restful controller 2020-06-08 07:07:26 +02:00
Wojtek Krzywiec
b7db201e7b database helper for tests 2020-06-08 06:41:30 +02:00
Wojtek Krzywiec
36316de3f7 database init for testing 2020-06-08 06:15:10 +02:00
Wojtek Krzywiec
428d281e38 actuator added & spring profiles introduced 2020-06-07 23:13:32 +02:00
Wojtek Krzywiec
62960ef444 Merge pull request #17 from wkrzywiec/give-back-book
Give back book
2020-06-07 11:51:13 +02:00
Wojtek Krzywiec
19ce80db76 add RowMapper for ReservedBook and BorrowedBook 2020-06-07 11:47:57 +02:00
Wojtek Krzywiec
8a1e7ae20e give back application layer 2020-06-07 11:03:28 +02:00
Wojtek Krzywiec
d2b7d08072 add give back business logic 2020-06-07 08:56:17 +02:00
Wojtek Krzywiec
2e5858d8a9 add give back business logic 2020-06-05 22:41:12 +02:00
Wojtek Krzywiec
5e221dd427 Merge pull request #16 from wkrzywiec/borrow
Borrow book
2020-06-04 08:18:13 +02:00
Wojtek Krzywiec
e5e4767a9c applicationa layer for borrowing books 2020-06-04 08:15:48 +02:00
Wojtek Krzywiec
8dc64f2c79 adpater for saving borrowed book 2020-06-04 06:59:03 +02:00
Wojtek Krzywiec
ba246cf0c9 borrow book facade 2020-06-02 07:52:52 +02:00
Wojtek Krzywiec
a557dea9a9 cleaning 2020-05-31 16:19:56 +02:00
Wojtek Krzywiec
b2f9c7f56f Merge pull request #15 from wkrzywiec/cancel-reservation
cancel reservation scheduler added
2020-05-31 16:03:51 +02:00
Wojtek Krzywiec
c13e60f42b correct test coverage exclusions 2020-05-31 15:58:21 +02:00
Wojtek Krzywiec
58978def54 cancel reservation scheduler added 2020-05-30 21:57:35 +02:00
Wojtek Krzywiec
6b0aeb4141 Merge pull request #14 from wkrzywiec/cancel-reservation
Cancel reservation
2020-05-30 21:50:09 +02:00
Wojtek Krzywiec
273dfed81e refactoring 2020-05-30 21:47:27 +02:00
Wojtek Krzywiec
62c683fb63 find overdue reservations 2020-05-30 21:30:56 +02:00
Wojtek Krzywiec
943693bd1d cancel reservation core 2020-05-30 16:35:25 +02:00
Wojtek Krzywiec
dbe12c918f remove book from available table after reservation 2020-05-29 20:37:54 +02:00
Wojtek Krzywiec
6f4c938a85 Merge pull request #13 from wkrzywiec/user
User domain
2020-05-29 17:45:12 +02:00
wkrzywiec
4d69df25e9 UserTestData and BookTestData 2020-05-29 17:40:47 +02:00
wkrzywiec
fa0e83ad92 UserCommandController 2020-05-29 08:56:16 +02:00
wkrzywiec
93f2eed4c8 UserDatabaseAdapter implemented 2020-05-28 20:20:45 +02:00
wkrzywiec
b2b1003b0b add user domain 2020-05-28 13:24:33 +02:00
Wojtek Krzywiec
29f3997a98 Merge pull request #12 from wkrzywiec/reservation-email
Reservation email
2020-05-28 11:52:54 +02:00
wkrzywiec
727481a520 ignore model classes for test coverage 2020-05-28 11:49:51 +02:00
wkrzywiec
5f4809cfd4 get book title & email address from db - email domain 2020-05-28 10:57:26 +02:00
wkrzywiec
c8b8c26d80 implement email sending adapter 2020-05-28 10:03:24 +02:00
wkrzywiec
9e5ba16a1c clean database after each integration test 2020-05-27 18:25:13 +02:00
wkrzywiec
3f07eac529 email domain tests 2020-05-25 08:35:00 +02:00
wkrzywiec
a234301f1d email address creation tests 2020-05-25 08:11:41 +02:00
wkrzywiec
14441bfbd6 email domain logic 2020-05-25 07:11:01 +02:00
wkrzywiec
648b877b86 publish book reserved event 2020-05-25 06:28:42 +02:00
Wojtek Krzywiec
fbeac054cd Merge pull request #11 from wkrzywiec/add-new-book-event
database cleanup
2020-05-24 12:05:09 +02:00
wkrzywiec
6435c351f9 database cleanup 2020-05-24 12:01:22 +02:00
Wojtek Krzywiec
c9de6efc28 Merge pull request #10 from wkrzywiec/add-new-book-event
Add new book event
2020-05-24 11:53:55 +02:00
wkrzywiec
fa0c0fe4f4 reservation REST endpoint added 2020-05-24 11:38:05 +02:00
wkrzywiec
672c6f6557 adjust AddNewBookComponentTest to make book available 2020-05-24 10:51:02 +02:00
wkrzywiec
d9b68daa94 architecture tests adjusted 2020-05-24 10:30:13 +02:00
wkrzywiec
1c59f79cde component test naming convention 2020-05-22 12:07:18 +02:00
wkrzywiec
d2f52f849d SpringInventoryEventPublisherAdpater added 2020-05-22 11:44:19 +02:00
wkrzywiec
6f998cf030 publish event added to domain 2020-05-21 16:35:09 +02:00
wkrzywiec
f613a67517 return savedBook by database 2020-05-21 16:24:58 +02:00
wkrzywiec
8bea3936b7 add missing integration test for InventoryDatabaseAdapter 2020-05-21 15:53:37 +02:00
Wojtek Krzywiec
bebd23d22b Merge pull request #9 from wkrzywiec/reserve-book
Reserve book
2020-05-21 12:35:59 +02:00
wkrzywiec
4b28fe3460 sonar analysis fixes 2020-05-21 12:33:01 +02:00
wkrzywiec
b3378b8c43 architecture unit tests 2020-05-21 07:58:56 +02:00
Wojtek Krzywiec
246071b743 Merge pull request #8 from wkrzywiec/reserve-book
Reserve book
2020-05-20 22:15:27 +02:00
wkrzywiec
4b2f22307a architecture test corrected 2020-05-20 22:13:26 +02:00
wkrzywiec
b06d8219a4 inventory - replace dto with model 2020-05-20 22:01:42 +02:00
wkrzywiec
05b4c9e270 refactoring - change naming convention 2020-05-20 20:31:17 +02:00
wkrzywiec
fe0dd60540 repackage stuff into domains 2020-05-20 16:52:06 +02:00
wkrzywiec
4b1fa6048d BorrowingFacade return reservationId 2020-05-20 16:31:21 +02:00
wkrzywiec
3c4527fff4 prepare data for emailSender 2020-05-20 13:43:48 +02:00
wkrzywiec
d325e9196e BorrowingDatabaseAdapter added 2020-05-20 12:18:32 +02:00
wkrzywiec
3f57a0c9c4 handling reservation tuning 2020-05-20 06:38:54 +02:00
wkrzywiec
4b9a0b1987 make book available 2020-05-19 16:32:48 +02:00
wkrzywiec
442bf870c3 add liquibase database init script 2020-05-18 16:05:42 +02:00
wkrzywiec
83f947b70b add liquibase database init script 2020-05-18 10:19:17 +02:00
wkrzywiec
8a66e987ad book reservation domain 2020-05-17 13:33:38 +02:00
wkrzywiec
dc3730f1c9 rename book domain to inventory 2020-05-16 14:19:19 +02:00
Wojtek Krzywiec
9cd190deb7 correct typo in workflow 2020-05-14 16:19:55 +02:00
Wojtek Krzywiec
f450176b20 add integration workflows 2020-05-14 16:17:23 +02:00
Wojtek Krzywiec
2eb6483aba docker set up added 2020-05-14 15:51:35 +02:00
Wojtek Krzywiec
022046139c Merge pull request #7 from wkrzywiec/add-new-book
Add new book
2020-05-14 14:18:23 +02:00
Wojtek Krzywiec
33ba479942 add README.md 2020-05-14 14:15:52 +02:00
Wojtek Krzywiec
c96c79a2d0 exclude unnecessary classes from test coverage 2020-05-14 13:58:00 +02:00
Wojtek Krzywiec
31db7bf37a rename model folder to dto 2020-05-14 13:45:02 +02:00
Wojtek Krzywiec
94425cda44 rename model folder to dto 2020-05-14 13:44:26 +02:00
Wojtek Krzywiec
4ef128c2f9 Merge pull request #6 from wkrzywiec/sonar-set-up
clean pom.xml
2020-05-14 13:21:50 +02:00
Wojtek Krzywiec
62a5be6d57 Merge pull request #5 from wkrzywiec/sonar-set-up
remove combining jacoco reports
2020-05-14 12:58:21 +02:00
Wojtek Krzywiec
53114aedf2 Merge pull request #4 from wkrzywiec/sonar-set-up
add jacoco merged report
2020-05-14 12:42:29 +02:00
Wojtek Krzywiec
e1f9f7acbc Merge pull request #3 from wkrzywiec/sonar-set-up
test reports in xml
2020-05-14 12:14:49 +02:00
151 changed files with 3785 additions and 463 deletions

View File

@@ -35,4 +35,36 @@ jobs:
- name: SonarCloud Scan
run: mvn -B clean verify -Psonar,component-test -Dsonar.login=${{ secrets.SONAR_TOKEN }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
artifact:
name: Publish - GitHub Packages
runs-on: ubuntu-18.04
needs: [test, sonar]
steps:
- uses: actions/checkout@v1
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11.0.4
- name: Publish artifact on GitHub Packages
run: mvn -B clean deploy -DskipTests
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
docker:
name: Publish - Docker Hub
runs-on: ubuntu-18.04
needs: [test, sonar]
env:
REPO: ${{ secrets.DOCKER_REPO }}
steps:
- uses: actions/checkout@v1
- name: Login to Docker Hub
run: docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_PASS }}
- name: Build Docker image
run: docker build -t $REPO:latest -t $REPO:${GITHUB_SHA::8} .
- name: Publish Docker image
run: docker push $REPO

1
.gitignore vendored
View File

@@ -31,3 +31,4 @@ build/
### VS Code ###
.vscode/
sendgrid.env

11
Dockerfile Normal file
View File

@@ -0,0 +1,11 @@
FROM maven:3.6.3-jdk-11-slim AS build
RUN mkdir -p /workspace
WORKDIR /workspace
COPY pom.xml /workspace
COPY src /workspace/src
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", "--spring.profiles.active=prod"]

46
README.md Normal file
View File

@@ -0,0 +1,46 @@
# Library
> written in *Hexagonal (Ports & Adapters) Architecture*
![Master Branch](https://github.com/wkrzywiec/library-hexagonal/workflows/Master%20Branch/badge.svg?branch=master) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=wkrzywiec_library-hexagonal&metric=alert_status)](https://sonarcloud.io/dashboard?id=wkrzywiec_library-hexagonal) [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=wkrzywiec_library-hexagonal&metric=coverage)](https://sonarcloud.io/dashboard?id=wkrzywiec_library-hexagonal) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
This is a small application that provides basic REST endpoints for managing library (add new book, reserve, borrow it, etc.).
The technology behind it:
* Java 11
* Postgres
* Spring Boot
## Installing / Getting started
#### Using `docker-compose`
In the terminal run the following command:
```console
$ docker-compose up
```
#### Using Maven (with H2 or local Postgres database)
First compile an application:
```console
$ mvn clean package
```
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)
First configure how you run the `LibraryHexagonalApplication.java` by adding `--spring.profiles.active=h2` (for H2 database) or `--spring.profiles.active=postgres` (for Postgres database) as a **Program argument**.
Then just run the `LibraryHexagonalApplication.java` class so it will use H2 database (you don't need to have postgres database up and running).

31
docker-compose.yml Normal file
View File

@@ -0,0 +1,31 @@
version: '3'
services:
postgres:
image: "postgres:9.6-alpine"
container_name: postgres
volumes:
- postgres-data:/var/lib/postgresql/data
ports:
- 5432:5432
environment:
- POSTGRES_SERVER=postgres
- POSTGRES_DB=library
- POSTGRES_USER=library
- POSTGRES_PASSWORD=library
library:
build: .
container_name: library-app
ports:
- 8080:8080
environment:
- POSTGRES_SERVER=postgres
- POSTGRES_DB=library
- POSTGRES_USER=library
- POSTGRES_PASSWORD=library
depends_on:
- postgres
volumes:
postgres-data:

122
pom.xml
View File

@@ -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,9 +17,15 @@
<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></code.coverage.exclusions>
<code.coverage.exclusions>
**/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>
<dependencies>
@@ -27,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>
@@ -47,6 +57,11 @@
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
@@ -54,6 +69,21 @@
<scope>runtime</scope>
<version>1.4.200</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.4.1</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>com.sendgrid</groupId>
<artifactId>sendgrid-java</artifactId>
<version>4.4.8</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
@@ -61,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>
@@ -78,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>
@@ -103,6 +142,10 @@
</executions>
<configuration>
<reportsDirectory>${surefire.and.failsafe.report.dir}</reportsDirectory>
<includes>
<include>**/*ITCase.java</include>
<include>**/*ComponentTest.java</include>
</includes>
</configuration>
</plugin>
<plugin>
@@ -110,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>
@@ -156,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>
@@ -202,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>
@@ -229,4 +317,12 @@
</build>
</profile>
</profiles>
<distributionManagement>
<repository>
<id>github</id>
<name>Library REST application (hexagonal)</name>
<url>https://maven.pkg.github.com/wkrzywiec/library-hexagonal</url>
</repository>
</distributionManagement>
</project>

View File

@@ -1,83 +0,0 @@
package io.wkrzywiec.hexagonal.library.application;
import io.restassured.RestAssured;
import io.restassured.response.ValidatableResponse;
import io.wkrzywiec.hexagonal.library.TestData;
import io.wkrzywiec.hexagonal.library.domain.book.model.BookDetailsDTO;
import io.wkrzywiec.hexagonal.library.domain.book.model.ExternalBookIdDTO;
import io.wkrzywiec.hexagonal.library.infrastructure.repository.BookEntity;
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.http.HttpStatus;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AddNewBookTest {
@LocalServerPort
private int port;
@Autowired
private JdbcTemplate jdbc;
private String baseURL;
@BeforeEach
public void init(){
this.baseURL = "http://localhost:" + port;
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
}
@Test
@DisplayName("Search for a new book in Google Books")
public void whenSearchForBook_thenGetList(){
//when
ValidatableResponse response = given()
.when()
.param("query", "lean startup")
.get( baseURL + "/google/books")
.prettyPeek()
.then();
//then
response.statusCode(HttpStatus.OK.value())
.contentType("application/json")
.body("items.size()", greaterThan(0));
}
@Test
@DisplayName("Add new book to a database")
public void givenGoogleBooId_whenAddNewBook_thenBookIsSaved(){
//given
BookDetailsDTO homoDeusBookDetails = TestData.homoDeusBookDetailsDTO();
ExternalBookIdDTO googleBookId =
ExternalBookIdDTO.builder()
.value(homoDeusBookDetails.getBookExternalId())
.build();
//when
ValidatableResponse response =
given()
.contentType("application/json")
.body(googleBookId)
.when()
.post( baseURL + "/books")
.prettyPeek()
.then();
//then
String homoDeusSql = "select * from book where book_external_id = '" + homoDeusBookDetails.getBookExternalId() + "'";
BookEntity savedBook = (BookEntity) jdbc.queryForObject(homoDeusSql, new BeanPropertyRowMapper(BookEntity.class));
assertEquals(homoDeusBookDetails.getTitle(), savedBook.getTitle());
assertEquals(homoDeusBookDetails.getTitle(), savedBook.getTitle());
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,63 @@
package io.wkrzywiec.hexagonal.library.domain.inventory;
import io.restassured.response.ValidatableResponse;
import io.wkrzywiec.hexagonal.library.BookTestData;
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.http.HttpStatus;
import org.springframework.test.context.jdbc.Sql;
import static io.restassured.RestAssured.given;
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;
public class AddNewBookComponentTest extends BaseComponentTest {
@Test
@DisplayName("Search for a new book in Google Books")
public void whenSearchForBook_thenGetList(){
//when
ValidatableResponse response = given()
.when()
.param("query", "lean startup")
.get( baseURL + "/google/books")
.prettyPeek()
.then();
//then
response.statusCode(HttpStatus.OK.value())
.contentType("application/json")
.body("items.size()", greaterThan(0));
}
@Test
@DisplayName("Add new book to a database & make it available")
@Sql(scripts = "/clean-database.sql", executionPhase = AFTER_TEST_METHOD)
public void givenGoogleBooId_whenAddNewBook_thenBookIsSaved() {
//given
AddNewBookCommand addNewBookCommand =
AddNewBookCommand.builder()
.googleBookId(BookTestData.homoDeusBookGoogleId())
.build();
//when
given()
.contentType("application/json")
.body(addNewBookCommand)
.when()
.post( baseURL + "/books")
.prettyPeek()
.then();
//then
Long savedBookId = databaseHelper.getHomoDeusBookId();
assertTrue(savedBookId > 0);
Long availableBookId = databaseHelper.getPrimaryKeyOfAvailableByBookBy(savedBookId);
assertTrue(availableBookId > 0);
}
}

View File

@@ -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);
}
}

View File

@@ -4,4 +4,4 @@ spring:
driverClassName: org.h2.Driver
username: sa
password: password
jpa.database-platform: org.hibernate.dialect.H2Dialect
jpa.database-platform: org.hibernate.dialect.H2Dialect

View File

@@ -1,9 +1,24 @@
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,
BorrowingDomainConfig.class,
EmailDomainConfig.class,
UserDomainConfig.class
})
public class LibraryHexagonalApplication {
public static void main(String[] args) {

View File

@@ -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";
}
}

View File

@@ -1,11 +0,0 @@
package io.wkrzywiec.hexagonal.library.domain.book;
import lombok.Builder;
@Builder
class Book {
long id;
String title;
}

View File

@@ -1,24 +0,0 @@
package io.wkrzywiec.hexagonal.library.domain.book;
import io.wkrzywiec.hexagonal.library.domain.book.model.BookDetailsDTO;
import io.wkrzywiec.hexagonal.library.domain.book.model.ExternalBookIdDTO;
import io.wkrzywiec.hexagonal.library.domain.book.ports.incoming.AddNewBook;
import io.wkrzywiec.hexagonal.library.domain.book.ports.incoming.GetBookDetails;
import io.wkrzywiec.hexagonal.library.domain.book.ports.outgoing.BookDatabase;
public class BookFacade implements AddNewBook {
private BookDatabase database;
private GetBookDetails getBookDetails;
public BookFacade(BookDatabase database, GetBookDetails getBookDetails) {
this.database = database;
this.getBookDetails = getBookDetails;
}
@Override
public void handle(ExternalBookIdDTO externalBookIdDTO){
BookDetailsDTO bookDetails = getBookDetails.handle(externalBookIdDTO.getValue());
database.save(bookDetails);
}
}

View File

@@ -1,24 +0,0 @@
package io.wkrzywiec.hexagonal.library.domain.book.model;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Value;
import java.util.List;
@Value
@Builder
public class BookDetailsDTO {
private String bookExternalId;
private String isbn10;
private String isbn13;
private String title;
private List<String> authors;
private String publisher;
private String publishedDate;
private String description;
private int pages;
@EqualsAndHashCode.Exclude
private String imageLink;
}

View File

@@ -1,15 +0,0 @@
package io.wkrzywiec.hexagonal.library.domain.book.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ExternalBookIdDTO {
private String value;
}

View File

@@ -1,7 +0,0 @@
package io.wkrzywiec.hexagonal.library.domain.book.ports.incoming;
import io.wkrzywiec.hexagonal.library.domain.book.model.ExternalBookIdDTO;
public interface AddNewBook {
void handle(ExternalBookIdDTO externalBookIdDTO);
}

View File

@@ -1,7 +0,0 @@
package io.wkrzywiec.hexagonal.library.domain.book.ports.incoming;
import io.wkrzywiec.hexagonal.library.domain.book.model.BookDetailsDTO;
public interface GetBookDetails {
BookDetailsDTO handle(String bookId);
}

View File

@@ -1,8 +0,0 @@
package io.wkrzywiec.hexagonal.library.domain.book.ports.outgoing;
import io.wkrzywiec.hexagonal.library.domain.book.model.BookDetailsDTO;
public interface BookDatabase {
void save(BookDetailsDTO newBookDTO);
}

View File

@@ -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);
}
}
}

View File

@@ -0,0 +1,22 @@
package io.wkrzywiec.hexagonal.library.domain.borrowing.application;
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;
@RequiredArgsConstructor
@Component
public class NewBookWasAddedEventHandler {
@Qualifier("MakeBookAvailable")
private final MakeBookAvailable makeBookAvailable;
@EventListener
public void handle(NewBookWasAddedEvent event){
makeBookAvailable.handle(new MakeBookAvailableCommand(event.getBookIdAsLong()));
}
}

View File

@@ -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();
}
}

View File

@@ -0,0 +1,5 @@
package io.wkrzywiec.hexagonal.library.domain.borrowing.application.model;
public enum BookStatus {
AVAILABLE, RESERVED, BORROWED
}

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,18 @@
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode
public class AvailableBook implements Book {
private final Long id;
public AvailableBook(Long id) {
this.id = id;
}
@Override
public Long getIdAsLong() {
return id;
}
}

View File

@@ -0,0 +1,5 @@
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
interface Book {
Long getIdAsLong();
}

View File

@@ -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 BookReservationCommand {
private Long bookId;
private Long userId;
}

View File

@@ -0,0 +1,35 @@
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
import java.time.Instant;
public class BookReservedEvent {
private final ReservationId reservationId;
private final Long userId;
private final ReservedBook reservedBook;
private final Instant timestamp;
public BookReservedEvent(ReservationDetails reservationDetails) {
this.reservationId = reservationDetails.getReservationId();
this.userId = reservationDetails.getReservedBook().getAssignedUserIdAsLong();
this.reservedBook = reservationDetails.getReservedBook();
timestamp = Instant.now();
}
public Long getReservationIdAsLong() {
return reservationId.getIdAsLong();
}
public Long getUserIdAsLong() {
return userId;
}
public Long getBookIdAsLong() {
return reservedBook.getIdAsLong();
}
public String getEventTimeStampAsString() {
return timestamp.toString();
}
}

View File

@@ -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;
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}

View File

@@ -0,0 +1,12 @@
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
@AllArgsConstructor
@Getter
@Builder
public class MakeBookAvailableCommand {
private Long bookId;
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,17 @@
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
import lombok.EqualsAndHashCode;
import lombok.Getter;
@Getter
@EqualsAndHashCode
public class ReservationDetails {
private final ReservationId reservationId;
private final ReservedBook reservedBook;
public ReservationDetails(ReservationId reservationId, ReservedBook reservedBook) {
this.reservationId = reservationId;
this.reservedBook = reservedBook;
}
}

View File

@@ -0,0 +1,16 @@
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode
public class ReservationId {
private final Long id;
public ReservationId(Long id) {
this.id = id;
}
public Long getIdAsLong(){
return id;
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,10 @@
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.exception;
public class ActiveUserNotFoundException extends RuntimeException {
public ActiveUserNotFoundException(Long bookId){
super("There is no active user with an ID: " + bookId,
null,
false,
false);
}
}

View File

@@ -0,0 +1,10 @@
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.exception;
public class AvailableBookNotFoundExeption extends RuntimeException {
public AvailableBookNotFoundExeption(Long bookId){
super("There is no available book with an ID: " + bookId,
null,
false,
false);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,10 @@
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.model.exception;
public class TooManyBooksAssignedToUserException extends RuntimeException {
public TooManyBooksAssignedToUserException(Long userId){
super("You can't assign another book to user account: " + userId + ". Reason: Too many books already assigned.",
null,
false,
false);
}
}

View File

@@ -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);
}

View File

@@ -0,0 +1,5 @@
package io.wkrzywiec.hexagonal.library.domain.borrowing.core.ports.incoming;
public interface CancelOverdueReservations {
void cancelOverdueReservations();
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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<>();
}
}
}

View File

@@ -0,0 +1,19 @@
package io.wkrzywiec.hexagonal.library.domain.borrowing.infrastructure;
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;
@Component
@RequiredArgsConstructor
public class SpringBorrowingEventPublisherAdapter implements BorrowingEventPublisher {
private final ApplicationEventPublisher eventPublisher;
@Override
public void publish(BookReservedEvent event) {
eventPublisher.publishEvent(event);
}
}

View File

@@ -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;
}

View File

@@ -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()
);
}
}

View File

@@ -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()
);
}
}

View File

@@ -0,0 +1,24 @@
package io.wkrzywiec.hexagonal.library.domain.email.application;
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;
@Component
@RequiredArgsConstructor
public class BookReservedEventHandler {
private final SendReservationConfirmation sendReservationConfirmation;
@EventListener
public void handle(BookReservedEvent event) {
sendReservationConfirmation.handle(
new SendReservationConfirmationCommand(
event.getReservationIdAsLong(),
event.getUserIdAsLong(),
event.getBookIdAsLong()));
}
}

View File

@@ -0,0 +1,18 @@
package io.wkrzywiec.hexagonal.library.domain.email.core;
import io.wkrzywiec.hexagonal.library.domain.email.core.model.EmailAddress;
import io.wkrzywiec.hexagonal.library.domain.email.core.model.ReservationConfirmEmail;
class EmailCreator {
static ReservationConfirmEmail reservationEmail(Long reservationId, String bookTitle, String emailTo){
EmailAddress from = new EmailAddress("tom@library.com");
EmailAddress to = new EmailAddress(emailTo);
String subject = String.format("Library - book reservation confirmation (id - %d)", reservationId);
String content = String.format("Dear reader,%n you have reserved a %s book which will be waiting for you in our library for next 2 days. Your reservation id is %d. %n Have a nice day, %n Library",
bookTitle, reservationId);
return new ReservationConfirmEmail(from, to, subject, content);
}
}

View File

@@ -0,0 +1,32 @@
package io.wkrzywiec.hexagonal.library.domain.email.core;
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
public class EmailFacade implements SendReservationConfirmation {
private final EmailSender emailSender;
private final EmailDatabase database;
@Override
public void handle(SendReservationConfirmationCommand sendReservationConfirmation) {
String bookTitle = database
.getTitleByBookId(sendReservationConfirmation.getBookId())
.orElseThrow(() -> new IllegalArgumentException("Can't get book title from database. Reason: there is no book with an id: " + sendReservationConfirmation.getBookId()));
String userEmailAddress = database
.getUserEmailAddress(sendReservationConfirmation.getUserId())
.orElseThrow(() -> new IllegalArgumentException("Can't get email address from database. Reason: there is no user with an id: " + sendReservationConfirmation.getUserId()));
ReservationConfirmEmail reservationConfirmEmail = EmailCreator.reservationEmail(
sendReservationConfirmation.getReservationId(),
bookTitle,
userEmailAddress
);
emailSender.sendReservationConfirmationEmail(reservationConfirmEmail);
}
}

View File

@@ -0,0 +1,24 @@
package io.wkrzywiec.hexagonal.library.domain.email.core.model;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailAddress {
private final String value;
public EmailAddress(String value) {
Pattern pattern = Pattern.compile("^(.+)@(.+)$");
Matcher matcher = pattern.matcher(value);
if(matcher.matches()){
this.value = value;
} else {
throw new IllegalArgumentException("Provided value is not an email address");
}
}
public String getAsString() {
return value;
}
}

View File

@@ -0,0 +1,28 @@
package io.wkrzywiec.hexagonal.library.domain.email.core.model;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class ReservationConfirmEmail {
private final EmailAddress from;
private final EmailAddress to;
private final String subject;
private final String content;
public String getFromEmailAddressAsString(){
return this.from.getAsString();
}
public String getToEmailAddressAsString(){
return this.to.getAsString();
}
public String getSubjectAsString(){
return this.subject;
}
public String getContentAsString(){
return this.content;
}
}

View File

@@ -0,0 +1,17 @@
package io.wkrzywiec.hexagonal.library.domain.email.core.model;
import lombok.Getter;
@Getter
public class SendReservationConfirmationCommand {
private final Long reservationId;
private final Long userId;
private final Long bookId;
public SendReservationConfirmationCommand(Long reservationId, Long userId, Long bookId) {
this.reservationId = reservationId;
this.userId = userId;
this.bookId = bookId;
}
}

View File

@@ -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);
}

View File

@@ -0,0 +1,8 @@
package io.wkrzywiec.hexagonal.library.domain.email.core.ports.outgoing;
import java.util.Optional;
public interface EmailDatabase {
Optional<String> getTitleByBookId(Long bookId);
Optional<String> getUserEmailAddress(Long userId);
}

View File

@@ -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);
}

View File

@@ -0,0 +1,38 @@
package io.wkrzywiec.hexagonal.library.domain.email.infrastructure;
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;
import java.util.Optional;
@RequiredArgsConstructor
public class EmailDatabaseAdapter implements EmailDatabase {
private final JdbcTemplate jdbcTemplate;
@Override
public Optional<String> getTitleByBookId(Long bookId) {
try {
return Optional.ofNullable(jdbcTemplate.queryForObject(
"SELECT title FROM book WHERE id = ?",
String.class,
bookId));
} catch (DataAccessException ex){
return Optional.empty();
}
}
@Override
public Optional<String> getUserEmailAddress(Long userId) {
try {
return Optional.ofNullable(jdbcTemplate.queryForObject(
"SELECT email FROM library_user WHERE id = ?",
String.class,
userId));
} catch (DataAccessException ex){
return Optional.empty();
}
}
}

View File

@@ -0,0 +1,38 @@
package io.wkrzywiec.hexagonal.library.domain.email.infrastructure;
import com.sendgrid.Method;
import com.sendgrid.Request;
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.domain.email.core.model.ReservationConfirmEmail;
import io.wkrzywiec.hexagonal.library.domain.email.core.ports.outgoing.EmailSender;
import java.io.IOException;
public class SendGridEmailSender implements EmailSender {
@Override
public void sendReservationConfirmationEmail(ReservationConfirmEmail reservationConfirmEmail) {
Email from = new Email(reservationConfirmEmail.getFromEmailAddressAsString());
Email to = new Email(reservationConfirmEmail.getToEmailAddressAsString());
Content content = new Content("text/plain", reservationConfirmEmail.getContentAsString());
Mail mail = new Mail(
from,
reservationConfirmEmail.getSubjectAsString(),
to,
content);
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
Request request = new Request();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());
sg.api(request);
} catch (IOException ex) {
System.out.print(ex);
}
}
}

View File

@@ -1,7 +1,7 @@
package io.wkrzywiec.hexagonal.library.application;
package io.wkrzywiec.hexagonal.library.domain.inventory.application;
import io.wkrzywiec.hexagonal.library.domain.book.model.ExternalBookIdDTO;
import io.wkrzywiec.hexagonal.library.domain.book.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;
@@ -18,8 +18,8 @@ public class BookCommandController {
private final AddNewBook addNewBook;
@PostMapping("")
public ResponseEntity<String> addNewBook(@RequestBody ExternalBookIdDTO externalBookIdDTO){
addNewBook.handle(externalBookIdDTO);
public ResponseEntity<String> addNewBook(@RequestBody AddNewBookCommand addNewBookCommand){
addNewBook.handle(addNewBookCommand);
return new ResponseEntity<>("New book was added to library", HttpStatus.CREATED);
}
}

View File

@@ -0,0 +1,30 @@
package io.wkrzywiec.hexagonal.library.domain.inventory.core;
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{
private InventoryDatabase database;
private GetBookDetails getBookDetails;
private InventoryEventPublisher eventPublisher;
public InventoryFacade(InventoryDatabase database, GetBookDetails getBookDetails, InventoryEventPublisher eventPublisher) {
this.database = database;
this.getBookDetails = getBookDetails;
this.eventPublisher = eventPublisher;
}
@Override
public void handle(AddNewBookCommand addNewBookCommand){
Book book = getBookDetails.handle(addNewBookCommand.getGoogleBookId());
Book savedBook = database.save(book);
eventPublisher.publishNewBookWasAddedEvent(new NewBookWasAddedEvent(savedBook.getIdAsLong()));
}
}

View File

@@ -0,0 +1,14 @@
package io.wkrzywiec.hexagonal.library.domain.inventory.core.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class AddNewBookCommand {
private String googleBookId;
}

View File

@@ -1,6 +1,7 @@
package io.wkrzywiec.hexagonal.library.infrastructure.repository;
package io.wkrzywiec.hexagonal.library.domain.inventory.core.model;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import javax.persistence.Column;
import javax.persistence.Entity;
@@ -9,10 +10,11 @@ import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Builder
@Entity
@Table(name="author")
public class AuthorEntiy {
@EqualsAndHashCode
@ToString
public class Author {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@@ -21,4 +23,11 @@ public class AuthorEntiy {
@Column(name="name", unique=true)
private String name;
public Author(String name) {
this.name = name;
}
private Author() {
}
}

View File

@@ -1,13 +1,10 @@
package io.wkrzywiec.hexagonal.library.infrastructure.repository;
package io.wkrzywiec.hexagonal.library.domain.inventory.core.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.EqualsAndHashCode;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
@@ -17,31 +14,20 @@ import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import java.util.Set;
@Builder
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
@Table(name="book")
public class BookEntity {
@EqualsAndHashCode
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name="book_external_id")
private String bookExternalId;
@Column(name="isbn_10")
private String isbn10;
@Column(name="isbn_13")
private String isbn13;
@Embedded
private BookIdentification identification;
@Column(name="title")
private String title;
@@ -52,7 +38,7 @@ public class BookEntity {
name="book_author",
joinColumns=@JoinColumn(name="book_id"),
inverseJoinColumns=@JoinColumn(name="author_id"))
private Set<AuthorEntiy> authors;
private Set<Author> authors;
@Column(name="publisher")
private String publisher;
@@ -63,9 +49,28 @@ public class BookEntity {
@Column(name="description", columnDefinition="TEXT")
private String description;
@Column(name="pages")
@Column(name="page_count")
private int pages;
@Column(name="imageLink", columnDefinition="TEXT")
@EqualsAndHashCode.Exclude
private String imageLink;
public Book(BookIdentification identification, String title, Set<Author> authors, String publisher, String publishedDate, String description, int pages, String imageLink) {
this.identification = identification;
this.title = title;
this.authors = authors;
this.publisher = publisher;
this.publishedDate = publishedDate;
this.description = description;
this.pages = pages;
this.imageLink = imageLink;
}
public Long getIdAsLong(){
return id;
}
private Book() {
}
}

View File

@@ -0,0 +1,30 @@
package io.wkrzywiec.hexagonal.library.domain.inventory.core.model;
import lombok.EqualsAndHashCode;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Embedded;
@Embeddable
@EqualsAndHashCode
public class BookIdentification {
@Column(name="book_external_id")
private String bookExternalId;
@Embedded
private Isbn10 isbn10;
@Embedded
private Isbn13 isbn13;
public BookIdentification(String bookExternalId, Isbn10 isbn10, Isbn13 isbn13) {
this.bookExternalId = bookExternalId;
this.isbn10 = isbn10;
this.isbn13 = isbn13;
}
private BookIdentification() {
}
}

View File

@@ -0,0 +1,28 @@
package io.wkrzywiec.hexagonal.library.domain.inventory.core.model;
import lombok.EqualsAndHashCode;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@EqualsAndHashCode
@Embeddable
public class Isbn10 {
@Column(name="isbn_10")
private String value;
public Isbn10(String value) {
if (value.matches("\\d{10}")){
this.value = value;
} else {
throw new IllegalArgumentException("ISBN-10 should have 10 digits only.");
}
}
private Isbn10() {
}
public String getAsString(){
return value;
}
}

View File

@@ -0,0 +1,29 @@
package io.wkrzywiec.hexagonal.library.domain.inventory.core.model;
import lombok.EqualsAndHashCode;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@EqualsAndHashCode
@Embeddable
public class Isbn13 {
@Column(name="isbn_13")
private String value;
public Isbn13(String value) {
if (value.matches("\\d{13}")){
this.value = value;
} else {
throw new IllegalArgumentException("ISBN-13 should have 10 digits only.");
}
}
private Isbn13() {
}
public String getAsString(){
return value;
}
}

View File

@@ -0,0 +1,22 @@
package io.wkrzywiec.hexagonal.library.domain.inventory.core.model;
import java.time.Instant;
public class NewBookWasAddedEvent {
private final Long bookId;
private final Instant timestamp;
public NewBookWasAddedEvent(Long bookId) {
this.bookId = bookId;
timestamp = Instant.now();
}
public Long getBookIdAsLong() {
return bookId;
}
public String getEventTimeStampAsString() {
return timestamp.toString();
}
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -0,0 +1,9 @@
package io.wkrzywiec.hexagonal.library.domain.inventory.infrastructure;
import io.wkrzywiec.hexagonal.library.domain.inventory.core.model.Book;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface BookRepository extends CrudRepository<Book, Long> {
}

View File

@@ -1,19 +1,21 @@
package io.wkrzywiec.hexagonal.library.infrastructure.adapter;
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.domain.book.model.BookDetailsDTO;
import io.wkrzywiec.hexagonal.library.domain.book.ports.incoming.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;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@@ -27,10 +29,9 @@ public class GoogleBooksAdapter implements GetBookDetails {
private final RestTemplate restTemplate;
@Override
public BookDetailsDTO handle(String googleBookId) {
public Book handle(String googleBookId) {
HttpHeaders requestHeader = new HttpHeaders();
// requestHeader.add("Accept", MediaType.ALL_VALUE);
HttpEntity<Object> requestEntity = new HttpEntity<>(requestHeader);
ResponseEntity<String> responseEntity =
@@ -46,18 +47,19 @@ public class GoogleBooksAdapter implements GetBookDetails {
JsonObject volumeInfo = response.getAsJsonObject("volumeInfo");
return BookDetailsDTO.builder()
.bookExternalId(googleBookId)
.isbn10(extractIsbn(volumeInfo, "ISBN_10"))
.isbn13(extractIsbn(volumeInfo, "ISBN_13"))
.title(volumeInfo.get("title").getAsString())
.authors(extractAuthors(volumeInfo))
.publisher(volumeInfo.get("publisher").getAsString())
.publishedDate(volumeInfo.get("publishedDate").getAsString())
.description(volumeInfo.get("description").getAsString())
.pages(volumeInfo.get("pageCount").getAsInt())
.imageLink(extractImage(volumeInfo))
.build();
Isbn10 isbn10 = new Isbn10(extractIsbn(volumeInfo, "ISBN_10"));
Isbn13 isbn13 = new Isbn13(extractIsbn(volumeInfo, "ISBN_13"));
return new Book(
new BookIdentification(googleBookId, isbn10, isbn13),
volumeInfo.get("title").getAsString(),
extractAuthors(volumeInfo),
volumeInfo.get("publisher").getAsString(),
volumeInfo.get("publishedDate").getAsString(),
volumeInfo.get("description").getAsString(),
volumeInfo.get("pageCount").getAsInt(),
extractImage(volumeInfo)
);
}
private String extractIsbn(JsonObject volumeInfo, String isbnType) {
@@ -74,7 +76,7 @@ public class GoogleBooksAdapter implements GetBookDetails {
.orElseThrow(() -> new RuntimeException("Inside volumeInfo there is no " + isbnType));
}
private List<String> extractAuthors(JsonObject volumeInfo) {
private Set<Author> extractAuthors(JsonObject volumeInfo) {
return StreamSupport.stream(
ofNullable(volumeInfo)
.map(volume -> volume.getAsJsonArray("authors"))
@@ -82,7 +84,8 @@ public class GoogleBooksAdapter implements GetBookDetails {
.spliterator(),
false)
.map(JsonElement::getAsString)
.collect(Collectors.toList());
.map(Author::new)
.collect(Collectors.toSet());
}
private String extractImage(JsonObject volumeInfo) {

View File

@@ -0,0 +1,16 @@
package io.wkrzywiec.hexagonal.library.domain.inventory.infrastructure;
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
public class InventoryDatabaseAdapter implements InventoryDatabase {
private final BookRepository repository;
@Override
public Book save(Book book) {
return repository.save(book);
}
}

View File

@@ -0,0 +1,17 @@
package io.wkrzywiec.hexagonal.library.domain.inventory.infrastructure;
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;
@RequiredArgsConstructor
public class SpringInventoryEventPublisherAdapter implements InventoryEventPublisher {
private final ApplicationEventPublisher applicationEventPublisher;
@Override
public void publishNewBookWasAddedEvent(NewBookWasAddedEvent event) {
applicationEventPublisher.publishEvent(event);
}
}

View File

@@ -0,0 +1,25 @@
package io.wkrzywiec.hexagonal.library.domain.user.application;
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;
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("/users")
@RequiredArgsConstructor
public class UserCommandController {
private final AddNewUser addNewUser;
@PostMapping("")
public ResponseEntity<String> addNewUser(@RequestBody AddUserCommand addUserCommand){
addNewUser.handle(addUserCommand);
return new ResponseEntity<>("New user was added to library", HttpStatus.CREATED);
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,17 @@
package io.wkrzywiec.hexagonal.library.domain.user.core.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class AddUserCommand {
private String email;
private String firstName;
private String lastName;
}

View File

@@ -0,0 +1,28 @@
package io.wkrzywiec.hexagonal.library.domain.user.core.model;
import lombok.EqualsAndHashCode;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Embeddable
@EqualsAndHashCode
public class EmailAddress {
@Column(name="email")
private String value;
public EmailAddress(String value) {
Pattern pattern = Pattern.compile("^(.+)@(.+)$");
Matcher matcher = pattern.matcher(value);
if(matcher.matches()){
this.value = value;
} else {
throw new IllegalArgumentException("Provided value is not an email address");
}
}
private EmailAddress(){}
}

View File

@@ -0,0 +1,43 @@
package io.wkrzywiec.hexagonal.library.domain.user.core.model;
import lombok.EqualsAndHashCode;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="library_user")
@EqualsAndHashCode
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@EqualsAndHashCode.Exclude
private Long id;
@Embedded
private EmailAddress emailAddress;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
public User(EmailAddress emailAddress, String firstName, String lastName) {
this.emailAddress = emailAddress;
this.firstName = firstName;
this.lastName = lastName;
}
public Long getIdentifierAsLong(){
return id;
}
private User(){}
}

View File

@@ -0,0 +1,15 @@
package io.wkrzywiec.hexagonal.library.domain.user.core.model;
public class UserIdentifier {
private final Long id;
public UserIdentifier(Long id) {
this.id = id;
}
public Long getAsLong(){
return id;
}
}

View File

@@ -0,0 +1,8 @@
package io.wkrzywiec.hexagonal.library.domain.user.core.ports.incoming;
import io.wkrzywiec.hexagonal.library.domain.user.core.model.AddUserCommand;
import io.wkrzywiec.hexagonal.library.domain.user.core.model.UserIdentifier;
public interface AddNewUser {
UserIdentifier handle(AddUserCommand addUserCommand);
}

View File

@@ -0,0 +1,8 @@
package io.wkrzywiec.hexagonal.library.domain.user.core.ports.outgoing;
import io.wkrzywiec.hexagonal.library.domain.user.core.model.User;
import io.wkrzywiec.hexagonal.library.domain.user.core.model.UserIdentifier;
public interface UserDatabase {
UserIdentifier save(User user);
}

View File

@@ -0,0 +1,18 @@
package io.wkrzywiec.hexagonal.library.domain.user.infrastructure;
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.outgoing.UserDatabase;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class UserDatabaseAdapter implements UserDatabase {
private final UserRepository userRepository;
@Override
public UserIdentifier save(User user) {
User savedUser = userRepository.save(user);
return new UserIdentifier(savedUser.getIdentifierAsLong());
}
}

View File

@@ -0,0 +1,9 @@
package io.wkrzywiec.hexagonal.library.domain.user.infrastructure;
import io.wkrzywiec.hexagonal.library.domain.user.core.model.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
}

View File

@@ -1,36 +0,0 @@
package io.wkrzywiec.hexagonal.library.infrastructure;
import io.wkrzywiec.hexagonal.library.domain.book.BookFacade;
import io.wkrzywiec.hexagonal.library.domain.book.ports.incoming.AddNewBook;
import io.wkrzywiec.hexagonal.library.domain.book.ports.incoming.GetBookDetails;
import io.wkrzywiec.hexagonal.library.domain.book.ports.outgoing.BookDatabase;
import io.wkrzywiec.hexagonal.library.infrastructure.adapter.BookDatabaseAdapter;
import io.wkrzywiec.hexagonal.library.infrastructure.adapter.GoogleBooksAdapter;
import io.wkrzywiec.hexagonal.library.infrastructure.repository.PostgresBookRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class BookDomainConfig {
@Bean
RestTemplate restTemplate(){
return new RestTemplate();
}
@Bean
BookDatabase bookRepository(PostgresBookRepository repository){
return new BookDatabaseAdapter(repository);
}
@Bean
GetBookDetails getBookDetails(RestTemplate restTemplate){
return new GoogleBooksAdapter(restTemplate);
}
@Bean
AddNewBook addNewBook(BookDatabase database, GetBookDetails getBookDetails){
return new BookFacade(database, getBookDetails);
}
}

View File

@@ -0,0 +1,59 @@
package io.wkrzywiec.hexagonal.library.infrastructure;
import io.wkrzywiec.hexagonal.library.domain.borrowing.core.BorrowingFacade;
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 io.wkrzywiec.hexagonal.library.domain.borrowing.infrastructure.BorrowingDatabaseAdapter;
import io.wkrzywiec.hexagonal.library.domain.borrowing.infrastructure.SpringBorrowingEventPublisherAdapter;
import org.springframework.beans.factory.annotation.Qualifier;
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
@Qualifier("MakeBookAvailable")
public MakeBookAvailable makeBookAvailable(BorrowingDatabase database, BorrowingEventPublisher borrowingEventPublisher) {
return new BorrowingFacade(database, borrowingEventPublisher);
}
@Bean
@Qualifier("ReserveBook")
public ReserveBook reserveBook(BorrowingDatabase database, BorrowingEventPublisher borrowingEventPublisher){
return new BorrowingFacade(database, borrowingEventPublisher);
}
@Bean
@Qualifier("BorrowBook")
public BorrowBook borrowBook(BorrowingDatabase database, BorrowingEventPublisher borrowingEventPublisher){
return new BorrowingFacade(database, borrowingEventPublisher);
}
@Bean
@Qualifier("GiveBackBook")
public GiveBackBook giveBackBook(BorrowingDatabase database, BorrowingEventPublisher borrowingEventPublisher){
return new BorrowingFacade(database, borrowingEventPublisher);
}
@Bean
@Qualifier("CancelOverdueReservations")
public CancelOverdueReservations cancelOverdueReservations(BorrowingDatabase database, BorrowingEventPublisher borrowingEventPublisher){
return new BorrowingFacade(database, borrowingEventPublisher);
}
}

Some files were not shown because too many files have changed in this diff Show More