Skip to content

Commit

Permalink
添加用户数据迁移接口
Browse files Browse the repository at this point in the history
1. 添加用户数据迁移接口(/migrate)
  • Loading branch information
Taskeren committed Nov 25, 2022
1 parent 36f5915 commit 92d2034
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ root = true
charset = utf-8

[{*.java, *.kt}]
indent_style = tab
indent_size = tab
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import explode2.booster.bomb.submods.basic.WelcomeBO
import explode2.booster.bomb.submods.chart.chartModule
import explode2.booster.bomb.submods.chart.setModule
import explode2.booster.bomb.submods.extra.newSongModule
import explode2.booster.bomb.submods.migrate.migrationModule
import explode2.booster.bomb.submods.user.userModule
import explode2.booster.event.KtorModuleEvent
import explode2.booster.event.RouteConfigure
Expand Down Expand Up @@ -151,7 +152,8 @@ private val bombModule: RouteConfigure = {
route("chart", chartModule)
// 上传接口模块
route("upload", newSongModule)

// 数据转移模块
route("migrate", migrationModule)
}

internal suspend fun ApplicationCall.respondJson(content: Any?, typeOfSrc: Type? = null, contentType: ContentType? = null, status: HttpStatusCode? = null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ object Localization {

const val ResourceNotFound = "RESOURCE_NOT_FOUND"

const val InvalidArgument = "INVALID_ARGUMENT"

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ object SuperstarPrincipal : BombPrincipal {
}
override fun calculateLastRecords(limit: Int): List<GameRecord> = emptyList()
override fun calculateBestRecords(limit: Int, sortedBy: ScoreOrRanking): List<GameRecord> = emptyList()
override fun getAllRecords(limit: Int, skip: Int): List<GameRecord> = emptyList()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package explode2.booster.bomb.submods.migrate

import explode2.booster.bomb.Localization
import explode2.booster.bomb.bombCall
import explode2.booster.bomb.respondData
import explode2.booster.bomb.respondError
import explode2.booster.bomb.submods.BombPrincipal
import explode2.booster.bomb.submods.toData
import explode2.booster.bomb.submods.toError
import explode2.booster.bomb.submods.user.toBO
import explode2.booster.event.RouteConfigure
import explode2.gateau.GameRecord
import io.ktor.server.auth.*
import io.ktor.server.routing.*

// [/migrate]
internal val migrationModule: RouteConfigure = {

authenticate {
post {
val user = bombCall.principal<BombPrincipal>()?.user
?: return@post bombCall.respondError(toError(Localization.NotAuthenticatedOrUserNotFound))

val skip = bombCall.parameters["skip"]?.toIntOrNull() ?: 0
val limit = bombCall.parameters["limit"]?.toIntOrNull() ?: 100

val recs = user.getAllRecords(limit, skip)

bombCall.respondData(UserMigrationBO(user.toBO(), recs.map(GameRecord::toBO)).toData())
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package explode2.booster.bomb.submods.migrate

import explode2.booster.bomb.submods.user.RecordBO
import explode2.booster.bomb.submods.user.UserBO

internal data class UserMigrationBO(
val user: UserBO,
val records: List<RecordBO>
)
2 changes: 2 additions & 0 deletions gateau/src/main/kotlin/explode2/gateau/GameUser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ interface GameUser {

fun calculateLastRecords(limit: Int): List<GameRecord>
fun calculateBestRecords(limit: Int, sortedBy: ScoreOrRanking): List<GameRecord>

fun getAllRecords(limit: Int, skip: Int): List<GameRecord>
}
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,10 @@ class MongoManager(private val provider: LabyrinthMongoBuilder = LabyrinthMongoB
).toList().map(::GameRecordWrap)
}

override fun getAllRecords(limit: Int, skip: Int): List<GameRecord> {
return collGameRec.find().skip(skip).limit(limit).map(::GameRecordWrap).toList()
}

override fun toString(): String = delegate.toString()
}

Expand Down

0 comments on commit 92d2034

Please sign in to comment.