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'
|
mongo_url = 'http://www.fadinglight.cn:8088/class'
|
||||||
mysql_url = 'http://localhost:8080/api/v1/label/'
|
mysql_url = 'http://localhost:8080/api/v1/label/'
|
||||||
|
|
||||||
|
|
||||||
async def getMongoBills():
|
async def getMongoBills():
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.get(mongo_url) as response:
|
async with session.get(mongo_url) as response:
|
||||||
data = await response.json()
|
data = await response.json()
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
async def postBillsToMysql(bills):
|
async def postBillsToMysql(bills):
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.post(mysql_url, json=bills) as response:
|
async with session.post(mysql_url, json=bills) as response:
|
||||||
data = await response.json()
|
data = await response.json()
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def dealOneBill(bill):
|
def dealOneBill(bill):
|
||||||
bill.setdefault('type', 0)
|
bill.setdefault('type', 0)
|
||||||
return dict(
|
return dict(
|
||||||
|
@ -27,10 +30,16 @@ def dealOneBill(bill):
|
||||||
type="consume" if bill['type'] == 0 else "income",
|
type="consume" if bill['type'] == 0 else "income",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
data = await getMongoBills()
|
data = await getMongoBills()
|
||||||
labels = data['data']
|
labels = data['data']
|
||||||
print(labels)
|
consumeLabels = labels['consume']
|
||||||
|
incomeLabels = labels['income']
|
||||||
|
|
||||||
|
# for i in consumeLabels:
|
||||||
|
|
||||||
|
|
||||||
# bills = list(map(dealOneBill, bills))
|
# bills = list(map(dealOneBill, bills))
|
||||||
# respData = await postBillsToMysql(bills)
|
# respData = await postBillsToMysql(bills)
|
||||||
# print(respData)
|
# print(respData)
|
||||||
|
|
|
@ -12,5 +12,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 relativeId: Int?,
|
||||||
)
|
)
|
|
@ -1,5 +1,6 @@
|
||||||
package cn.fadinglight.plugins
|
package cn.fadinglight.plugins
|
||||||
|
|
||||||
|
import cn.fadinglight.models.LabelType
|
||||||
import cn.fadinglight.routes.billRoute
|
import cn.fadinglight.routes.billRoute
|
||||||
import cn.fadinglight.routes.labelRoute
|
import cn.fadinglight.routes.labelRoute
|
||||||
import io.ktor.server.application.*
|
import io.ktor.server.application.*
|
||||||
|
@ -10,7 +11,7 @@ fun Application.configureRouting() {
|
||||||
routing {
|
routing {
|
||||||
route("/") {
|
route("/") {
|
||||||
get {
|
get {
|
||||||
call.respondText("Welcome che's Bill App!")
|
call.respond(LabelType.LABEL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
route("/api/v1") {
|
route("/api/v1") {
|
||||||
|
|
|
@ -2,8 +2,10 @@ package cn.fadinglight.routes
|
||||||
|
|
||||||
import cn.fadinglight.libs.Resp
|
import cn.fadinglight.libs.Resp
|
||||||
import cn.fadinglight.services.LabelServiceImpl
|
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.application.*
|
||||||
|
import io.ktor.server.request.*
|
||||||
import io.ktor.server.response.*
|
import io.ktor.server.response.*
|
||||||
import io.ktor.server.routing.*
|
import io.ktor.server.routing.*
|
||||||
|
|
||||||
|
@ -18,6 +20,15 @@ fun Route.labelRoute() {
|
||||||
).json()
|
).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.Label
|
||||||
import cn.fadinglight.models.LabelType
|
import cn.fadinglight.models.LabelType
|
||||||
import cn.fadinglight.vo.LabelGroup
|
import cn.fadinglight.vo.LabelGroup
|
||||||
|
import cn.fadinglight.vo.LabelPost
|
||||||
|
|
||||||
interface LabelService {
|
interface LabelService {
|
||||||
suspend fun getLabels(): LabelGroup
|
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 deleteLabel(labelId: Int): Int
|
||||||
suspend fun addCount(labelId: Int): Int
|
suspend fun addCount(labelId: Int): Int
|
||||||
}
|
}
|
|
@ -1,11 +1,11 @@
|
||||||
package cn.fadinglight.services
|
package cn.fadinglight.services
|
||||||
|
|
||||||
import cn.fadinglight.vo.LabelVO
|
|
||||||
import cn.fadinglight.vo.vo
|
import cn.fadinglight.vo.vo
|
||||||
import cn.fadinglight.mapers.Labels
|
import cn.fadinglight.mapers.Labels
|
||||||
import cn.fadinglight.models.Label
|
import cn.fadinglight.models.Label
|
||||||
import cn.fadinglight.models.LabelType
|
import cn.fadinglight.models.LabelType
|
||||||
import cn.fadinglight.vo.LabelGroup
|
import cn.fadinglight.vo.LabelGroup
|
||||||
|
import cn.fadinglight.vo.LabelPost
|
||||||
import org.jetbrains.exposed.sql.*
|
import org.jetbrains.exposed.sql.*
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ class LabelServiceImpl : LabelService {
|
||||||
type = LabelType.valueOf(row[Labels.type]),
|
type = LabelType.valueOf(row[Labels.type]),
|
||||||
name = row[Labels.name],
|
name = row[Labels.name],
|
||||||
count = row[Labels.count],
|
count = row[Labels.count],
|
||||||
relativedId = row[Labels.relativeId]
|
relativeId = row[Labels.relativeId]
|
||||||
)
|
)
|
||||||
|
|
||||||
override suspend fun getLabels(): LabelGroup {
|
override suspend fun getLabels(): LabelGroup {
|
||||||
|
@ -25,11 +25,11 @@ class LabelServiceImpl : LabelService {
|
||||||
.map(::resultRowToLabel)
|
.map(::resultRowToLabel)
|
||||||
.groupBy { it.type }
|
.groupBy { it.type }
|
||||||
}
|
}
|
||||||
val consumeLabels = labelGroups[LabelType.CLASS]
|
val consumeLabels = labelGroups[LabelType.CLASS]
|
||||||
?.map {
|
?.map {
|
||||||
it.vo().apply {
|
it.vo().apply {
|
||||||
this.labels = labelGroups[LabelType.LABEL]
|
this.labels = labelGroups[LabelType.LABEL]
|
||||||
?.filter { it2 -> it2.relativedId == it.id }
|
?.filter { it2 -> it2.relativeId == it.id }
|
||||||
?.map(Label::vo)
|
?.map(Label::vo)
|
||||||
?: emptyList()
|
?: emptyList()
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ class LabelServiceImpl : LabelService {
|
||||||
?.map {
|
?.map {
|
||||||
it.vo().apply {
|
it.vo().apply {
|
||||||
this.labels = labelGroups[LabelType.INCOME_CLASS]
|
this.labels = labelGroups[LabelType.INCOME_CLASS]
|
||||||
?.filter { it2 -> it2.relativedId == it.id }
|
?.filter { it2 -> it2.relativeId == it.id }
|
||||||
?.map(Label::vo)
|
?.map(Label::vo)
|
||||||
?: emptyList()
|
?: emptyList()
|
||||||
}
|
}
|
||||||
|
@ -46,13 +46,14 @@ class LabelServiceImpl : LabelService {
|
||||||
return LabelGroup(income = incomeLabels, consume = consumeLabels)
|
return LabelGroup(income = incomeLabels, consume = consumeLabels)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun addLabel(labelType: LabelType, label: Label): Int = transaction {
|
override suspend fun addLabel(label: Label): Int = transaction {
|
||||||
Labels.insertAndGetId {
|
Labels
|
||||||
it[type] = label.type.name
|
.insertAndGetId {
|
||||||
it[name] = label.name
|
it[type] = label.type.name
|
||||||
it[count] = label.count
|
it[name] = label.name
|
||||||
it[relativeId] = label.relativedId
|
it[count] = 0
|
||||||
}.value
|
it[relativeId] = label.relativeId
|
||||||
|
}.value
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun deleteLabel(labelId: Int): Int {
|
override suspend fun deleteLabel(labelId: Int): Int {
|
||||||
|
|
|
@ -8,7 +8,7 @@ import java.util.*
|
||||||
@Serializable
|
@Serializable
|
||||||
data class BillVO(
|
data class BillVO(
|
||||||
val id: Int? = null,
|
val id: Int? = null,
|
||||||
val type: String,
|
val type: BillType,
|
||||||
val date: String,
|
val date: String,
|
||||||
val money: Float,
|
val money: Float,
|
||||||
val cls: String,
|
val cls: String,
|
||||||
|
@ -18,7 +18,7 @@ data class BillVO(
|
||||||
|
|
||||||
fun BillVO.bill() = Bill(
|
fun BillVO.bill() = Bill(
|
||||||
id = id,
|
id = id,
|
||||||
type = BillType.valueOf(type.uppercase(Locale.getDefault())),
|
type = type,
|
||||||
date = date,
|
date = date,
|
||||||
money = money,
|
money = money,
|
||||||
cls = cls,
|
cls = cls,
|
||||||
|
@ -28,7 +28,7 @@ fun BillVO.bill() = Bill(
|
||||||
|
|
||||||
fun Bill.vo() = BillVO(
|
fun Bill.vo() = BillVO(
|
||||||
id = id,
|
id = id,
|
||||||
type = type.name,
|
type = type,
|
||||||
date = date,
|
date = date,
|
||||||
money = money,
|
money = money,
|
||||||
cls = cls,
|
cls = cls,
|
||||||
|
|
|
@ -4,6 +4,22 @@ import cn.fadinglight.models.Label
|
||||||
import cn.fadinglight.models.LabelType
|
import cn.fadinglight.models.LabelType
|
||||||
import kotlinx.serialization.Serializable
|
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
|
@Serializable
|
||||||
data class LabelVO(
|
data class LabelVO(
|
||||||
val name: String,
|
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(
|
fun Label.vo() = LabelVO(
|
||||||
name = name,
|
name = name,
|
||||||
count = count,
|
count = count,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user