diff --git a/src/main/kotlin/cn/fadinglight/dao/LabelDao.kt b/src/main/kotlin/cn/fadinglight/dao/LabelDao.kt index 4945d5c..57aa857 100644 --- a/src/main/kotlin/cn/fadinglight/dao/LabelDao.kt +++ b/src/main/kotlin/cn/fadinglight/dao/LabelDao.kt @@ -1,7 +1,25 @@ package cn.fadinglight.dao +import cn.fadinglight.models.Label +import cn.fadinglight.models.LabelType + data class LabelDao( val name: String, val count: Int, - val labels: List? -) \ No newline at end of file + var labels: List?, +) + + +fun LabelDao.label(type: LabelType) = Label( + id = null, + type = type, + name = name, + count = 0, + relativedId = null +) + +fun Label.dao() = LabelDao( + name = name, + count = count, + labels = emptyList(), +) diff --git a/src/main/kotlin/cn/fadinglight/mapers/Schemas.kt b/src/main/kotlin/cn/fadinglight/mapers/Schemas.kt index 2978df2..8728d64 100644 --- a/src/main/kotlin/cn/fadinglight/mapers/Schemas.kt +++ b/src/main/kotlin/cn/fadinglight/mapers/Schemas.kt @@ -5,15 +5,15 @@ import org.jetbrains.exposed.dao.id.IntIdTable object Bills : IntIdTable() { val type = varchar("type", 15) val date = varchar("date", 15) - val money = integer("money") + val money = float("money") val cls = varchar("cls", 31) val label = varchar("label", 31) - val options = varchar("options", 255).nullable() + val options = varchar("options", 255).default("") } object Labels : IntIdTable() { val type = varchar("type", 15) val name = varchar("name", 15) - val relativeId = integer("relative_id") - val nums = integer("nums") + val relativeId = integer("relative_id").nullable() + val count = integer("nums").default(0) } diff --git a/src/main/kotlin/cn/fadinglight/models/Label.kt b/src/main/kotlin/cn/fadinglight/models/Label.kt index 4b3587e..7c1ff05 100644 --- a/src/main/kotlin/cn/fadinglight/models/Label.kt +++ b/src/main/kotlin/cn/fadinglight/models/Label.kt @@ -11,5 +11,5 @@ data class Label( val type: LabelType, val name: String, var count: Int, - val relativedId: Int, + val relativedId: Int?, ) \ No newline at end of file diff --git a/src/main/kotlin/cn/fadinglight/routes/LabelRoutes.kt b/src/main/kotlin/cn/fadinglight/routes/LabelRoutes.kt index 0568644..b5a6779 100644 --- a/src/main/kotlin/cn/fadinglight/routes/LabelRoutes.kt +++ b/src/main/kotlin/cn/fadinglight/routes/LabelRoutes.kt @@ -1,9 +1,10 @@ -package cn.fadinglight.routes - -import io.ktor.server.routing.* - -fun Route.labelRoute() { - route("/label") { - - } +package cn.fadinglight.routes + +import io.ktor.server.routing.* + +fun Route.labelRoute() { + route("/label") { + get {} + post {} + } } \ No newline at end of file diff --git a/src/main/kotlin/cn/fadinglight/services/LabelServe.kt b/src/main/kotlin/cn/fadinglight/services/LabelServe.kt deleted file mode 100644 index 0dd0d96..0000000 --- a/src/main/kotlin/cn/fadinglight/services/LabelServe.kt +++ /dev/null @@ -1,12 +0,0 @@ -package cn.fadinglight.services - -import cn.fadinglight.dao.LabelDao -import cn.fadinglight.models.Label -import cn.fadinglight.models.LabelType - -interface LabelServe { - suspend fun getLabels(): List - suspend fun addLabel(labelType: LabelType, label: Label): Boolean - suspend fun deleteLabel(labelId: Int): Boolean - suspend fun addCount(labelId: Int): Boolean -} \ No newline at end of file diff --git a/src/main/kotlin/cn/fadinglight/services/LabelService.kt b/src/main/kotlin/cn/fadinglight/services/LabelService.kt new file mode 100644 index 0000000..71bf65b --- /dev/null +++ b/src/main/kotlin/cn/fadinglight/services/LabelService.kt @@ -0,0 +1,12 @@ +package cn.fadinglight.services + +import cn.fadinglight.dao.LabelDao +import cn.fadinglight.models.Label +import cn.fadinglight.models.LabelType + +interface LabelService { + suspend fun getLabels(): List + suspend fun addLabel(labelType: LabelType, label: Label): Int + suspend fun deleteLabel(labelId: Int): Int + suspend fun addCount(labelId: Int): Int +} \ No newline at end of file diff --git a/src/main/kotlin/cn/fadinglight/services/LabelServiceImpl.kt b/src/main/kotlin/cn/fadinglight/services/LabelServiceImpl.kt new file mode 100644 index 0000000..0b9f98a --- /dev/null +++ b/src/main/kotlin/cn/fadinglight/services/LabelServiceImpl.kt @@ -0,0 +1,55 @@ +package cn.fadinglight.services + +import cn.fadinglight.dao.LabelDao +import cn.fadinglight.dao.dao +import cn.fadinglight.mapers.Labels +import cn.fadinglight.models.Label +import cn.fadinglight.models.LabelType +import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.transactions.transaction + +class LabelServiceImpl : LabelService { + private fun resultRowToLabel(row: ResultRow) = Label( + id = row[Labels.id].value, + type = LabelType.valueOf(row[Labels.type]), + name = row[Labels.name], + count = row[Labels.count], + relativedId = row[Labels.relativeId] + ) + + override suspend fun getLabels(): List { + val labelGroups = transaction { + Labels.selectAll().map(::resultRowToLabel).groupBy { it.type } + } + return labelGroups[LabelType.CLASS]?.map { + it.dao().apply { + labels = labelGroups[LabelType.LABEL] + ?.filter { it2 -> it2.relativedId == it.id } + ?.map(Label::dao) + ?: emptyList() + } + } ?: emptyList() + } + + override suspend fun addLabel(labelType: LabelType, label: Label): Int = transaction { + Labels.insertAndGetId { + it[type] = label.type.name + it[name] = label.name + it[count] = label.count + it[relativeId] = label.relativedId + }.value + } + + override suspend fun deleteLabel(labelId: Int): Int { + TODO("Not yet implemented") + } + + override suspend fun addCount(labelId: Int): Int = transaction { + Labels.update({ Labels.id eq labelId }) { + with(SqlExpressionBuilder) { + it[count] = count + 1 + } + } + } + +} \ No newline at end of file