add posts

This commit is contained in:
InsanusMokrassar 2022-08-18 18:59:05 +06:00
parent b32b3e7c38
commit 9502accf65
19 changed files with 234 additions and 33 deletions

View File

@ -8,6 +8,6 @@ android.enableJetifier=true
# Project data
group=project_group
group=dev.inmo
version=0.0.1
android_code_version=1

View File

@ -3,6 +3,10 @@
kotlin = "1.7.10"
kotlin-serialization = "1.4.0-RC"
plagubot = "2.1.0"
tgbotapi = "3.1.1"
microutils = "0.12.1"
dexcount = "3.1.0"
junit_version = "4.12"
test_ext_junit_version = "1.1.3"
@ -24,6 +28,11 @@ kotlin-test-js = { module = "org.jetbrains.kotlin:kotlin-test-js", version.ref =
android-test-junit = { module = "androidx.test.ext:junit", version.ref = "test_ext_junit_version" }
android-test-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espresso_core" }
tgbotapi = { module = "dev.inmo:tgbotapi", version.ref = "tgbotapi" }
plagubot-plugin = { module = "dev.inmo:plagubot.plugin", version.ref = "plagubot" }
microutils-repos-common = { module = "dev.inmo:micro_utils.repos.common", version.ref = "microutils" }
microutils-repos-exposed = { module = "dev.inmo:micro_utils.repos.exposed", version.ref = "microutils" }
# buildscript classpaths
android-tools-build = { module = "com.android.tools.build:gradle", version.ref = "android-gradle-plugin" }

View File

@ -1,8 +0,0 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
}
apply from: "$mppProjectWithSerializationPresetPath"

View File

@ -1,7 +0,0 @@
package project_group
class Library {
fun someLibraryMethod(): Boolean {
return true
}
}

View File

@ -1,14 +0,0 @@
/*
* This Kotlin source file was generated by the Gradle 'init' task.
*/
package project_group
import kotlin.test.Test
import kotlin.test.assertTrue
class LibraryTest {
@Test fun testSomeLibraryMethod() {
val classUnderTest = Library()
assertTrue(classUnderTest.someLibraryMethod(), "someLibraryMethod should return 'true'")
}
}

View File

@ -1 +0,0 @@
<manifest package="project_group.lib"/>

24
posts/build.gradle Normal file
View File

@ -0,0 +1,24 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
}
apply from: "$mppProjectWithSerializationPresetPath"
kotlin {
sourceSets {
commonMain {
dependencies {
api libs.tgbotapi
api libs.microutils.repos.common
}
}
jvmMain {
dependencies {
api libs.microutils.repos.exposed
api libs.plagubot.plugin
}
}
}
}

View File

@ -0,0 +1 @@
package dev.inmo.plaguposter.posts

View File

@ -0,0 +1,20 @@
package dev.inmo.plaguposter.posts.models
import dev.inmo.tgbotapi.types.ChatId
import kotlinx.serialization.Serializable
@Serializable
sealed interface Post {
val content: List<PostContentInfo>
}
@Serializable
data class NewPost(
override val content: List<PostContentInfo>
) : Post
@Serializable
data class RegisteredPost(
val id: PostId,
override val content: List<PostContentInfo>
) : Post

View File

@ -0,0 +1,13 @@
package dev.inmo.plaguposter.posts.models
import dev.inmo.tgbotapi.types.ChatId
import dev.inmo.tgbotapi.types.MessageIdentifier
import kotlinx.serialization.Serializable
@Serializable
data class PostContentInfo(
val chatId: ChatId,
val messageId: MessageIdentifier,
val group: String?,
val order: Int
)

View File

@ -0,0 +1,10 @@
package dev.inmo.plaguposter.posts.models
import kotlinx.serialization.Serializable
import kotlin.jvm.JvmInline
@Serializable
@JvmInline
value class PostId(
val string: String
)

View File

@ -0,0 +1,7 @@
package dev.inmo.plaguposter.posts.repo
import dev.inmo.micro_utils.repos.CRUDRepo
import dev.inmo.plaguposter.posts.models.*
interface PostsRepo : CRUDRepo<RegisteredPost, PostId, NewPost>, ReadPostsRepo, WritePostsRepo {
}

View File

@ -0,0 +1,6 @@
package dev.inmo.plaguposter.posts.repo
import dev.inmo.micro_utils.repos.ReadCRUDRepo
import dev.inmo.plaguposter.posts.models.*
interface ReadPostsRepo : ReadCRUDRepo<RegisteredPost, PostId>

View File

@ -0,0 +1,7 @@
package dev.inmo.plaguposter.posts.repo
import dev.inmo.micro_utils.repos.ReadCRUDRepo
import dev.inmo.micro_utils.repos.WriteCRUDRepo
import dev.inmo.plaguposter.posts.models.*
interface WritePostsRepo : WriteCRUDRepo<RegisteredPost, PostId, NewPost>

View File

@ -0,0 +1,12 @@
package dev.inmo.plaguposter.posts
import dev.inmo.plagubot.Plugin
import kotlinx.serialization.json.JsonObject
import org.jetbrains.exposed.sql.Database
import org.koin.core.module.Module
object Plugin : Plugin {
override fun Module.setupDI(database: Database, params: JsonObject) {
TODO("Not yet implemented")
}
}

View File

@ -0,0 +1,35 @@
package dev.inmo.plaguposter.posts.exposed
import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.repos.KeyValuesRepo
import dev.inmo.micro_utils.repos.exposed.*
import dev.inmo.plaguposter.posts.models.*
import dev.inmo.plaguposter.posts.repo.PostsRepo
import dev.inmo.tgbotapi.types.ChatId
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.statements.UpdateStatement
import sun.security.pkcs.ContentInfo
internal class ExposedContentInfoRepo(
override val database: Database,
postIdColumnReference: Column<String>
) : ExposedRepo, Table(name = "posts_content") {
val postIdColumn = (text("post_id") references postIdColumnReference).index()
val chatIdColumn = long("chat_id")
val messageIdColumn = long("message_id")
val groupColumn = text("group").nullable()
val orderColumn = integer("order")
val ResultRow.asObject
get() = PostContentInfo(
ChatId(get(chatIdColumn)),
get(messageIdColumn),
get(groupColumn),
get(orderColumn)
)
init {
initTable()
}
}

View File

@ -0,0 +1,86 @@
package dev.inmo.plaguposter.posts.exposed
import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.repos.KeyValuesRepo
import dev.inmo.micro_utils.repos.exposed.AbstractExposedCRUDRepo
import dev.inmo.plaguposter.posts.models.*
import dev.inmo.plaguposter.posts.repo.PostsRepo
import kotlinx.serialization.json.Json
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.statements.UpdateStatement
class ExposedPostsRepo(
override val database: Database,
json: Json
) : PostsRepo, AbstractExposedCRUDRepo<RegisteredPost, PostId, NewPost>(
tableName = "posts"
) {
val idColumn = text("id").clientDefault { uuid4().toString() }
private val contentRepo by lazy {
ExposedContentInfoRepo(
database,
idColumn
)
}
override val primaryKey: PrimaryKey? = PrimaryKey(idColumn)
override val selectById: SqlExpressionBuilder.(PostId) -> Op<Boolean> = { idColumn.eq(it.string) }
override val selectByIds: SqlExpressionBuilder.(List<PostId>) -> Op<Boolean> = { idColumn.inList(it.map { it.string }) }
override val ResultRow.asObject: RegisteredPost
get() {
val id = PostId(get(idColumn))
return RegisteredPost(
id,
with(contentRepo) {
select { postIdColumn.eq(id.string) }.map {
it.asObject
}
}
)
}
override fun InsertStatement<Number>.asObject(value: NewPost): RegisteredPost {
val id = PostId(get(idColumn))
with(contentRepo) {
value.content.forEach { contentInfo ->
insert {
it[postIdColumn] = id.string
it[chatIdColumn] = contentInfo.chatId.chatId
it[messageIdColumn] = contentInfo.messageId
it[groupColumn] = contentInfo.group
it[orderColumn] = contentInfo.order
}
}
}
return RegisteredPost(
id,
with(contentRepo) {
select { postIdColumn.eq(id.string) }.map {
it.asObject
}
}
)
}
override fun update(id: PostId, value: NewPost, it: UpdateStatement) {
with(contentRepo) {
deleteWhere { postIdColumn.eq(id.string) }
value.content.forEach { contentInfo ->
insert {
it[postIdColumn] = id.string
it[chatIdColumn] = contentInfo.chatId.chatId
it[messageIdColumn] = contentInfo.messageId
it[groupColumn] = contentInfo.group
it[orderColumn] = contentInfo.order
}
}
}
}
override fun insert(value: NewPost, it: InsertStatement<Number>) {}
}

View File

@ -0,0 +1 @@
<manifest package="dev.inmo.plaguposter.posts"/>

View File

@ -1,7 +1,7 @@
rootProject.name = 'project_name'
rootProject.name = 'plaguposter'
String[] includes = [
":lib"
":posts"
]