Skip to content

Commit

Permalink
feat: modify the controller's routing information for authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiinaKin committed Sep 12, 2024
1 parent 6a9803a commit 7d9d2f1
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 117 deletions.
113 changes: 92 additions & 21 deletions app/src/main/kotlin/io/sakurasou/controller/AlbumController.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package io.sakurasou.controller

import io.github.smiley4.ktorswaggerui.dsl.routing.delete
import io.github.smiley4.ktorswaggerui.dsl.routing.get
import io.github.smiley4.ktorswaggerui.dsl.routing.patch
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.routing.*
import io.sakurasou.constant.*
import io.sakurasou.controller.request.AlbumInsertRequest
import io.sakurasou.controller.request.AlbumPatchRequest
import io.sakurasou.controller.request.pageRequest
import io.sakurasou.controller.request.AlbumSelfPatchRequest
import io.sakurasou.controller.vo.AlbumPageVO
import io.sakurasou.controller.vo.AlbumVO
import io.sakurasou.controller.vo.CommonResponse
import io.sakurasou.controller.vo.PageResult
import io.sakurasou.extension.*

/**
* @author Shiina Kin
Expand All @@ -23,7 +21,6 @@ import io.sakurasou.controller.vo.PageResult
fun Route.albumRoute() {
route("album") {
post({
protected = true
request {
body<AlbumInsertRequest> {
required = true
Expand All @@ -35,23 +32,17 @@ fun Route.albumRoute() {
body<CommonResponse<Unit>> { }
}
}
}) {
}, ALBUM_WRITE_SELF) {
TODO()
}
route("{id}", {
route("{albumId}", {
protected = true
request {
pathParameter<Long>("id") {
pathParameter<Long>("album id") {
description = "album id"
required = true
}
}
response {
HttpStatusCode.NotFound to {
description = "album not found"
body<CommonResponse<Unit>> { }
}
}
}) {
delete({
response {
Expand All @@ -60,12 +51,12 @@ fun Route.albumRoute() {
body<CommonResponse<Unit>> { }
}
}
}) {

}, ALBUM_DELETE_SELF) {
TODO()
}
patch({
request {
body<AlbumPatchRequest> {
body<AlbumSelfPatchRequest> {
required = true
}
}
Expand All @@ -75,7 +66,7 @@ fun Route.albumRoute() {
body<CommonResponse<Unit>> { }
}
}
}) {
}, ALBUM_WRITE_SELF) {
TODO()
}
get({
Expand All @@ -85,7 +76,7 @@ fun Route.albumRoute() {
body<CommonResponse<AlbumVO>> { }
}
}
}) {
}, ALBUM_READ_SELF_SINGLE) {
TODO()
}
}
Expand All @@ -102,11 +93,91 @@ fun Route.albumRoute() {
description = "page or pageSize wrong"
}
}
}) {
}, ALBUM_READ_SELF_ALL) {
val pageVO = call.pageRequest()

TODO()
}
route("all", {
protected = true
request {
pathParameter<Long>("userId") {
description = "user id"
required = true
}
}
response {
HttpStatusCode.NotFound to {
description = "album not found"
body<CommonResponse<Unit>> { }
}
}
}) {
route("{albumId}", {
protected = true
request {
pathParameter<Long>("album id") {
description = "album id"
required = true
}
}
}) {
delete({
response {
HttpStatusCode.OK to {
description = "success"
body<CommonResponse<Unit>> { }
}
}
}, ALBUM_DELETE_ALL) {
TODO()
}
patch({
request {
body<AlbumPatchRequest> {
required = true
}
}
response {
HttpStatusCode.OK to {
description = "success"
body<CommonResponse<Unit>> { }
}
}
}, ALBUM_WRITE_ALL) {
TODO()
}
get({
response {
HttpStatusCode.OK to {
description = "success"
body<CommonResponse<AlbumVO>> { }
}
}
}, ALBUM_READ_ALL_SINGLE) {
TODO()
}
}
get("page", {
pageRequest()
response {
HttpStatusCode.OK to {
description = "success"
body<PageResult<AlbumPageVO>> {
description = "page result"
}
}
HttpStatusCode.BadRequest to {
description = "page or pageSize wrong"
}
}
}, ALBUM_READ_ALL_ALL) {
val pageVO = call.pageRequest()

TODO()
}
}

}
}

Expand Down
61 changes: 61 additions & 0 deletions app/src/main/kotlin/io/sakurasou/controller/AuthController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.sakurasou.controller

import io.github.smiley4.ktorswaggerui.dsl.routing.post
import io.github.smiley4.ktorswaggerui.dsl.routing.route
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.sakurasou.controller.request.UserInsertRequest
import io.sakurasou.controller.request.UserLoginRequest
import io.sakurasou.model.dao.relation.RelationDao
import io.sakurasou.model.dao.user.UserDao
import io.sakurasou.service.auth.AuthService
import io.sakurasou.service.auth.AuthServiceImpl

/**
* @author Shiina Kin
* 2024/9/12 10:14
*/
fun Route.authRoute(userDao: UserDao, relationDao: RelationDao) {
val authService = AuthServiceImpl(userDao, relationDao)
val authController = AuthController(authService)
route("user", {
protected = false
}) {
post("login", {
request {
body<UserLoginRequest> {
required = true
}
}
}) {
val loginRequest = call.receive<UserLoginRequest>()
val token = authController.handleLogin(loginRequest)
call.respond(mapOf("token" to token))
}
post("signup", {
protected = false
request {
body<UserInsertRequest> {
required = true
}
}
}) {
val userInsertRequest = call.receive<UserInsertRequest>()
authController.handleSignup(userInsertRequest)
}
}
}

class AuthController(
private val authService: AuthService
) {
suspend fun handleLogin(loginRequest: UserLoginRequest): String {
return authService.login(loginRequest)
}

suspend fun handleSignup(userInsertRequest: UserInsertRequest) {
authService.saveUser(userInsertRequest)
}
}
18 changes: 11 additions & 7 deletions app/src/main/kotlin/io/sakurasou/controller/GroupController.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package io.sakurasou.controller

import io.github.smiley4.ktorswaggerui.dsl.routing.*
import io.github.smiley4.ktorswaggerui.dsl.routing.route
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.routing.*
import io.sakurasou.constant.GROUP_DELETE
import io.sakurasou.constant.GROUP_READ_ALL
import io.sakurasou.constant.GROUP_READ_SINGLE
import io.sakurasou.constant.GROUP_WRITE
import io.sakurasou.controller.request.GroupInsertRequest
import io.sakurasou.controller.request.GroupPatchRequest
import io.sakurasou.controller.request.pageRequest
import io.sakurasou.controller.vo.CommonResponse
import io.sakurasou.controller.vo.GroupPageVO
import io.sakurasou.controller.vo.GroupVO
import io.sakurasou.controller.vo.PageResult
import io.sakurasou.extension.*

/**
* @author Shiina Kin
Expand All @@ -36,7 +40,7 @@ fun Route.groupRoute() {
body<CommonResponse<Unit>> { }
}
}
}) {
}, GROUP_WRITE) {
TODO()
}
route("{id}", {
Expand All @@ -60,7 +64,7 @@ fun Route.groupRoute() {
body<CommonResponse<Unit>> { }
}
}
}) {
}, GROUP_DELETE) {
TODO()
}
patch({
Expand All @@ -75,7 +79,7 @@ fun Route.groupRoute() {
body<CommonResponse<Unit>> { }
}
}
}) {
}, GROUP_WRITE) {
TODO()
}
get({
Expand All @@ -85,7 +89,7 @@ fun Route.groupRoute() {
body<CommonResponse<GroupVO>> { }
}
}
}) {
}, GROUP_READ_SINGLE) {
TODO()
}
}
Expand All @@ -102,7 +106,7 @@ fun Route.groupRoute() {
description = "page or pageSize wrong"
}
}
}) {
}, GROUP_READ_ALL) {
val pageVO = call.pageRequest()

TODO()
Expand Down
Loading

0 comments on commit 7d9d2f1

Please sign in to comment.