From 69b740f8c3e5e7b19835bf554dd5dcc12a16d8be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A6=E5=8E=98=E5=AD=90?= Date: Fri, 9 Dec 2022 12:50:53 +0800 Subject: [PATCH] =?UTF-8?q?feat=F0=9F=94=AB:=20=E6=9B=B4=E6=94=B9get=20/la?= =?UTF-8?q?bel/=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=8F=AF=E4=BB=A5=E8=8E=B7?= =?UTF-8?q?=E5=8F=96year=EF=BC=8Cmonth=EF=BC=8Cday=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E4=B8=94=E5=9C=A8readme=E4=B8=8A=E7=BC=96=E5=86=99=E4=BA=86api?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 51 ++++++++++++++++--- docker-compose.yaml | 16 +++--- .../cn/fadinglight/routes/BillRoutes.kt | 23 +++++---- .../cn/fadinglight/services/BillService.kt | 1 + .../fadinglight/services/BillServiceImpl.kt | 12 ++--- src/main/resources/db.properties | 2 +- 6 files changed, 73 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 34cb194..aa73a4f 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,48 @@ -# Ktor 项目模板 +# Ktor 项目 > Kotlin + Gradle + Ktor + Exposed + Docker -配置好啦 +## api -- Database -- Logging -- Routing -- Session -- WebSocket \ No newline at end of file +### bill + +```bash +GET /api/v1/bill/{year}/{month?}/{day?} + +POST /api/v1/bill/ +Body List<{ + type: Int, + date: String, + money: Float, + cls: String, + label: String, + options: String, +}> + +DELETE /api/v1/bill/{id} + +PUT /api/v1/bill/ +Body { + id: Int?, + type: Int, + date: String, + money: Float, + cls: String, + label: String, + options: String, +} + +``` + +### label + +```bash +GET /api/v1/label/ + +POST /api/v1/label/ +Body { + type: String, + name: String, + relativeId: Int?, +} +``` \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 511ccf2..e7cf2d9 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,12 +1,12 @@ services: - back: - container_name: bill-ktor - image: registry.cn-hangzhou.aliyuncs.com/fadinglight/bill-ktor:dev - restart: always - environment: - JDBC_URL: mariadb://db/bill - ports: - - 80:4000 +# back: +# container_name: bill-ktor +# image: registry.cn-hangzhou.aliyuncs.com/fadinglight/bill-ktor:dev +# restart: always +# environment: +# JDBC_URL: mariadb://db/bill +# ports: +# - 80:4000 db: container_name: bill-test-db diff --git a/src/main/kotlin/cn/fadinglight/routes/BillRoutes.kt b/src/main/kotlin/cn/fadinglight/routes/BillRoutes.kt index f6edf91..f20b1e0 100644 --- a/src/main/kotlin/cn/fadinglight/routes/BillRoutes.kt +++ b/src/main/kotlin/cn/fadinglight/routes/BillRoutes.kt @@ -14,22 +14,25 @@ import io.ktor.server.util.* fun Route.billRoute() { val billService = BillServiceImpl() route("/bill") { - get("/{year}/{month}") { + get("/{year}/{month?}/{day?}") { runCatching { - val year = call.parameters["year"]!! - val month = call.parameters["month"]!! - billService.getManyBills(year, month) + val year = call.parameters.getOrFail("year") + val month = call.parameters["month"] + val day = call.parameters["day"] + if (month is String && day is String) { + billService.getManyBills(year, month, day) + } else if (month is String) { + billService.getManyBills(year, month) + } else { + billService.getManyBills(year) + } }.onSuccess { call.respond(Resp.Ok(it).json()) }.onFailure { - call.respond(Resp.Error(it.message, code = -1).json()) + call.respond(Resp.Error(it.message).json()) } } - get("/") { - call.respond(status = HttpStatusCode.OK, Resp.Ok(billService.getManyBills()).json()) - } - post("/") { runCatching { val bills = call.receive>().map(BillVO::bill) @@ -42,7 +45,7 @@ fun Route.billRoute() { } } - delete("{id?}") { + delete("{id}") { runCatching { val id = call.parameters.getOrFail("id").toInt() billService.deleteOneBill(id) diff --git a/src/main/kotlin/cn/fadinglight/services/BillService.kt b/src/main/kotlin/cn/fadinglight/services/BillService.kt index d95360a..7f2017f 100644 --- a/src/main/kotlin/cn/fadinglight/services/BillService.kt +++ b/src/main/kotlin/cn/fadinglight/services/BillService.kt @@ -3,6 +3,7 @@ package cn.fadinglight.services import cn.fadinglight.models.Bill interface BillService { + suspend fun getManyBills(year: String): List suspend fun getManyBills(year: String, month: String): List suspend fun getManyBills(year: String, month: String, day: String): List suspend fun getManyBills(): List diff --git a/src/main/kotlin/cn/fadinglight/services/BillServiceImpl.kt b/src/main/kotlin/cn/fadinglight/services/BillServiceImpl.kt index ddbcb90..c003ddb 100644 --- a/src/main/kotlin/cn/fadinglight/services/BillServiceImpl.kt +++ b/src/main/kotlin/cn/fadinglight/services/BillServiceImpl.kt @@ -5,20 +5,14 @@ import cn.fadinglight.mapers.Labels import cn.fadinglight.models.Bill import cn.fadinglight.models.BillType import cn.fadinglight.models.LabelType -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.awaitAll -import kotlinx.coroutines.coroutineScope import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList import org.jetbrains.exposed.sql.SqlExpressionBuilder.like -import org.jetbrains.exposed.sql.SqlExpressionBuilder.neq -import org.jetbrains.exposed.sql.transactions.experimental.suspendedTransactionAsync import org.jetbrains.exposed.sql.transactions.transaction class BillServiceImpl : BillService { - private val labelService = LabelServiceImpl() private fun formatSingletonNumber(n: String) = if (n.length == 1) { "0$n" } else { @@ -35,6 +29,12 @@ class BillServiceImpl : BillService { options = row[Bills.options] ) + override suspend fun getManyBills(year: String): List = transaction { + Bills + .select(Bills.date like "${year}%") + .map(::resultRowToBill) + } + override suspend fun getManyBills(year: String, month: String): List = transaction { Bills .select(Bills.date like "%${year}-${formatSingletonNumber(month)}%") diff --git a/src/main/resources/db.properties b/src/main/resources/db.properties index 0b82a68..bc64379 100644 --- a/src/main/resources/db.properties +++ b/src/main/resources/db.properties @@ -1,4 +1,4 @@ driverClassName=org.mariadb.jdbc.Driver -jdbcUrl=jdbc:mariadb://www.fadinglight.cn:3306/bill +jdbcUrl=jdbc:mariadb://localhost:3306/bill dataSource.user=root dataSource.password=123456 \ No newline at end of file