Skip to content

Commit

Permalink
feat: complete strategy controller
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiinaKin committed Sep 23, 2024
1 parent 86ed885 commit aeb1cfd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 20 deletions.
76 changes: 57 additions & 19 deletions app/src/main/kotlin/io/sakurasou/controller/StrategyController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,33 @@ import io.github.smiley4.ktorswaggerui.dsl.routing.post
import io.github.smiley4.ktorswaggerui.dsl.routing.route
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.routing.*
import io.sakurasou.constant.STRATEGY_DELETE
import io.sakurasou.constant.STRATEGY_READ_ALL
import io.sakurasou.constant.STRATEGY_READ_SINGLE
import io.sakurasou.constant.STRATEGY_WRITE
import io.sakurasou.controller.request.PageRequest
import io.sakurasou.controller.request.StrategyInsertRequest
import io.sakurasou.controller.request.StrategyPatchRequest
import io.sakurasou.controller.vo.CommonResponse
import io.sakurasou.controller.vo.PageResult
import io.sakurasou.controller.vo.StrategyPageVO
import io.sakurasou.controller.vo.StrategyVO
import io.sakurasou.exception.WrongParameterException
import io.sakurasou.extension.pageRequest
import io.sakurasou.extension.success
import io.sakurasou.plugins.AuthorizationPlugin
import io.sakurasou.service.strategy.StrategyService

/**
* @author Shiina Kin
* 2024/9/9 08:59
*/
fun Route.strategyRoute() {
fun Route.strategyRoute(strategyService: StrategyService) {
val controller = StrategyController(strategyService)
route("strategy") {
insertStrategy()
insertStrategy(controller)
route("{id}", {
protected = true
request {
Expand All @@ -43,15 +49,15 @@ fun Route.strategyRoute() {
}
}
}) {
deleteStrategy()
updateStrategy()
fetchStrategy()
deleteStrategy(controller)
patchStrategy(controller)
fetchStrategy(controller)
}
pageStrategies()
pageStrategies(controller)
}
}

private fun Route.insertStrategy() {
private fun Route.insertStrategy(controller: StrategyController) {
route {
install(AuthorizationPlugin) {
permission = STRATEGY_WRITE
Expand All @@ -71,12 +77,14 @@ private fun Route.insertStrategy() {
}
}
}) {
TODO()
val insertRequest = call.receive<StrategyInsertRequest>()
controller.handleInsertStrategy(insertRequest)
call.success()
}
}
}

private fun Route.deleteStrategy() {
private fun Route.deleteStrategy(controller: StrategyController) {
route {
install(AuthorizationPlugin) {
permission = STRATEGY_DELETE
Expand All @@ -89,12 +97,14 @@ private fun Route.deleteStrategy() {
}
}
}) {
TODO()
val id = call.parameters["id"]?.toLong() ?: throw WrongParameterException()
controller.handleDeleteStrategy(id)
call.success()
}
}
}

private fun Route.updateStrategy() {
private fun Route.patchStrategy(controller: StrategyController) {
route {
install(AuthorizationPlugin) {
permission = STRATEGY_WRITE
Expand All @@ -112,12 +122,15 @@ private fun Route.updateStrategy() {
}
}
}) {
TODO()
val id = call.parameters["id"]?.toLong() ?: throw WrongParameterException()
val patchRequest = call.receive<StrategyPatchRequest>()
controller.handlePatchStrategy(id, patchRequest)
call.success()
}
}
}

private fun Route.fetchStrategy() {
private fun Route.fetchStrategy(controller: StrategyController) {
route {
install(AuthorizationPlugin) {
permission = STRATEGY_READ_SINGLE
Expand All @@ -130,12 +143,14 @@ private fun Route.fetchStrategy() {
}
}
}) {
TODO()
val id = call.parameters["id"]?.toLong() ?: throw WrongParameterException()
val strategyVO = controller.handleFetchStrategy(id)
call.success(strategyVO)
}
}
}

private fun Route.pageStrategies() {
private fun Route.pageStrategies(controller: StrategyController) {
route {
install(AuthorizationPlugin) {
permission = STRATEGY_READ_ALL
Expand All @@ -154,12 +169,35 @@ private fun Route.pageStrategies() {
}
}
}) {
val pageVO = call.pageRequest()

TODO()
val pageRequest = call.pageRequest()
val pageResult = controller.handlePageStrategies(pageRequest)
call.success(pageResult)
}
}
}

class StrategyController {
class StrategyController(
private val strategyService: StrategyService
) {
suspend fun handleInsertStrategy(request: StrategyInsertRequest) {
strategyService.saveStrategy(request)
}

suspend fun handleDeleteStrategy(id: Long) {
strategyService.deleteStrategy(id)
}

suspend fun handlePatchStrategy(id: Long, request: StrategyPatchRequest) {
strategyService.updateStrategy(id, request)
}

suspend fun handleFetchStrategy(id: Long): StrategyVO {
val strategy = strategyService.fetchStrategy(id)
return strategy
}

suspend fun handlePageStrategies(pageRequest: PageRequest): PageResult<StrategyPageVO> {
val pageResult = strategyService.pageStrategies(pageRequest)
return pageResult
}
}
6 changes: 5 additions & 1 deletion app/src/main/kotlin/io/sakurasou/plugins/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@ import io.ktor.server.plugins.statuspages.*
import io.ktor.server.resources.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.util.logging.*
import io.sakurasou.config.InstanceCenter.authService
import io.sakurasou.config.InstanceCenter.commonService
import io.sakurasou.config.InstanceCenter.roleService
import io.sakurasou.config.InstanceCenter.settingService
import io.sakurasou.config.InstanceCenter.strategyService
import io.sakurasou.config.InstanceCenter.userService
import io.sakurasou.controller.*
import io.sakurasou.exception.ServiceThrowable
import io.sakurasou.exception.SiteNotInitializationException
import io.sakurasou.extension.failure
import io.sakurasou.extension.isSiteNotInitialized
import org.jetbrains.exposed.sql.exposedLogger

fun Application.configureRouting() {
install(Resources)
install(StatusPages) {
exception<Throwable> { call, cause ->
exposedLogger.error(cause)
call.respondText(text = "500: $cause", status = HttpStatusCode.InternalServerError)
}
exception<ServiceThrowable> { call: ApplicationCall, cause ->
Expand All @@ -47,7 +51,7 @@ fun Application.configureRouting() {
authenticate("auth-jwt") {
imageRoute()
albumRoute()
strategyRoute()
strategyRoute(strategyService)
settingRoute(settingService)
userRoute(userService)
groupRoute()
Expand Down

0 comments on commit aeb1cfd

Please sign in to comment.