Skip to content

Commit

Permalink
refactor:规范DAO和Entity命名
Browse files Browse the repository at this point in the history
  • Loading branch information
shaoyuanyu committed Nov 2, 2024
1 parent abf7fac commit 12b97dd
Show file tree
Hide file tree
Showing 29 changed files with 76 additions and 71 deletions.
4 changes: 2 additions & 2 deletions app/src/main/kotlin/cn/kzoj/app/plugins/Security.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package cn.kzoj.app.plugins
import cn.kzoj.persistence.UserService
import cn.kzoj.dto.exception.user.UserAccountValidateException
import cn.kzoj.dto.exception.user.UserAuthorityException
import cn.kzoj.dto.entity.user.UserAuthority
import cn.kzoj.dto.entity.user.UserSession
import cn.kzoj.dto.user.UserAuthority
import cn.kzoj.dto.user.UserSession
import io.ktor.server.application.Application
import io.ktor.server.application.install
import io.ktor.server.auth.Authentication
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/cn/kzoj/app/routes/JudgeRoutes.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.kzoj.app.routes

import cn.kzoj.judge.JudgeDispatcher
import cn.kzoj.dto.entity.judge.SubmitRequest
import cn.kzoj.dto.judge.SubmitRequest
import io.ktor.server.application.Application
import io.ktor.server.auth.authenticate
import io.ktor.server.request.receive
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/kotlin/cn/kzoj/app/routes/ProblemRoutes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import cn.kzoj.persistence.ProblemService
import cn.kzoj.dto.exception.problem.ProblemIdNotIntException
import cn.kzoj.dto.exception.problem.ProblemPageIndexNotPositiveIntException
import cn.kzoj.dto.exception.user.UserAuthorityException
import cn.kzoj.dto.entity.problem.Problem
import cn.kzoj.dto.entity.user.UserAuthority
import cn.kzoj.dto.entity.user.UserSession
import cn.kzoj.dto.problem.Problem
import cn.kzoj.dto.user.UserAuthority
import cn.kzoj.dto.user.UserSession
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.*
import io.ktor.server.auth.authenticate
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/kotlin/cn/kzoj/app/routes/UserRoutes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package cn.kzoj.app.routes

import cn.kzoj.persistence.UserService
import cn.kzoj.dto.exception.user.UserAuthorityException
import cn.kzoj.dto.entity.user.User
import cn.kzoj.dto.entity.user.UserAuthority
import cn.kzoj.dto.entity.user.UserSession
import cn.kzoj.dto.user.User
import cn.kzoj.dto.user.UserAuthority
import cn.kzoj.dto.user.UserSession
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.*
import io.ktor.server.auth.UserIdPrincipal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID

@Suppress("Unused")
class ProblemDAO(id: EntityID<Int>) : IntEntity(id) {
companion object: IntEntityClass<ProblemDAO>(ProblemTable)
class ProblemEntity(id: EntityID<Int>) : IntEntity(id) {
companion object: IntEntityClass<ProblemEntity>(ProblemTable)

var problemId by ProblemTable.problemId
var title by ProblemTable.title
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID

@Suppress("unused")
class ProblemCaseDAO(id: EntityID<Int>) : IntEntity(id) {
companion object: IntEntityClass<ProblemCaseDAO>(ProblemCaseTable)
class ProblemCaseEntity(id: EntityID<Int>) : IntEntity(id) {
companion object: IntEntityClass<ProblemCaseEntity>(ProblemCaseTable)

var pid by ProblemCaseTable.pid
var input by ProblemCaseTable.input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import org.jetbrains.exposed.dao.EntityClass
import org.jetbrains.exposed.dao.id.EntityID

@Suppress("unused")
class UserInfoDAO(uuid: EntityID<String>) : Entity<String>(uuid) {
companion object: EntityClass<String, UserInfoDAO>(UserInfoTable)
class UserInfoEntity(uuid: EntityID<String>) : Entity<String>(uuid) {
companion object: EntityClass<String, UserInfoEntity>(UserInfoTable)

var username by UserInfoTable.username
var password by UserInfoTable.password
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.kzoj.dto.entity.gojudge
package cn.kzoj.dto.gojudge

import kotlinx.serialization.Serializable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.kzoj.dto.entity.gojudge
package cn.kzoj.dto.gojudge

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.kzoj.dto.entity.judge
package cn.kzoj.dto.judge

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.kzoj.dto.entity.judge
package cn.kzoj.dto.judge

import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.kzoj.dto.entity.judge
package cn.kzoj.dto.judge

import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.kzoj.dto.entity.judge
package cn.kzoj.dto.judge

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.kzoj.dto.entity.judge
package cn.kzoj.dto.judge

import kotlinx.serialization.Serializable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.kzoj.dto.entity.judge
package cn.kzoj.dto.judge

import kotlinx.serialization.Serializable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.kzoj.dto.entity.problem
package cn.kzoj.dto.problem

import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.kzoj.dto.entity.problemcase
package cn.kzoj.dto.problemcase

import kotlinx.serialization.Serializable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.kzoj.dto.entity.user
package cn.kzoj.dto.user

import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.kzoj.dto.entity.user
package cn.kzoj.dto.user

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.kzoj.dto.entity.user
package cn.kzoj.dto.user

import kotlinx.serialization.Serializable

Expand Down
12 changes: 6 additions & 6 deletions judge/src/main/kotlin/cn/kzoj/judge/JudgeDispatcher.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cn.kzoj.judge

import cn.kzoj.dto.entity.judge.JudgeStatus
import cn.kzoj.dto.judge.JudgeStatus
import cn.kzoj.persistence.ProblemCaseService
import cn.kzoj.dto.entity.judge.JudgeRequest
import cn.kzoj.dto.entity.judge.SubmitRequest
import cn.kzoj.dto.entity.judge.JudgeResult
import cn.kzoj.dto.entity.problemcase.ProblemCase
import cn.kzoj.dto.entity.judge.SubmitReceipt
import cn.kzoj.dto.judge.JudgeRequest
import cn.kzoj.dto.judge.SubmitRequest
import cn.kzoj.dto.judge.JudgeResult
import cn.kzoj.dto.problemcase.ProblemCase
import cn.kzoj.dto.judge.SubmitReceipt
import cn.kzoj.dto.exception.judge.JudgeIdNotFoundException
import io.ktor.util.logging.KtorSimpleLogger
import kotlinx.coroutines.delay
Expand Down
9 changes: 7 additions & 2 deletions judge/src/main/kotlin/cn/kzoj/judge/SandboxRun.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package cn.kzoj.judge

import cn.kzoj.common.sandbox.LanguageConfig
import cn.kzoj.common.sandbox.SandboxConfig
import cn.kzoj.dto.entity.gojudge.*
import cn.kzoj.dto.entity.judge.JudgeRequest
import cn.kzoj.dto.gojudge.CopyIn
import cn.kzoj.dto.gojudge.File
import cn.kzoj.dto.gojudge.GoJudgeCommand
import cn.kzoj.dto.gojudge.GoJudgeRequest
import cn.kzoj.dto.gojudge.GoJudgeResult
import cn.kzoj.dto.gojudge.Status
import cn.kzoj.dto.judge.JudgeRequest
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.plugins.contentnegotiation.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.kzoj.persistence

import cn.kzoj.dto.entity.problemcase.ProblemCase
import cn.kzoj.persistence.database.problemcase.ProblemCaseDAO
import cn.kzoj.dto.problemcase.ProblemCase
import cn.kzoj.persistence.database.problemcase.ProblemCaseEntity
import cn.kzoj.persistence.database.problemcase.ProblemCaseTable
import cn.kzoj.persistence.database.problemcase.expose
import cn.kzoj.persistence.minio.problemcase.getProblemCaseObject
Expand All @@ -16,7 +16,7 @@ class ProblemCaseService(
) {
suspend fun getProblemCaseList(problemId: Int): List<ProblemCase> =
newSuspendedTransaction(context=Dispatchers.Default, db=database) {
ProblemCaseDAO.find { ProblemCaseTable.problemId eq problemId }.toList().let {
ProblemCaseEntity.find { ProblemCaseTable.problemId eq problemId }.toList().let {
val problemCaseArrayList: ArrayList<ProblemCase> = arrayListOf()
it.forEach { problemCaseArrayList.add(it.expose()) }
problemCaseArrayList.toList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package cn.kzoj.persistence
import cn.kzoj.dto.exception.problem.ProblemIdNotFoundException
import cn.kzoj.dto.exception.problem.ProblemPageIndexOutOfRangeException
import cn.kzoj.dto.exception.problem.ProblemTitleNotFoundException
import cn.kzoj.dto.entity.problem.Problem
import cn.kzoj.persistence.database.problem.ProblemDAO
import cn.kzoj.dto.problem.Problem
import cn.kzoj.persistence.database.problem.ProblemEntity
import cn.kzoj.persistence.database.problem.ProblemTable
import cn.kzoj.persistence.database.problem.expose
import kotlinx.coroutines.Dispatchers
Expand All @@ -17,7 +17,7 @@ class ProblemService(
) {
suspend fun createProblem(newProblem: Problem): Int =
newSuspendedTransaction(context=Dispatchers.Default, db=database) {
ProblemDAO.new {
ProblemEntity.new {
title = newProblem.title
author = newProblem.author
createdByUser = newProblem.createdByUser
Expand All @@ -40,7 +40,7 @@ class ProblemService(

suspend fun deleteProblem(id: Int) {
newSuspendedTransaction(context = Dispatchers.Default, db = database) {
ProblemDAO.findById(id).let {
ProblemEntity.findById(id).let {
if (it == null) {
throw ProblemIdNotFoundException()
}
Expand All @@ -53,7 +53,7 @@ class ProblemService(
@Suppress("DuplicatedCode")
suspend fun updateProblem(newProblem: Problem) {
newSuspendedTransaction(context=Dispatchers.Default, db=database) {
ProblemDAO.findByIdAndUpdate(newProblem.id!!) {
ProblemEntity.findByIdAndUpdate(newProblem.id!!) {
it.title = newProblem.title
it.author = newProblem.author
it.description = newProblem.description
Expand All @@ -79,7 +79,7 @@ class ProblemService(

suspend fun queryProblemById(id: Int): Problem =
newSuspendedTransaction(context=Dispatchers.Default, db=database) {
ProblemDAO.findById(id).let {
ProblemEntity.findById(id).let {
if (it == null) {
throw ProblemIdNotFoundException()
}
Expand All @@ -90,7 +90,7 @@ class ProblemService(

suspend fun queryProblemByTitle(title: String): List<Problem> =
newSuspendedTransaction(context=Dispatchers.Default, db=database) {
ProblemDAO.find { ProblemTable.title like "%$title%" }.also {
ProblemEntity.find { ProblemTable.title like "%$title%" }.also {
if (it.empty()) {
throw ProblemTitleNotFoundException()
}
Expand All @@ -99,13 +99,13 @@ class ProblemService(

suspend fun queryProblemTotality(): Long =
newSuspendedTransaction(context=Dispatchers.Default, db=database) {
ProblemDAO.all().count()
ProblemEntity.all().count()
}

// TODO: 增加orderedBy参数及对应功能
suspend fun queryProblemByPage(pageIndex: Int, pageSize: Int = 20, isAscending: Boolean = true): List<Problem> =
newSuspendedTransaction(context=Dispatchers.Default, db=database) {
ProblemDAO.all().run {
ProblemEntity.all().run {
if (isAscending) {
sortedBy { it.id }
} else {
Expand Down
16 changes: 8 additions & 8 deletions persistence/src/main/kotlin/cn/kzoj/persistence/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package cn.kzoj.persistence
import cn.kzoj.dto.exception.user.UserIdNotFoundException
import cn.kzoj.dto.exception.user.UsernameDuplicatedException
import cn.kzoj.dto.exception.user.UsernameNotFoundException
import cn.kzoj.dto.entity.user.User
import cn.kzoj.persistence.database.user.UserDAO
import cn.kzoj.dto.user.User
import cn.kzoj.persistence.database.user.UserEntity
import cn.kzoj.persistence.database.user.UserTable
import cn.kzoj.persistence.database.user.expose
import cn.kzoj.persistence.database.user.exposeWithoutPasswd
Expand Down Expand Up @@ -32,7 +32,7 @@ class UserService(
suspend fun createUser(newUser: User) =
try {
newSuspendedTransaction(context = Dispatchers.Default, db = database) {
UserDAO.new {
UserEntity.new {
username = newUser.username
encryptedPassword = newUser.plainPassword!! // TODO:加密
school = newUser.school
Expand All @@ -59,7 +59,7 @@ class UserService(
*/
suspend fun queryUserByUUID(uuid: String): User =
newSuspendedTransaction(context=Dispatchers.Default, db=database) {
UserDAO.findById(UUID.fromString(uuid)).let {
UserEntity.findById(UUID.fromString(uuid)).let {
if (it == null) {
throw UserIdNotFoundException()
}
Expand All @@ -75,7 +75,7 @@ class UserService(
suspend fun updateUser(newUser: User) {
try {
newSuspendedTransaction(context=Dispatchers.Default, db=database) {
UserDAO.findByIdAndUpdate(UUID.fromString(newUser.uuid)) {
UserEntity.findByIdAndUpdate(UUID.fromString(newUser.uuid)) {
it.username = newUser.username
it.encryptedPassword = newUser.plainPassword!! // TODO:加密
it.school = newUser.school
Expand All @@ -98,7 +98,7 @@ class UserService(
*/
suspend fun deleteUser(userId: String) {
newSuspendedTransaction(context=Dispatchers.Default, db=database) {
UserDAO.findById(UUID.fromString(userId)).let {
UserEntity.findById(UUID.fromString(userId)).let {
if (it == null) {
throw UserIdNotFoundException()
}
Expand All @@ -113,7 +113,7 @@ class UserService(
*/
suspend fun validate(username: String, password: String): String? =
newSuspendedTransaction(context=Dispatchers.Default, db=database) {
UserDAO.find {
UserEntity.find {
UserTable.username eq username
}.let {
if (it.empty()) {
Expand Down Expand Up @@ -144,7 +144,7 @@ class UserService(
avatarFileInputStream.close()

newSuspendedTransaction(context=Dispatchers.Default, db=database) {
UserDAO.findByIdAndUpdate(UUID.fromString(userId)) {
UserEntity.findByIdAndUpdate(UUID.fromString(userId)) {
it.avatarHashIndex = avatarHashIndex
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cn.kzoj.persistence.database.problem

import cn.kzoj.dto.entity.problem.Problem
import cn.kzoj.dto.problem.Problem
import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID

class ProblemDAO(id: EntityID<Int>) : IntEntity(id) {
companion object: IntEntityClass<ProblemDAO>(ProblemTable)
class ProblemEntity(id: EntityID<Int>) : IntEntity(id) {
companion object: IntEntityClass<ProblemEntity>(ProblemTable)

var title by ProblemTable.title
var author by ProblemTable.author
Expand All @@ -27,7 +27,7 @@ class ProblemDAO(id: EntityID<Int>) : IntEntity(id) {
var utcLastModified by ProblemTable.utcLastModified
}

fun ProblemDAO.expose(): Problem =
fun ProblemEntity.expose(): Problem =
Problem(
id = this.id.value,
title = this.title,
Expand All @@ -49,7 +49,7 @@ fun ProblemDAO.expose(): Problem =
utcLastModified = this.utcLastModified,
)

fun List<ProblemDAO>.expose(): List<Problem> =
fun List<ProblemEntity>.expose(): List<Problem> =
let {
val problemArrayList: ArrayList<Problem> = arrayListOf()
it.forEach { problemArrayList.add(it.expose()) }
Expand Down
Loading

0 comments on commit 12b97dd

Please sign in to comment.