Skip to content

Commit 18e2707

Browse files
authored
Merge pull request #7 from stephano-tri/feature-#3
Feature #3
2 parents 131f353 + a34ba2c commit 18e2707

File tree

9 files changed

+93
-20
lines changed

9 files changed

+93
-20
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
# copy to container
44
docker cp $dump_file $container_id:/var
55

6+
# create database
7+
docker compose exec -it $db_container bash
8+
psql -U postgres
9+
create database dvdrental;
10+
611
# user pg_restore
712
docker compose exec $db_container pg_restore -U $user -C -d postgres /var/$dump_file
813
```

docker-compose.yml

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,38 @@ services:
1818
soft: -1
1919
hard: -1
2020

21-
db:
22-
container_name: postgres
23-
image: postgres
24-
restart: always
21+
postgresql-main:
22+
image: 'bitnami/postgresql:latest'
2523
ports:
26-
- 5432:5432
24+
- '5432:5432'
25+
restart: on-failure
2726
volumes:
28-
- ./postgres-data:/var/lib/postgresql/data
27+
- postgresql_main_data:/bitnami/postgresql
28+
- ./init.sql:/docker-entrypoint-initdb. d/db.sql
2929
environment:
30-
POSTGRES_USER: postgres
31-
POSTGRES_PASSWORD: example
32-
POSTGRES_DB: postgres
30+
- POSTGRESQL_REPLICATION_MODE=master
31+
- POSTGRESQL_REPLICATION_USER=repl_user
32+
- POSTGRESQL_REPLICATION_PASSWORD=repl_password
33+
- POSTGRESQL_USERNAME=my_user
34+
- POSTGRESQL_PASSWORD=my_password
35+
- POSTGRESQL_DATABASE=ins
36+
- ALLOW_EMPTY_PASSWORD=yes
37+
38+
postgresql-standby:
39+
image: 'bitnami/postgresql:latest'
40+
ports:
41+
- '5433:5432'
42+
restart: on-failure
43+
depends_on:
44+
- postgresql-main
45+
environment:
46+
- POSTGRESQL_REPLICATION_MODE=slave
47+
- POSTGRESQL_REPLICATION_USER=repl_user
48+
- POSTGRESQL_REPLICATION_PASSWORD=repl_password
49+
- POSTGRESQL_MASTER_HOST=postgresql-main
50+
- POSTGRESQL_MASTER_PORT_NUMBER=5432
51+
- POSTGRESQL_USERNAME=my_user
52+
- POSTGRESQL_PASSWORD=my_password
3353

3454
zookeeper:
3555
image: zookeeper:3.7
@@ -145,3 +165,7 @@ services:
145165
CONNECT_LOG4J_ROOT_LOGLEVEL: INFO
146166
CONNECT_LOG4J_LOGGERS: org.reflections=ERROR
147167
CLASSPATH: /usr/share/java/monitoring-interceptors/monitoring-interceptors-3.3.0.jar
168+
169+
volumes:
170+
postgresql_main_data:
171+
driver: local

dvdrental.zip

-538 KB
Binary file not shown.

init.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO repl_user;
2+
ALTER USER REPL_USER WITH SUPERUSER;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package eom.improve.kafkaboot.model
2+
3+
import java.io.Serializable
4+
import java.time.LocalDateTime
5+
6+
data class FilmActor(
7+
val actorId: Int,
8+
val filmId: Int,
9+
val lastUpdate: LocalDateTime
10+
) : Serializable
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package eom.improve.kafkaboot.model
2+
3+
import java.io.Serializable
4+
import java.time.LocalDateTime
5+
6+
data class FilmCategory(
7+
val filmId: Int,
8+
val categoryId: Int,
9+
val lastUpdate: LocalDateTime
10+
) : Serializable
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
package eom.improve.kafkaboot.repository
22

3-
interface FilmActorRepository {
3+
import eom.improve.kafkaboot.model.FilmActor
4+
import org.springframework.data.r2dbc.repository.R2dbcRepository
5+
import reactor.core.publisher.Flux
6+
import reactor.core.publisher.Mono
7+
8+
interface FilmActorRepository : R2dbcRepository<FilmActor, Int> {
9+
fun findAllByFilmId(filmId: Int) : Flux<FilmActor>
10+
fun deleteByFilmId(filmId: Int) : Mono<Void>
411
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
package eom.improve.kafkaboot.repository
22

3-
interface FilmCategoryRepository {
3+
import eom.improve.kafkaboot.model.FilmCategory
4+
import org.springframework.data.r2dbc.repository.R2dbcRepository
5+
import reactor.core.publisher.Flux
6+
import reactor.core.publisher.Mono
7+
8+
interface FilmCategoryRepository : R2dbcRepository<FilmCategory, Int> {
9+
fun findAllByFilmId(filmId: Int) : Flux<FilmCategory>
10+
fun deleteByFilmId(filmId: Int) : Mono<Void>
411
}

src/main/kotlin/eom/improve/kafkaboot/service/FilmService.kt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package eom.improve.kafkaboot.service
22

33
import eom.improve.kafkaboot.model.FilmEntity
4-
import eom.improve.kafkaboot.repository.FilmRepository
5-
import eom.improve.kafkaboot.repository.InventoryRepository
6-
import eom.improve.kafkaboot.repository.PaymentRepository
7-
import eom.improve.kafkaboot.repository.RentalRepository
4+
import eom.improve.kafkaboot.repository.*
85
import org.springframework.stereotype.Service
9-
import org.springframework.transaction.annotation.Transactional
106
import reactor.core.publisher.Flux
117
import reactor.core.publisher.Mono
128
import reactor.kotlin.core.publisher.toMono
@@ -17,7 +13,9 @@ class FilmService(
1713
private val filmRepository : FilmRepository,
1814
private val paymentRepository : PaymentRepository,
1915
private val inventoryRepository : InventoryRepository,
20-
private val rentalRepository : RentalRepository
16+
private val rentalRepository : RentalRepository,
17+
private val filmActorRepository: FilmActorRepository,
18+
private val filmCategoryRepository: FilmCategoryRepository
2119
) {
2220
fun findAll() : Flux<FilmEntity> = filmRepository.findAllBy()
2321

@@ -31,7 +29,6 @@ class FilmService(
3129
return filmRepository.save(toBeSavedFilm);
3230
}
3331

34-
@Transactional
3532
fun deleteFilm(filmId : Int) : Mono<Void> {
3633
// need to implement cascade delete(maybe soft) for table data that set foreign key
3734
return filmRepository.findById(filmId)
@@ -52,13 +49,24 @@ class FilmService(
5249
}.then(inventoryEn.toMono())
5350
}
5451
.flatMap { inventoryEn ->
55-
println(inventoryEn.inventoryId)
5652
inventoryRepository.deleteByInventoryId(inventoryEn.inventoryId)
5753
}
5854
.then(filmEn.toMono())
5955
}
6056
.flatMap { filmEn ->
61-
filmRepository.deleteByFilmId(filmEn.filmId)
57+
Mono.zip(
58+
filmActorRepository.findAllByFilmId(filmEn.filmId)
59+
.flatMap { filmActorRepository.deleteByFilmId(it.filmId) }
60+
.then()
61+
,
62+
filmCategoryRepository.findAllByFilmId(filmEn.filmId)
63+
.flatMap { filmCategoryRepository.deleteByFilmId(it.filmId) }
64+
.then()
65+
)
66+
.then(filmEn.toMono())
67+
}
68+
.flatMap {
69+
filmRepository.deleteByFilmId(it.filmId)
6270
}
6371
}
6472
}

0 commit comments

Comments
 (0)