to🪛: label service

This commit is contained in:
车厘子 2022-10-28 13:44:17 +08:00
parent 76e1f5d9ec
commit 4f1c9b2192
7 changed files with 101 additions and 27 deletions

View File

@ -1,7 +1,25 @@
package cn.fadinglight.dao package cn.fadinglight.dao
import cn.fadinglight.models.Label
import cn.fadinglight.models.LabelType
data class LabelDao( data class LabelDao(
val name: String, val name: String,
val count: Int, val count: Int,
val labels: List<LabelDao>? var labels: List<LabelDao>?,
)
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(),
) )

View File

@ -5,15 +5,15 @@ import org.jetbrains.exposed.dao.id.IntIdTable
object Bills : IntIdTable() { object Bills : IntIdTable() {
val type = varchar("type", 15) val type = varchar("type", 15)
val date = varchar("date", 15) val date = varchar("date", 15)
val money = integer("money") val money = float("money")
val cls = varchar("cls", 31) val cls = varchar("cls", 31)
val label = varchar("label", 31) val label = varchar("label", 31)
val options = varchar("options", 255).nullable() val options = varchar("options", 255).default("")
} }
object Labels : IntIdTable() { object Labels : IntIdTable() {
val type = varchar("type", 15) val type = varchar("type", 15)
val name = varchar("name", 15) val name = varchar("name", 15)
val relativeId = integer("relative_id") val relativeId = integer("relative_id").nullable()
val nums = integer("nums") val count = integer("nums").default(0)
} }

View File

@ -11,5 +11,5 @@ data class Label(
val type: LabelType, val type: LabelType,
val name: String, val name: String,
var count: Int, var count: Int,
val relativedId: Int, val relativedId: Int?,
) )

View File

@ -4,6 +4,7 @@ import io.ktor.server.routing.*
fun Route.labelRoute() { fun Route.labelRoute() {
route("/label") { route("/label") {
get {}
post {}
} }
} }

View File

@ -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<LabelDao>
suspend fun addLabel(labelType: LabelType, label: Label): Boolean
suspend fun deleteLabel(labelId: Int): Boolean
suspend fun addCount(labelId: Int): Boolean
}

View File

@ -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<LabelDao>
suspend fun addLabel(labelType: LabelType, label: Label): Int
suspend fun deleteLabel(labelId: Int): Int
suspend fun addCount(labelId: Int): Int
}

View File

@ -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<LabelDao> {
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
}
}
}
}