Skip to content

Commit

Permalink
refactor(backend): move repositories to non Reactive API
Browse files Browse the repository at this point in the history
Related to #231
  • Loading branch information
davinkevin committed Aug 3, 2024
1 parent cc902a5 commit 2ae63fe
Show file tree
Hide file tree
Showing 24 changed files with 392 additions and 635 deletions.
10 changes: 3 additions & 7 deletions backend/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import com.gitlab.davinkevin.podcastserver.database.DatabaseConfiguration
import com.gitlab.davinkevin.podcastserver.dockerimages.DockerImagesConfiguration
import com.gradle.enterprise.gradleplugin.testretry.retry
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.internal.deprecation.DeprecatableConfiguration
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
Expand Down Expand Up @@ -58,9 +57,7 @@ dependencies {
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")

implementation("org.springframework:spring-r2dbc")
implementation("org.postgresql:postgresql")
runtimeOnly("org.postgresql:r2dbc-postgresql")
implementation(project(":backend-lib-database"))

implementation("org.jdom:jdom2:2.0.6.1")
Expand All @@ -69,7 +66,6 @@ dependencies {
implementation(project(":backend-lib-youtubedl"))
implementation("net.bramp.ffmpeg:ffmpeg:0.8.0")

implementation("io.r2dbc:r2dbc-pool")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
implementation("org.jetbrains.kotlin:kotlin-reflect")

Expand Down Expand Up @@ -119,9 +115,9 @@ tasks.test {

systemProperty("user.timezone", "UTC")
project.extensions.getByType<DatabaseConfiguration>().apply {
systemProperty("spring.r2dbc.url", r2dbc())
systemProperty("spring.r2dbc.username", user)
systemProperty("spring.r2dbc.password", password)
systemProperty("spring.datasource.url", jdbc())
systemProperty("spring.datasource.username", user)
systemProperty("spring.datasource.password", password)
dependsOn(migrateDbTask)
}
jvmArgs = listOf("--add-opens", "java.base/java.time=ALL-UNNAMED")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import com.github.davinkevin.podcastserver.database.Tables.ITEM
import com.github.davinkevin.podcastserver.database.Tables.PODCAST
import com.github.davinkevin.podcastserver.database.tables.Cover.COVER
import org.jooq.DSLContext
import reactor.core.publisher.Flux
import reactor.kotlin.core.publisher.toMono
import java.net.URI
import java.time.OffsetDateTime
import java.util.*
Expand All @@ -16,45 +14,43 @@ import kotlin.io.path.extension

class CoverRepository(private val query: DSLContext) {

fun save(cover: CoverForCreation): Cover? {
fun save(cover: CoverForCreation): Cover {
val id = UUID.randomUUID()
return query.insertInto(COVER)

query.insertInto(COVER)
.set(COVER.ID, id)
.set(COVER.WIDTH, cover.width)
.set(COVER.HEIGHT, cover.height)
.set(COVER.URL, cover.url.toASCIIString())
.toMono()
.map { Cover(id, cover.url, cover.height, cover.width) }
.block()
.execute()

return Cover(id, cover.url, cover.height, cover.width)
}

fun findCoverOlderThan(date: OffsetDateTime): List<DeleteCoverRequest> = Flux.defer {
Flux.from(
query
.select(
PODCAST.ID, PODCAST.TITLE,
ITEM.ID, ITEM.TITLE,
COVER.ID, COVER.URL
)
.from(
COVER
.innerJoin(ITEM).on(COVER.ID.eq(ITEM.COVER_ID))
.innerJoin(PODCAST).on(ITEM.PODCAST_ID.eq(PODCAST.ID))
)
.where(ITEM.CREATION_DATE.lessOrEqual(date))
.orderBy(COVER.ID.asc())
fun findCoverOlderThan(date: OffsetDateTime): List<DeleteCoverRequest> {
return query
.select(
PODCAST.ID, PODCAST.TITLE,
ITEM.ID, ITEM.TITLE,
COVER.ID, COVER.URL
)
.map { (podcastId, podcastTitle, itemId, itemTitle, coverId, coverUrl) ->
DeleteCoverRequest(
id = coverId,
extension = Path(coverUrl).extension,
item = Item(itemId, itemTitle),
podcast = Podcast(podcastId, podcastTitle)
)
}
}
.collectList()
.block()!!
.from(
COVER
.innerJoin(ITEM).on(COVER.ID.eq(ITEM.COVER_ID))
.innerJoin(PODCAST).on(ITEM.PODCAST_ID.eq(PODCAST.ID))
)
.where(ITEM.CREATION_DATE.lessOrEqual(date))
.orderBy(COVER.ID.asc())
.fetch()
.map { (podcastId, podcastTitle, itemId, itemTitle, coverId, coverUrl) ->
DeleteCoverRequest(
id = coverId,
extension = Path(coverUrl).extension,
item = Item(itemId, itemTitle),
podcast = Podcast(podcastId, podcastTitle)
)
}
}
}

data class CoverForCreation(val width: Int, val height: Int, val url: URI)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import com.github.davinkevin.podcastserver.manager.downloader.DownloadingItem.Po
import org.jooq.DSLContext
import org.jooq.Record9
import org.jooq.impl.DSL.*
import reactor.core.publisher.Flux
import reactor.kotlin.core.publisher.toMono
import java.net.URI
import java.nio.file.Path
import java.time.OffsetDateTime
Expand All @@ -42,8 +40,7 @@ class DownloadRepository(private val query: DSLContext) {
.and(ITEM.ID.notIn(select(DOWNLOADING_ITEM.ITEM_ID).from(DOWNLOADING_ITEM)))
.orderBy(ITEM.PUB_DATE.asc())
)
.toMono()
.block()
.execute()
}

fun addItemToQueue(id: UUID) {
Expand All @@ -56,8 +53,7 @@ class DownloadRepository(private val query: DSLContext) {
.where(ITEM.ID.eq(id))
.and(ITEM.ID.notIn(select(DOWNLOADING_ITEM.ITEM_ID).from(DOWNLOADING_ITEM)))
)
.toMono()
.block()
.execute()
}

fun findAllToDownload(limit: Int): List<DownloadingItem> {
Expand All @@ -71,59 +67,52 @@ class DownloadRepository(private val query: DSLContext) {

val item = DOWNLOADING_ITEM.item()

val items = Flux.from(
query.with(snapshot)
.select(
item.ID, item.TITLE, item.STATUS, item.URL, item.NUMBER_OF_FAIL,
item.podcast().ID, item.podcast().TITLE,
item.cover().ID, item.cover().URL
)
.from(
snapshot
.innerJoin(DOWNLOADING_ITEM)
.on(snapshot.field(DOWNLOADING_ITEM.ITEM_ID)!!.eq(DOWNLOADING_ITEM.ITEM_ID))
)
.where(snapshot.field(DOWNLOADING_ITEM.STATE)!!.eq(DownloadingState.WAITING))
.orderBy(snapshot.field(position))
)
.collectList()
.block()!!
val items = query.with(snapshot)
.select(
item.ID, item.TITLE, item.STATUS, item.URL, item.NUMBER_OF_FAIL,
item.podcast().ID, item.podcast().TITLE,
item.cover().ID, item.cover().URL
)
.from(
snapshot
.innerJoin(DOWNLOADING_ITEM)
.on(snapshot.field(DOWNLOADING_ITEM.ITEM_ID)!!.eq(DOWNLOADING_ITEM.ITEM_ID))
)
.where(snapshot.field(DOWNLOADING_ITEM.STATE)!!.eq(DownloadingState.WAITING))
.orderBy(snapshot.field(position))
.fetch()

return items.map(::toDownloadingItem)
}

fun findAllDownloading(): List<DownloadingItem> {
val item = DOWNLOADING_ITEM.item()
val items = Flux.from(
query
.select(
item.ID, item.TITLE, item.STATUS, item.URL, item.NUMBER_OF_FAIL,
item.podcast().ID, item.podcast().TITLE,
item.cover().ID, item.cover().URL
)
.from(DOWNLOADING_ITEM)
.where(DOWNLOADING_ITEM.STATE.eq(DownloadingState.DOWNLOADING))
.orderBy(DOWNLOADING_ITEM.POSITION.asc())
)
.collectList().block()!!
val items = query
.select(
item.ID, item.TITLE, item.STATUS, item.URL, item.NUMBER_OF_FAIL,
item.podcast().ID, item.podcast().TITLE,
item.cover().ID, item.cover().URL
)
.from(DOWNLOADING_ITEM)
.where(DOWNLOADING_ITEM.STATE.eq(DownloadingState.DOWNLOADING))
.orderBy(DOWNLOADING_ITEM.POSITION.asc())
.fetch()

return items.map(::toDownloadingItem)
}

fun findAllWaiting(): List<DownloadingItem> {
val item = DOWNLOADING_ITEM.item()
val items = Flux.from(
query
.select(
item.ID, item.TITLE, item.STATUS, item.URL, item.NUMBER_OF_FAIL,
item.podcast().ID, item.podcast().TITLE,
item.cover().ID, item.cover().URL
)
.from(DOWNLOADING_ITEM)
.where(DOWNLOADING_ITEM.STATE.eq(DownloadingState.WAITING))
.orderBy(DOWNLOADING_ITEM.POSITION.asc())
)
.collectList().block()!!
val items = query
.select(
item.ID, item.TITLE, item.STATUS, item.URL, item.NUMBER_OF_FAIL,
item.podcast().ID, item.podcast().TITLE,
item.cover().ID, item.cover().URL
)
.from(DOWNLOADING_ITEM)
.where(DOWNLOADING_ITEM.STATE.eq(DownloadingState.WAITING))
.orderBy(DOWNLOADING_ITEM.POSITION.asc())
.fetch()

return items.map(::toDownloadingItem)
}
Expand All @@ -133,16 +122,14 @@ class DownloadRepository(private val query: DSLContext) {
.update(DOWNLOADING_ITEM)
.set(DOWNLOADING_ITEM.STATE, DownloadingState.DOWNLOADING)
.where(DOWNLOADING_ITEM.ITEM_ID.eq(id))
.toMono()
.block()
.execute()
}

fun remove(id: UUID, hasToBeStopped: Boolean) {
query
.deleteFrom(DOWNLOADING_ITEM)
.where(DOWNLOADING_ITEM.ITEM_ID.eq(id))
.toMono()
.block()
.execute()

if (hasToBeStopped) stopItem(id)
}
Expand Down Expand Up @@ -191,17 +178,15 @@ class DownloadRepository(private val query: DSLContext) {
.update(DOWNLOADING_ITEM)
.set(DOWNLOADING_ITEM.POSITION, numberOfDownloadingItem + position + 1 )
.where(DOWNLOADING_ITEM.ITEM_ID.eq(id))
.toMono()
.block()
.execute()
}

fun stopItem(id: UUID): Int {
return query
.update(ITEM)
.set(ITEM.STATUS, STOPPED)
.where(ITEM.ID.eq(id))
.toMono()
.block() ?: 0
.execute()
}

fun updateDownloadItem(item: DownloadingItem): Int {
Expand All @@ -210,8 +195,7 @@ class DownloadRepository(private val query: DSLContext) {
.set(ITEM.STATUS, item.status.toDb())
.set(ITEM.NUMBER_OF_FAIL, item.numberOfFail)
.where(ITEM.ID.eq(item.id))
.toMono()
.block() ?: 0
.execute()
}

fun finishDownload(id: UUID, length: Long, mimeType: String, fileName: Path, downloadDate: OffsetDateTime): Int {
Expand All @@ -223,17 +207,15 @@ class DownloadRepository(private val query: DSLContext) {
.set(ITEM.FILE_NAME, fileName)
.set(ITEM.DOWNLOAD_DATE, downloadDate)
.where(ITEM.ID.eq(id))
.toMono()
.block() ?: 0
.execute()
}

fun resetToWaitingStateAllDownloadingItems(): Int {
return query.
update(DOWNLOADING_ITEM)
update(DOWNLOADING_ITEM)
.set(DOWNLOADING_ITEM.STATE, DownloadingState.WAITING)
.where(DOWNLOADING_ITEM.STATE.eq(DownloadingState.DOWNLOADING))
.toMono()
.block() ?: 0
.execute()
}
}

Expand Down
Loading

0 comments on commit 2ae63fe

Please sign in to comment.