Skip to content

Commit

Permalink
Merge pull request #3 from kiyotakeshi/persist-to-DB
Browse files Browse the repository at this point in the history
Persist to db
  • Loading branch information
kiyotakeshi committed Jan 22, 2023
2 parents 04bbec1 + ccbf7d3 commit 0b81cfd
Show file tree
Hide file tree
Showing 20 changed files with 836 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -35,3 +35,4 @@ out/

### VS Code ###
.vscode/
.docker
77 changes: 76 additions & 1 deletion README.md
Expand Up @@ -19,4 +19,79 @@ $ java -version
openjdk version "17.0.3.1" 2022-04-22 LTS
OpenJDK Runtime Environment (build 17.0.3.1+2-LTS)
OpenJDK 64-Bit Server VM (build 17.0.3.1+2-LTS, mixed mode, sharing)
```
```

## setup DB

- run Postgres

```shell
$ docker compose up -d
```

- exec DDL

```sql
drop table if exists customer_role;
drop sequence if exists customer_role_id_seq;

drop table if exists customers;
drop sequence if exists customer_id_seq;

create sequence customer_id_seq;
create table customers
(
id int not null default nextval('customer_id_seq') primary key,
email varchar(50) not null unique,
password varchar(500) not null
);

drop table if exists roles;
drop sequence if exists role_id_seq;

create sequence role_id_seq;
create table roles
(
id int not null default nextval('role_id_seq') primary key,
name varchar(50) not null unique
);

create sequence customer_role_id_seq;
create table customer_role
(
id int not null default nextval('customer_role_id_seq') primary key,
customer_id int not null,
role_id int not null,
constraint fk_customer_role_customer_id foreign key (customer_id) references customers (id),
constraint fk_customer_role_role_id foreign key (role_id) references roles (id)
);

-- initial data for admin user
insert into roles (id, name)
values (default, 'ADMIN'),
(default, 'USER');

insert into customers (id, email, password)
values (default, 'admin@example.com', '$2a$10$ancDG4fEZY31a9OtuqSbs./SPUu7s00qam5sXinI5NrTLSGlCy/BK');

insert into customer_role (customer_id, role_id)
values ((select c.id from customers c where c.email = 'admin@example.com'),
(select r.id from roles r where r.name = 'ADMIN')),
((select c.id from customers c where c.email = 'admin@example.com'),
(select r.id from roles r where r.name = 'USER'));

select c.*, r.name
from customers c
left join customer_role cr on c.id = cr.customer_id
left join roles r on cr.role_id = r.id;
```

## generate code for jOOQ

```shell
$ ./gradlew generateJooq
```

## test API

[use Postman collections](./postman/spring-security-zenn-ariticle.postman_collection.json)
57 changes: 57 additions & 0 deletions build.gradle.kts
Expand Up @@ -3,6 +3,9 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "3.0.1"
id("io.spring.dependency-management") version "1.1.0"
// https://github.com/etiennestuder/gradle-jooq-plugin#compatibility
id("nu.studer.jooq") version "8.1"

kotlin("jvm") version "1.7.22"
kotlin("plugin.spring") version "1.7.22"
}
Expand All @@ -19,14 +22,68 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-security")

implementation("org.springframework.boot:spring-boot-starter-jooq")

implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

runtimeOnly("org.postgresql:postgresql")

testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.security:spring-security-test")

jooqGenerator("org.postgresql:postgresql")
// https://github.com/etiennestuder/gradle-jooq-plugin/issues/207
jooqGenerator("jakarta.xml.bind:jakarta.xml.bind-api:3.0.1")
}

jooq {
version.set(dependencyManagement.importedProperties["jooq.version"])
edition.set(nu.studer.gradle.jooq.JooqEdition.OSS)

configurations {
create("main") {
// ref https://github.com/etiennestuder/gradle-jooq-plugin#generating-the-jooq-sources
// ref https://www.greptips.com/posts/1350/#jooq-configurations
generateSchemaSourceOnCompilation.set(false)
jooqConfiguration.apply {
jdbc.apply {
driver = "org.postgresql.Driver"
// TODO: 環境変数から読み取るようにし、direnv 等で設定する
// url = System.getenv("POSTGRES_URL")
// user = System.getenv("POSTGRES_USER")
// password = System.getenv("POSTGRES_PASSWORD")
url = "jdbc:postgresql://localhost:15432/testdb"
user = "postgres"
password = "password"
}
generator.apply {
name = "org.jooq.codegen.KotlinGenerator"
database.apply {
name = "org.jooq.meta.postgres.PostgresDatabase"
inputSchema = "public"
// excludes = "flyway_schema_history"
}
generate.apply {
isDeprecated = false
isTables = true
// isRecords = true
// isImmutablePojos = true
// isFluentSetters = true
}
target.apply {
packageName = "com.example.zenn.jooq.codegen"
directory = "build/generated-src/jooq/"
}
strategy.name = "org.jooq.codegen.DefaultGeneratorStrategy"
}
}
}
}
}


tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
Expand Down
14 changes: 14 additions & 0 deletions docker-compose.yaml
@@ -0,0 +1,14 @@
services:
postgres:
image: postgres:13.9
container_name: spring-security-demo-postgres
ports:
- 15432:5432
volumes:
- ./.docker/postgres:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: testdb
POSTGRES_INITDB_ARGS: "--encoding=UTF-8"
restart: always

0 comments on commit 0b81cfd

Please sign in to comment.