diff --git a/scripts/labels_mongo2mysql.py b/scripts/labels_mongo2mysql.py index 3a7e2ec..064d795 100644 --- a/scripts/labels_mongo2mysql.py +++ b/scripts/labels_mongo2mysql.py @@ -4,18 +4,21 @@ import asyncio mongo_url = 'http://www.fadinglight.cn:8088/class' mysql_url = 'http://localhost:8080/api/v1/label/' + async def getMongoBills(): async with aiohttp.ClientSession() as session: async with session.get(mongo_url) as response: data = await response.json() return data + async def postBillsToMysql(bills): async with aiohttp.ClientSession() as session: async with session.post(mysql_url, json=bills) as response: data = await response.json() return data + def dealOneBill(bill): bill.setdefault('type', 0) return dict( @@ -27,12 +30,18 @@ def dealOneBill(bill): type="consume" if bill['type'] == 0 else "income", ) + async def main(): data = await getMongoBills() labels = data['data'] - print(labels) + consumeLabels = labels['consume'] + incomeLabels = labels['income'] + + # for i in consumeLabels: + + # bills = list(map(dealOneBill, bills)) # respData = await postBillsToMysql(bills) # print(respData) -asyncio.run(main()) \ No newline at end of file +asyncio.run(main()) diff --git a/src/main/kotlin/cn/fadinglight/models/Label.kt b/src/main/kotlin/cn/fadinglight/models/Label.kt index 8f14d9d..91ee643 100644 --- a/src/main/kotlin/cn/fadinglight/models/Label.kt +++ b/src/main/kotlin/cn/fadinglight/models/Label.kt @@ -12,5 +12,5 @@ data class Label( val type: LabelType, val name: String, var count: Int, - val relativedId: Int?, + val relativeId: Int?, ) \ No newline at end of file diff --git a/src/main/kotlin/cn/fadinglight/plugins/Routing.kt b/src/main/kotlin/cn/fadinglight/plugins/Routing.kt index 79d7d84..e9d343b 100644 --- a/src/main/kotlin/cn/fadinglight/plugins/Routing.kt +++ b/src/main/kotlin/cn/fadinglight/plugins/Routing.kt @@ -1,5 +1,6 @@ package cn.fadinglight.plugins +import cn.fadinglight.models.LabelType import cn.fadinglight.routes.billRoute import cn.fadinglight.routes.labelRoute import io.ktor.server.application.* @@ -10,7 +11,7 @@ fun Application.configureRouting() { routing { route("/") { get { - call.respondText("Welcome che's Bill App!") + call.respond(LabelType.LABEL) } } route("/api/v1") { diff --git a/src/main/kotlin/cn/fadinglight/routes/LabelRoutes.kt b/src/main/kotlin/cn/fadinglight/routes/LabelRoutes.kt index 11bd90e..1295904 100644 --- a/src/main/kotlin/cn/fadinglight/routes/LabelRoutes.kt +++ b/src/main/kotlin/cn/fadinglight/routes/LabelRoutes.kt @@ -2,8 +2,10 @@ package cn.fadinglight.routes import cn.fadinglight.libs.Resp import cn.fadinglight.services.LabelServiceImpl -import cn.fadinglight.vo.LabelVO +import cn.fadinglight.vo.LabelPost +import cn.fadinglight.vo.label import io.ktor.server.application.* +import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* @@ -18,6 +20,15 @@ fun Route.labelRoute() { ).json() ) } - post {} + post("/") { + runCatching { + call.receive().label() + }.onSuccess { + val labelId = labelService.addLabel(it) + call.respond(Resp.Ok(labelId).json()) + }.onFailure { + call.respond(Resp.Error(it.message).json()) + } + } } } \ 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 index 65a5157..1126513 100644 --- a/src/main/kotlin/cn/fadinglight/services/LabelService.kt +++ b/src/main/kotlin/cn/fadinglight/services/LabelService.kt @@ -3,10 +3,11 @@ package cn.fadinglight.services import cn.fadinglight.models.Label import cn.fadinglight.models.LabelType import cn.fadinglight.vo.LabelGroup +import cn.fadinglight.vo.LabelPost interface LabelService { suspend fun getLabels(): LabelGroup - suspend fun addLabel(labelType: LabelType, label: Label): Int + suspend fun addLabel(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 index efebd69..0e61b7c 100644 --- a/src/main/kotlin/cn/fadinglight/services/LabelServiceImpl.kt +++ b/src/main/kotlin/cn/fadinglight/services/LabelServiceImpl.kt @@ -1,11 +1,11 @@ package cn.fadinglight.services -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 cn.fadinglight.vo.LabelPost import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.transaction @@ -15,7 +15,7 @@ class LabelServiceImpl : LabelService { type = LabelType.valueOf(row[Labels.type]), name = row[Labels.name], count = row[Labels.count], - relativedId = row[Labels.relativeId] + relativeId = row[Labels.relativeId] ) override suspend fun getLabels(): LabelGroup { @@ -25,11 +25,11 @@ class LabelServiceImpl : LabelService { .map(::resultRowToLabel) .groupBy { it.type } } - val consumeLabels = labelGroups[LabelType.CLASS] + val consumeLabels = labelGroups[LabelType.CLASS] ?.map { it.vo().apply { this.labels = labelGroups[LabelType.LABEL] - ?.filter { it2 -> it2.relativedId == it.id } + ?.filter { it2 -> it2.relativeId == it.id } ?.map(Label::vo) ?: emptyList() } @@ -38,7 +38,7 @@ class LabelServiceImpl : LabelService { ?.map { it.vo().apply { this.labels = labelGroups[LabelType.INCOME_CLASS] - ?.filter { it2 -> it2.relativedId == it.id } + ?.filter { it2 -> it2.relativeId == it.id } ?.map(Label::vo) ?: emptyList() } @@ -46,13 +46,14 @@ class LabelServiceImpl : LabelService { return LabelGroup(income = incomeLabels, consume = consumeLabels) } - 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 addLabel(label: Label): Int = transaction { + Labels + .insertAndGetId { + it[type] = label.type.name + it[name] = label.name + it[count] = 0 + it[relativeId] = label.relativeId + }.value } override suspend fun deleteLabel(labelId: Int): Int { diff --git a/src/main/kotlin/cn/fadinglight/vo/BillVO.kt b/src/main/kotlin/cn/fadinglight/vo/BillVO.kt index f1ce56c..e298243 100644 --- a/src/main/kotlin/cn/fadinglight/vo/BillVO.kt +++ b/src/main/kotlin/cn/fadinglight/vo/BillVO.kt @@ -8,7 +8,7 @@ import java.util.* @Serializable data class BillVO( val id: Int? = null, - val type: String, + val type: BillType, val date: String, val money: Float, val cls: String, @@ -18,7 +18,7 @@ data class BillVO( fun BillVO.bill() = Bill( id = id, - type = BillType.valueOf(type.uppercase(Locale.getDefault())), + type = type, date = date, money = money, cls = cls, @@ -28,7 +28,7 @@ fun BillVO.bill() = Bill( fun Bill.vo() = BillVO( id = id, - type = type.name, + type = type, date = date, money = money, cls = cls, diff --git a/src/main/kotlin/cn/fadinglight/vo/LabelVO.kt b/src/main/kotlin/cn/fadinglight/vo/LabelVO.kt index c0d93d7..71f38ea 100644 --- a/src/main/kotlin/cn/fadinglight/vo/LabelVO.kt +++ b/src/main/kotlin/cn/fadinglight/vo/LabelVO.kt @@ -4,6 +4,22 @@ import cn.fadinglight.models.Label import cn.fadinglight.models.LabelType import kotlinx.serialization.Serializable + +@Serializable +data class LabelPost( + val type: LabelType, + val name: String, + val relativeId: Int?, +) + +fun LabelPost.label() = Label( + id = null, + type = type, + name = name, + count = 0, + relativeId = relativeId +) + @Serializable data class LabelVO( val name: String, @@ -18,14 +34,6 @@ data class LabelGroup( ) -fun LabelVO.label(type: LabelType) = Label( - id = null, - type = type, - name = name, - count = 0, - relativedId = null -) - fun Label.vo() = LabelVO( name = name, count = count,