2022-08-21 10:03:27 +00:00
|
|
|
package dev.inmo.plaguposter.ratings.exposed
|
|
|
|
|
2022-09-06 17:56:58 +00:00
|
|
|
import dev.inmo.micro_utils.pagination.utils.optionallyReverse
|
2022-12-14 05:50:02 +00:00
|
|
|
import dev.inmo.micro_utils.repos.exposed.initTable
|
2022-09-06 17:56:58 +00:00
|
|
|
import dev.inmo.micro_utils.repos.exposed.keyvalue.AbstractExposedKeyValueRepo
|
2022-08-21 10:03:27 +00:00
|
|
|
import dev.inmo.plaguposter.posts.models.PostId
|
|
|
|
import dev.inmo.plaguposter.ratings.models.Rating
|
|
|
|
import dev.inmo.plaguposter.ratings.repo.RatingsRepo
|
2022-09-06 17:56:58 +00:00
|
|
|
import org.jetbrains.exposed.sql.*
|
2022-10-25 06:46:31 +00:00
|
|
|
import org.jetbrains.exposed.sql.statements.*
|
2022-09-06 17:56:58 +00:00
|
|
|
import org.jetbrains.exposed.sql.transactions.transaction
|
2022-08-21 10:03:27 +00:00
|
|
|
|
2022-09-06 17:56:58 +00:00
|
|
|
class ExposedRatingsRepo (
|
2022-09-04 09:46:45 +00:00
|
|
|
database: Database
|
2022-09-06 17:56:58 +00:00
|
|
|
) : RatingsRepo, AbstractExposedKeyValueRepo<PostId, Rating>(
|
2022-08-21 10:03:27 +00:00
|
|
|
database,
|
|
|
|
"ratings"
|
2022-09-06 17:56:58 +00:00
|
|
|
) {
|
|
|
|
override val keyColumn = text("post_id")
|
|
|
|
val ratingsColumn = double("rating")
|
2022-11-17 09:09:10 +00:00
|
|
|
override val selectById: ISqlExpressionBuilder.(PostId) -> Op<Boolean> = { keyColumn.eq(it.string) }
|
|
|
|
override val selectByValue: ISqlExpressionBuilder.(Rating) -> Op<Boolean> = { ratingsColumn.eq(it.double) }
|
2022-09-06 17:56:58 +00:00
|
|
|
override val ResultRow.asKey: PostId
|
|
|
|
get() = get(keyColumn).let(::PostId)
|
|
|
|
override val ResultRow.asObject: Rating
|
|
|
|
get() = get(ratingsColumn).let(::Rating)
|
|
|
|
|
2022-12-14 05:50:02 +00:00
|
|
|
init {
|
|
|
|
initTable()
|
|
|
|
}
|
|
|
|
|
2022-10-25 06:46:31 +00:00
|
|
|
override fun update(k: PostId, v: Rating, it: UpdateBuilder<Int>) {
|
2022-09-06 17:56:58 +00:00
|
|
|
it[ratingsColumn] = v.double
|
|
|
|
}
|
|
|
|
|
2022-10-25 06:46:31 +00:00
|
|
|
override fun insertKey(k: PostId, v: Rating, it: InsertStatement<Number>) {
|
2022-09-06 17:56:58 +00:00
|
|
|
it[keyColumn] = k.string
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun Query.optionallyLimit(limit: Int?) = if (limit == null) {
|
|
|
|
this
|
|
|
|
} else {
|
|
|
|
limit(limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
override suspend fun getPosts(
|
|
|
|
range: ClosedRange<Rating>,
|
|
|
|
reversed: Boolean,
|
|
|
|
count: Int?,
|
|
|
|
exclude: List<PostId>
|
|
|
|
): Map<PostId, Rating> = transaction(database) {
|
|
|
|
select {
|
|
|
|
ratingsColumn.greaterEq(range.start.double).and(
|
|
|
|
ratingsColumn.lessEq(range.endInclusive.double)
|
|
|
|
).and(
|
|
|
|
keyColumn.notInList(exclude.map { it.string })
|
|
|
|
)
|
|
|
|
}.optionallyLimit(count).optionallyReverse(reversed).map {
|
|
|
|
it.asKey to it.asObject
|
|
|
|
}
|
|
|
|
}.toMap()
|
|
|
|
|
|
|
|
override suspend fun getPostsWithRatingGreaterEq(
|
|
|
|
then: Rating,
|
|
|
|
reversed: Boolean,
|
|
|
|
count: Int?,
|
|
|
|
exclude: List<PostId>
|
|
|
|
) = transaction(database) {
|
|
|
|
select { ratingsColumn.greaterEq(then.double).and(keyColumn.notInList(exclude.map { it.string })) }.optionallyLimit(count).optionallyReverse(reversed).map {
|
|
|
|
it.asKey to it.asObject
|
|
|
|
}
|
|
|
|
}.toMap()
|
|
|
|
|
|
|
|
override suspend fun getPostsWithRatingLessEq(
|
|
|
|
then: Rating,
|
|
|
|
reversed: Boolean,
|
|
|
|
count: Int?,
|
|
|
|
exclude: List<PostId>
|
|
|
|
): Map<PostId, Rating> = transaction(database) {
|
|
|
|
select { ratingsColumn.lessEq(then.double).and(keyColumn.notInList(exclude.map { it.string })) }.optionallyLimit(count).optionallyReverse(reversed).map {
|
|
|
|
it.asKey to it.asObject
|
|
|
|
}
|
|
|
|
}.toMap()
|
|
|
|
}
|