diff --git a/build.gradle.kts b/build.gradle.kts index 9f56e20..fc6c9b8 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -12,7 +12,6 @@ plugins { } - group = "cn.fadinglight" version = "0.0.1" application { @@ -22,7 +21,6 @@ application { applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment") } - tasks.withType { manifest { attributes( diff --git a/src/main/kotlin/cn/fadinglight/dao/BillDao.kt b/src/main/kotlin/cn/fadinglight/dao/BillDao.kt deleted file mode 100644 index f44ac65..0000000 --- a/src/main/kotlin/cn/fadinglight/dao/BillDao.kt +++ /dev/null @@ -1,36 +0,0 @@ -package cn.fadinglight.dao - -import cn.fadinglight.models.Bill -import cn.fadinglight.models.BillType -import kotlinx.serialization.Serializable - -@Serializable -data class BillDao( - val id: Int? = null, - val type: String, - val date: String, - val money: Int, - val cls: String, - val label: String, - val options: String? = null, -) - -fun BillDao.bill() = Bill( - id = id, - type = BillType.valueOf(type), - date = date, - money = money, - cls = cls, - label = label, - options = options ?: "", -) - -fun Bill.dao() = BillDao( - id = id, - type = type.name, - date = date, - money = money, - cls = cls, - label = label, - options = options, -) \ No newline at end of file diff --git a/src/main/kotlin/cn/fadinglight/dao/LabelDao.kt b/src/main/kotlin/cn/fadinglight/dao/LabelDao.kt deleted file mode 100644 index 57aa857..0000000 --- a/src/main/kotlin/cn/fadinglight/dao/LabelDao.kt +++ /dev/null @@ -1,25 +0,0 @@ -package cn.fadinglight.dao - -import cn.fadinglight.models.Label -import cn.fadinglight.models.LabelType - -data class LabelDao( - val name: String, - val count: Int, - 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/libs/Response.kt b/src/main/kotlin/cn/fadinglight/libs/Response.kt index bba81ee..d3f510a 100644 --- a/src/main/kotlin/cn/fadinglight/libs/Response.kt +++ b/src/main/kotlin/cn/fadinglight/libs/Response.kt @@ -8,11 +8,11 @@ data class RespData(val code: Int, val data: T?, val message: String?) sealed class Resp { abstract fun json(): RespData - class Ok(val data: T) : Resp() { - override fun json() = RespData(code = 0, data = data, message = null) + class Ok(private val data: T) : Resp() { + override fun json() = RespData(code = 0, data = data, message = null) } - class Error(val message: String?, val code: Int) : Resp() { + class Error(private val message: String?, val code: Int) : Resp() { override fun json(): RespData = RespData(code = code, data = null, message = message) } diff --git a/src/main/kotlin/cn/fadinglight/models/Bill.kt b/src/main/kotlin/cn/fadinglight/models/Bill.kt index 531210d..5e22316 100644 --- a/src/main/kotlin/cn/fadinglight/models/Bill.kt +++ b/src/main/kotlin/cn/fadinglight/models/Bill.kt @@ -12,8 +12,8 @@ data class Bill( var id: Int?, val type: BillType, val date: String, - val money: Int, + val money: Float, val cls: String, val label: String, - val options: String?, + val options: String = "", ) \ No newline at end of file diff --git a/src/main/kotlin/cn/fadinglight/models/Label.kt b/src/main/kotlin/cn/fadinglight/models/Label.kt index 7c1ff05..8f14d9d 100644 --- a/src/main/kotlin/cn/fadinglight/models/Label.kt +++ b/src/main/kotlin/cn/fadinglight/models/Label.kt @@ -4,6 +4,7 @@ package cn.fadinglight.models enum class LabelType { CLASS, LABEL, + INCOME_CLASS, } data class Label( diff --git a/src/main/kotlin/cn/fadinglight/plugins/Routing.kt b/src/main/kotlin/cn/fadinglight/plugins/Routing.kt index a4968c6..79d7d84 100644 --- a/src/main/kotlin/cn/fadinglight/plugins/Routing.kt +++ b/src/main/kotlin/cn/fadinglight/plugins/Routing.kt @@ -10,7 +10,7 @@ fun Application.configureRouting() { routing { route("/") { get { - call.respondText("Hello World!") + call.respondText("Welcome che's Bill App!") } } route("/api/v1") { diff --git a/src/main/kotlin/cn/fadinglight/routes/BillRoutes.kt b/src/main/kotlin/cn/fadinglight/routes/BillRoutes.kt index ee340b4..978c672 100644 --- a/src/main/kotlin/cn/fadinglight/routes/BillRoutes.kt +++ b/src/main/kotlin/cn/fadinglight/routes/BillRoutes.kt @@ -1,45 +1,54 @@ package cn.fadinglight.routes -import cn.fadinglight.dao.BillDao -import cn.fadinglight.dao.bill import cn.fadinglight.libs.Resp import cn.fadinglight.services.BillServiceImpl +import cn.fadinglight.vo.BillVO +import cn.fadinglight.vo.bill import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* -import java.lang.Exception +import io.ktor.server.util.* fun Route.billRoute() { val billService = BillServiceImpl() route("/bill") { get("/{year}/{month}") { - val year = call.parameters["year"]!! - val month = call.parameters["month"]!! - call.respond(Resp.Ok(billService.getManyBills(year, month)).json()) + runCatching { + val year = call.parameters["year"]!! + val month = call.parameters["month"]!! + billService.getManyBills(year, month) + }.onSuccess { + call.respond(Resp.Ok(it).json()) + }.onFailure { + call.respond(Resp.Error(it.message, code = -1).json()) + } } - get { + get("/") { call.respond(status = HttpStatusCode.OK, Resp.Ok(billService.getManyBills()).json()) } - post { - try { - val bills = call.receive>().map(BillDao::bill) - val count = billService.addManyBills(bills) - call.respond(status = HttpStatusCode.Created, Resp.Ok(count).json()) - } catch (e: Exception) { - call.respond(status = HttpStatusCode.Created, Resp.Error(e.message, code = -1).json()) + post("/") { + runCatching { + val bills = call.receive>().map(BillVO::bill) + billService.addManyBills(bills) + }.onSuccess { + call.respond(Resp.Ok(it).json()) + }.onFailure { + call.respond(Resp.Error(it.message, code = -1).json()) } } + delete("{id}") { - val id = call.parameters["id"]!!.toInt() + val id = call.parameters.getOrFail("id").toInt() val count = billService.deleteOneBill(id) call.respond(status = HttpStatusCode.OK, Resp.Ok(count).json()) } - put { - val bill = call.receive().bill() + + put("/") { + val bill = call.receive().bill() val count = billService.updateOneBill(bill) call.respond(status = HttpStatusCode.OK, Resp.Ok(count).json()) } diff --git a/src/main/kotlin/cn/fadinglight/routes/LabelRoutes.kt b/src/main/kotlin/cn/fadinglight/routes/LabelRoutes.kt index b5a6779..11bd90e 100644 --- a/src/main/kotlin/cn/fadinglight/routes/LabelRoutes.kt +++ b/src/main/kotlin/cn/fadinglight/routes/LabelRoutes.kt @@ -1,10 +1,23 @@ package cn.fadinglight.routes +import cn.fadinglight.libs.Resp +import cn.fadinglight.services.LabelServiceImpl +import cn.fadinglight.vo.LabelVO +import io.ktor.server.application.* +import io.ktor.server.response.* import io.ktor.server.routing.* fun Route.labelRoute() { + val labelService = LabelServiceImpl() route("/label") { - get {} + get("/") { + call.respond( + Resp.Ok( +// mapOf("consume" to listOf(), "income" to listOf()) + labelService.getLabels() + ).json() + ) + } post {} } } \ No newline at end of file diff --git a/src/main/kotlin/cn/fadinglight/services/BillService.kt b/src/main/kotlin/cn/fadinglight/services/BillService.kt index 7c41ed5..d95360a 100644 --- a/src/main/kotlin/cn/fadinglight/services/BillService.kt +++ b/src/main/kotlin/cn/fadinglight/services/BillService.kt @@ -6,7 +6,7 @@ interface BillService { suspend fun getManyBills(year: String, month: String): List suspend fun getManyBills(year: String, month: String, day: String): List suspend fun getManyBills(): List - suspend fun getOneBill(billId: Int): Bill + suspend fun getOneBill(billId: Int): Bill? suspend fun addManyBills(bills: List): Int suspend fun addOneBill(bill: Bill): Int suspend fun updateManyBills(bills: List): Int diff --git a/src/main/kotlin/cn/fadinglight/services/BillServiceImpl.kt b/src/main/kotlin/cn/fadinglight/services/BillServiceImpl.kt index 4de9605..ffc2b15 100644 --- a/src/main/kotlin/cn/fadinglight/services/BillServiceImpl.kt +++ b/src/main/kotlin/cn/fadinglight/services/BillServiceImpl.kt @@ -1,6 +1,7 @@ package cn.fadinglight.services import cn.fadinglight.mapers.Bills +import cn.fadinglight.mapers.Labels import cn.fadinglight.models.Bill import cn.fadinglight.models.BillType import org.jetbrains.exposed.sql.* @@ -21,43 +22,61 @@ class BillServiceImpl : BillService { ) override suspend fun getManyBills(year: String, month: String): List = transaction { - Bills.select(Bills.date like "%${year}-${month}%").map(::resultRowToBill) + Bills + .select(Bills.date like "%${year}-${month}%") + .map(::resultRowToBill) } override suspend fun getManyBills(year: String, month: String, day: String): List = transaction { - Bills.select(Bills.date eq "${year}-${month}-${day}").map(::resultRowToBill) + Bills + .select(Bills.date eq "${year}-${month}-${day}") + .map(::resultRowToBill) } override suspend fun getManyBills(): List = transaction { - Bills.selectAll().map(::resultRowToBill) + Bills + .selectAll() + .map(::resultRowToBill) } - override suspend fun getOneBill(billId: Int): Bill = transaction { - Bills.select(Bills.id eq billId).single().let(::resultRowToBill) + override suspend fun getOneBill(billId: Int): Bill? = transaction { + Bills + .select(Bills.id eq billId) + .map(::resultRowToBill) + .singleOrNull() } - override suspend fun addOneBill(bill: Bill): Int = transaction { - Bills.insertAndGetId { - it[type] = bill.type.name - it[date] = bill.date - it[cls] = bill.cls - it[label] = bill.label - it[money] = bill.money - it[options] = bill.options - }.value + Bills + .insertAndGetId { + it[type] = bill.type.name + it[date] = bill.date + it[cls] = bill.cls + it[label] = bill.label + it[money] = bill.money + it[options] = bill.options + } + + Labels + .update({ Labels.name inList listOf(bill.cls, bill.label) }) { + with(SqlExpressionBuilder) { + it[count] = count + 1 + } + } } override suspend fun addManyBills(bills: List): Int = transaction { - Bills.batchInsert(bills) { - this[Bills.type] = it.type.name - this[Bills.date] = it.date - this[Bills.cls] = it.cls - this[Bills.label] = it.label - this[Bills.money] = it.money - this[Bills.options] = it.options - }.count() + Bills + .batchInsert(bills) { + this[Bills.type] = it.type.name + this[Bills.date] = it.date + this[Bills.cls] = it.cls + this[Bills.label] = it.label + this[Bills.money] = it.money + this[Bills.options] = it.options + } + .count() } override suspend fun updateManyBills(bills: List): Int { @@ -65,14 +84,15 @@ class BillServiceImpl : BillService { } override suspend fun updateOneBill(bill: Bill) = transaction { - Bills.update({ Bills.id eq bill.id }) { - it[type] = bill.type.name - it[date] = bill.date - it[money] = bill.money - it[cls] = bill.cls - it[label] = bill.label - it[options] = bill.options - } + Bills + .update({ Bills.id eq bill.id }) { + it[type] = bill.type.name + it[date] = bill.date + it[money] = bill.money + it[cls] = bill.cls + it[label] = bill.label + it[options] = bill.options + } } override suspend fun deleteManyBills(bills: List): Int { @@ -80,8 +100,7 @@ class BillServiceImpl : BillService { } override suspend fun deleteOneBill(billId: Int): Int = transaction { - Bills.deleteWhere { Bills.id eq billId } + Bills + .deleteWhere { Bills.id eq billId } } - - } diff --git a/src/main/kotlin/cn/fadinglight/services/LabelService.kt b/src/main/kotlin/cn/fadinglight/services/LabelService.kt index 71bf65b..65a5157 100644 --- a/src/main/kotlin/cn/fadinglight/services/LabelService.kt +++ b/src/main/kotlin/cn/fadinglight/services/LabelService.kt @@ -1,11 +1,11 @@ package cn.fadinglight.services -import cn.fadinglight.dao.LabelDao import cn.fadinglight.models.Label import cn.fadinglight.models.LabelType +import cn.fadinglight.vo.LabelGroup interface LabelService { - suspend fun getLabels(): List + suspend fun getLabels(): LabelGroup suspend fun addLabel(labelType: LabelType, label: Label): Int suspend fun deleteLabel(labelId: Int): Int suspend fun addCount(labelId: Int): Int diff --git a/src/main/kotlin/cn/fadinglight/services/LabelServiceImpl.kt b/src/main/kotlin/cn/fadinglight/services/LabelServiceImpl.kt index 0b9f98a..efebd69 100644 --- a/src/main/kotlin/cn/fadinglight/services/LabelServiceImpl.kt +++ b/src/main/kotlin/cn/fadinglight/services/LabelServiceImpl.kt @@ -1,10 +1,11 @@ package cn.fadinglight.services -import cn.fadinglight.dao.LabelDao -import cn.fadinglight.dao.dao +import cn.fadinglight.vo.LabelVO +import cn.fadinglight.vo.vo import cn.fadinglight.mapers.Labels import cn.fadinglight.models.Label import cn.fadinglight.models.LabelType +import cn.fadinglight.vo.LabelGroup import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.transaction @@ -17,18 +18,32 @@ class LabelServiceImpl : LabelService { relativedId = row[Labels.relativeId] ) - override suspend fun getLabels(): List { + override suspend fun getLabels(): LabelGroup { val labelGroups = transaction { - Labels.selectAll().map(::resultRowToLabel).groupBy { it.type } + 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() + val consumeLabels = labelGroups[LabelType.CLASS] + ?.map { + it.vo().apply { + this.labels = labelGroups[LabelType.LABEL] + ?.filter { it2 -> it2.relativedId == it.id } + ?.map(Label::vo) + ?: emptyList() + } + } ?: emptyList() + val incomeLabels = labelGroups[LabelType.CLASS] + ?.map { + it.vo().apply { + this.labels = labelGroups[LabelType.INCOME_CLASS] + ?.filter { it2 -> it2.relativedId == it.id } + ?.map(Label::vo) + ?: emptyList() + } + } ?: emptyList() + return LabelGroup(income = incomeLabels, consume = consumeLabels) } override suspend fun addLabel(labelType: LabelType, label: Label): Int = transaction { @@ -51,5 +66,4 @@ class LabelServiceImpl : LabelService { } } } - } \ No newline at end of file