to🪛: label service
This commit is contained in:
parent
76e1f5d9ec
commit
4f1c9b2192
|
@ -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<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(),
|
||||
)
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -11,5 +11,5 @@ data class Label(
|
|||
val type: LabelType,
|
||||
val name: String,
|
||||
var count: Int,
|
||||
val relativedId: Int,
|
||||
val relativedId: Int?,
|
||||
)
|
|
@ -4,6 +4,7 @@ import io.ktor.server.routing.*
|
|||
|
||||
fun Route.labelRoute() {
|
||||
route("/label") {
|
||||
|
||||
get {}
|
||||
post {}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
12
src/main/kotlin/cn/fadinglight/services/LabelService.kt
Normal file
12
src/main/kotlin/cn/fadinglight/services/LabelService.kt
Normal 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
|
||||
}
|
55
src/main/kotlin/cn/fadinglight/services/LabelServiceImpl.kt
Normal file
55
src/main/kotlin/cn/fadinglight/services/LabelServiceImpl.kt
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user