to: fix api
This commit is contained in:
parent
ba9adfe799
commit
b4c10559d1
|
@ -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())
|
||||
asyncio.run(main())
|
||||
|
|
|
@ -12,5 +12,5 @@ data class Label(
|
|||
val type: LabelType,
|
||||
val name: String,
|
||||
var count: Int,
|
||||
val relativedId: Int?,
|
||||
val relativeId: Int?,
|
||||
)
|
|
@ -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") {
|
||||
|
|
|
@ -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<LabelPost>().label()
|
||||
}.onSuccess {
|
||||
val labelId = labelService.addLabel(it)
|
||||
call.respond(Resp.Ok(labelId).json())
|
||||
}.onFailure {
|
||||
call.respond(Resp.Error(it.message).json())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue
Block a user