feat🔫: 更改get /label/接口,可以获取year,month,day,并且在readme上编写了api接口文档

This commit is contained in:
车厘子 2022-12-09 12:50:53 +08:00
parent 203dc02805
commit 69b740f8c3
6 changed files with 73 additions and 32 deletions

View File

@ -1,11 +1,48 @@
# Ktor 项目模板
# Ktor 项目
> Kotlin + Gradle + Ktor + Exposed + Docker
配置好啦
## api
- Database
- Logging
- Routing
- Session
- WebSocket
### 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?,
}
```

View File

@ -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

View File

@ -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<List<BillVO>>().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)

View File

@ -3,6 +3,7 @@ package cn.fadinglight.services
import cn.fadinglight.models.Bill
interface BillService {
suspend fun getManyBills(year: String): List<Bill>
suspend fun getManyBills(year: String, month: String): List<Bill>
suspend fun getManyBills(year: String, month: String, day: String): List<Bill>
suspend fun getManyBills(): List<Bill>

View File

@ -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<Bill> = transaction {
Bills
.select(Bills.date like "${year}%")
.map(::resultRowToBill)
}
override suspend fun getManyBills(year: String, month: String): List<Bill> = transaction {
Bills
.select(Bills.date like "%${year}-${formatSingletonNumber(month)}%")

View File

@ -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