mirror of
https://github.com/InsanusMokrassar/PlaguPoster.git
synced 2025-12-06 04:55:41 +00:00
add selector
This commit is contained in:
@@ -1,24 +1,81 @@
|
||||
package dev.inmo.plaguposter.ratings.exposed
|
||||
|
||||
import dev.inmo.micro_utils.repos.KeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedKeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.mappers.withMapper
|
||||
import dev.inmo.micro_utils.pagination.utils.optionallyReverse
|
||||
import dev.inmo.micro_utils.repos.exposed.keyvalue.AbstractExposedKeyValueRepo
|
||||
import dev.inmo.plaguposter.posts.models.PostId
|
||||
import dev.inmo.plaguposter.ratings.models.Rating
|
||||
import dev.inmo.plaguposter.ratings.repo.RatingsRepo
|
||||
import org.jetbrains.exposed.sql.Column
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.statements.InsertStatement
|
||||
import org.jetbrains.exposed.sql.statements.UpdateStatement
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
|
||||
class ExposedRatingsRepo(
|
||||
class ExposedRatingsRepo (
|
||||
database: Database
|
||||
) : RatingsRepo, KeyValueRepo<PostId, Rating> by ExposedKeyValueRepo(
|
||||
) : RatingsRepo, AbstractExposedKeyValueRepo<PostId, Rating>(
|
||||
database,
|
||||
{ text("post_id") },
|
||||
{ double("rating") },
|
||||
"ratings"
|
||||
).withMapper(
|
||||
{ string },
|
||||
{ double },
|
||||
{ PostId(this) },
|
||||
{ Rating(this) }
|
||||
)
|
||||
) {
|
||||
override val keyColumn = text("post_id")
|
||||
val ratingsColumn = double("rating")
|
||||
override val selectById: SqlExpressionBuilder.(PostId) -> Op<Boolean> = { keyColumn.eq(it.string) }
|
||||
override val selectByValue: SqlExpressionBuilder.(Rating) -> Op<Boolean> = { ratingsColumn.eq(it.double) }
|
||||
override val ResultRow.asKey: PostId
|
||||
get() = get(keyColumn).let(::PostId)
|
||||
override val ResultRow.asObject: Rating
|
||||
get() = get(ratingsColumn).let(::Rating)
|
||||
|
||||
override fun update(k: PostId, v: Rating, it: UpdateStatement) {
|
||||
it[ratingsColumn] = v.double
|
||||
}
|
||||
|
||||
override fun insert(k: PostId, v: Rating, it: InsertStatement<Number>) {
|
||||
it[keyColumn] = k.string
|
||||
it[ratingsColumn] = v.double
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user