updates in posts repos, content repos and build scripts
This commit is contained in:
build.gradlegradle.properties
features
content
posts
server
src
jvmMain
kotlin
dev
inmo
postssystem
features
posts
roles
common
src
commonMain
kotlin
dev
inmo
postssystem
features
roles
common
kotlin-js-store
server/src/main/java/dev/inmo/postssystem/server
@ -1,8 +1,10 @@
|
||||
package dev.inmo.postssystem.features.content.common
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlin.jvm.JvmInline
|
||||
|
||||
@Serializable
|
||||
@JvmInline
|
||||
value class ContentId(val string: String)
|
||||
|
||||
/**
|
||||
|
@ -11,7 +11,6 @@ kotlin {
|
||||
dependencies {
|
||||
api project(":postssystem.features.content.common")
|
||||
api project(":postssystem.features.common.server")
|
||||
api project(":postssystem.features.content.server")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
94
features/posts/server/src/jvmMain/kotlin/dev/inmo/postssystem/features/posts/server/ExposedServerPostsStorage.kt
Normal file
94
features/posts/server/src/jvmMain/kotlin/dev/inmo/postssystem/features/posts/server/ExposedServerPostsStorage.kt
Normal file
@ -0,0 +1,94 @@
|
||||
package dev.inmo.postssystem.features.posts.server
|
||||
|
||||
import com.soywiz.klock.DateTime
|
||||
import dev.inmo.micro_utils.repos.exposed.*
|
||||
import dev.inmo.postssystem.features.content.common.ContentId
|
||||
import dev.inmo.postssystem.features.posts.common.*
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.statements.InsertStatement
|
||||
import org.jetbrains.exposed.sql.statements.UpdateStatement
|
||||
|
||||
private class PostsContentsRelations(
|
||||
override val database: Database,
|
||||
postIdColumn: Column<Long>
|
||||
) : Table("PostsContentsRelations"), ExposedRepo {
|
||||
val postIdColumn = long("postid").references(postIdColumn)
|
||||
val contentIdColumn = text("contentId")
|
||||
}
|
||||
|
||||
class ExposedServerPostsStorage(
|
||||
override val database: Database,
|
||||
) : ServerPostsStorage, AbstractExposedCRUDRepo<RegisteredPost, PostId, NewPost>(
|
||||
tableName = "Posts"
|
||||
) {
|
||||
val idColumn = long("id").autoIncrement()
|
||||
private val datetimeColumn = long("creationDT")
|
||||
override val primaryKey: PrimaryKey = PrimaryKey(idColumn)
|
||||
|
||||
private val subrepo = PostsContentsRelations(
|
||||
database,
|
||||
idColumn
|
||||
)
|
||||
private fun getContentInTransaction(id: PostId): List<ContentId> {
|
||||
return subrepo.select { subrepo.postIdColumn.eq(id.long) }.map { it[subrepo.contentIdColumn].let(::ContentId) }
|
||||
}
|
||||
|
||||
override val selectByIds: SqlExpressionBuilder.(List<PostId>) -> Op<Boolean> = {
|
||||
idColumn.inList(it.map { it.long })
|
||||
}
|
||||
|
||||
override val selectById: SqlExpressionBuilder.(PostId) -> Op<Boolean> = {
|
||||
idColumn.eq(it.long)
|
||||
}
|
||||
override val ResultRow.asObject: RegisteredPost
|
||||
get() {
|
||||
val id = get(idColumn).let(::PostId)
|
||||
return RegisteredPost(
|
||||
get(idColumn).let(::PostId),
|
||||
getContentInTransaction(id),
|
||||
get(datetimeColumn).let(DateTime::fromUnix)
|
||||
)
|
||||
}
|
||||
|
||||
init {
|
||||
initTable()
|
||||
subrepo.initTable()
|
||||
}
|
||||
|
||||
override fun insert(value: NewPost, it: InsertStatement<Number>) {
|
||||
it[datetimeColumn] = DateTime.now().unixMillisLong
|
||||
}
|
||||
|
||||
override fun update(id: PostId, value: NewPost, it: UpdateStatement) {
|
||||
it[datetimeColumn] = DateTime.now().unixMillisLong
|
||||
|
||||
subrepo.deleteWhere {
|
||||
subrepo.postIdColumn.eq(id.long)
|
||||
}
|
||||
|
||||
value.content.map { it.string }.forEach { contentId ->
|
||||
subrepo.insert {
|
||||
it[subrepo.postIdColumn] = id.long
|
||||
it[subrepo.contentIdColumn] = contentId
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun InsertStatement<Number>.asObject(value: NewPost): RegisteredPost {
|
||||
val id = get(idColumn).let(::PostId)
|
||||
|
||||
value.content.map { it.string }.forEach { contentId ->
|
||||
subrepo.insert {
|
||||
it[subrepo.postIdColumn] = id.long
|
||||
it[subrepo.contentIdColumn] = contentId
|
||||
}
|
||||
}
|
||||
|
||||
return RegisteredPost(
|
||||
get(idColumn).let(::PostId),
|
||||
getContentInTransaction(id),
|
||||
get(datetimeColumn).let(DateTime::fromUnix)
|
||||
)
|
||||
}
|
||||
|
||||
}
|
@ -24,7 +24,7 @@ object RoleSerializer : KSerializer<Role> {
|
||||
private val serializers = mutableMapOf<String, KSerializer<out Role>>()
|
||||
override val descriptor: SerialDescriptor = String.serializer().descriptor
|
||||
|
||||
@InternalSerializationApi
|
||||
@OptIn(InternalSerializationApi::class)
|
||||
override fun deserialize(decoder: Decoder): Role {
|
||||
return if (decoder is JsonDecoder) {
|
||||
val originalJson = decoder.decodeJsonElement().jsonObject
|
||||
@ -48,7 +48,7 @@ object RoleSerializer : KSerializer<Role> {
|
||||
return userRoleFormat.encodeToJsonElement(this::class.serializer() as KSerializer<T>, this)
|
||||
}
|
||||
|
||||
@InternalSerializationApi
|
||||
@OptIn(InternalSerializationApi::class)
|
||||
override fun serialize(encoder: Encoder, value: Role) {
|
||||
if (encoder is JsonEncoder) {
|
||||
if (value is UnknownRole) {
|
||||
|
Reference in New Issue
Block a user