Skip to content

Commit

Permalink
重构部分 api (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
h56983577 authored Nov 27, 2023
1 parent 43dfedf commit cfa61a7
Show file tree
Hide file tree
Showing 21 changed files with 474 additions and 35 deletions.
13 changes: 13 additions & 0 deletions cloudapi-model/src/main/kotlin/cn/edu/buaa/scs/model/Host.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cn.edu.buaa.scs.model

data class Host(
val ip: String,
val status: String,
val totalMem: Double,
val usedMem: Double,
val totalCPU: Double,
val usedCPU: Double,
val totalStorage: Long,
val usedStorage: Long,
val count: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ package cn.edu.buaa.scs.controller.models

/**
*
* @param id
* @param id 学工号
* @param departmentId 所在单位
* @param role 1 for student, 2 for teacher, 4 for admin
* @param name 姓名
*/
data class CreateUserRequest(
/* 学工号 */
val id: kotlin.String,
/* 所在单位 */
val departmentId: kotlin.Int,
/* 1 for student, 2 for teacher, 4 for admin */
val role: CreateUserRequest.Role
val role: CreateUserRequest.Role,
/* 姓名 */
val name: kotlin.String? = null
)
{
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* cloudapi_v2
* buaa scs cloud api v2
*
* The version of the OpenAPI document: 2.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package cn.edu.buaa.scs.controller.models


/**
*
* @param id
* @param name
*/
data class DepartmentModel(
val id: kotlin.String,
val name: kotlin.String
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* cloudapi_v2
* buaa scs cloud api v2
*
* The version of the OpenAPI document: 2.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package cn.edu.buaa.scs.controller.models


/**
*
* @param ip
* @param status
* @param totalMem
* @param usedMem
* @param totalCPU
* @param usedCPU
* @param totalStorage
* @param usedStorage
* @param count
*/
data class Host(
val ip: kotlin.String,
val status: kotlin.String,
val totalMem: kotlin.Double,
val usedMem: kotlin.Double,
val totalCPU: kotlin.Double,
val usedCPU: kotlin.Double,
val totalStorage: kotlin.Long,
val usedStorage: kotlin.Long,
val count: kotlin.Int
)

Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ package cn.edu.buaa.scs.controller.models
*
* @param id
* @param name
* @param courseCount
* @param expCount
*/
data class TermModel(
val id: kotlin.Int? = null,
val name: kotlin.String? = null
val name: kotlin.String? = null,
val courseCount: kotlin.Int? = null,
val expCount: kotlin.Int? = null
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* cloudapi_v2
* buaa scs cloud api v2
*
* The version of the OpenAPI document: 2.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package cn.edu.buaa.scs.controller.models


/**
*
* @param name 学期名称
*/
data class TermsGetRequest(
/* 学期名称 */
val name: kotlin.String? = null
)

5 changes: 3 additions & 2 deletions cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/route/Admin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import io.ktor.server.routing.*

fun Route.adminRoute() {
route("/admin") {

route("/user") {
post {
val req = call.receive<CreateUserRequest>()
val role = UserRole.fromLevel(req.role.value)
call.respond(convertUserModel(call.admin.addUser(req.id, role)))
val name = req.name
val departmentId = req.departmentId
call.respond(convertUserModel(call.admin.addUser(req.id, name, role, departmentId)))
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/route/Term.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
package cn.edu.buaa.scs.route

import cn.edu.buaa.scs.controller.models.CreateUserRequest
import cn.edu.buaa.scs.controller.models.TermModel
import cn.edu.buaa.scs.error.BadRequestException
import cn.edu.buaa.scs.model.Term
import cn.edu.buaa.scs.model.UserRole
import cn.edu.buaa.scs.service.admin
import cn.edu.buaa.scs.service.term
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

fun Route.termRoute() {
route("/terms") {
get {
call.respond(call.term.getAllTerms().map { convertTermModel(it) })
call.respond(call.term.getAllTerms())
}

get("/latest") {
call.respond(convertTermModel(call.term.getLatestTerm()))
}

post {
val req = call.receive<TermModel>()
call.term.createTerm(req.name ?: "")
call.respond("OK")
}

delete("/{id}") {
val id = call.parameters["id"] ?: throw BadRequestException("学期 ID 不得为空")
call.term.deleteTerm(id.toInt())
call.respond("OK")
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/route/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ fun Route.userRoute() {
route("/stuffs") {
get {
val search = call.parameters["search"]
call.respond(call.userService.getTeachersAndStudents(search).map { convertUserModel(it) })
val limit = call.parameters["limit"]?.toIntOrNull() ?: 10
call.respond(call.userService.getTeachersAndStudents(search, limit).map { convertUserModel(it) })
}
}

Expand Down Expand Up @@ -54,6 +55,12 @@ fun Route.userRoute() {
}

}

route("/departments") {
get {
call.respond(call.userService.getAllDepartments())
}
}
}

internal fun convertUserModel(user: User): UserModel {
Expand Down
6 changes: 6 additions & 0 deletions cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/route/Vm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ fun Route.vmRoute() {
}
}

route("/hosts") {
get {
call.respond(call.vm.getHosts())
}
}

route("/vms") {
get {
call.respond(call.vm.adminGetAllVms().map { convertVirtualMachineResponse(it) })
Expand Down
4 changes: 2 additions & 2 deletions cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/service/Admin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ val ApplicationCall.admin: AdminService
class AdminService(val call: ApplicationCall) : IService {
companion object : IService.Caller<AdminService>()

fun addUser(id: String, role: UserRole): User {
fun addUser(id: String, name: String?, role: UserRole, departmentId: Int): User {
if (!call.user().isAdmin()) {
throw AuthorizationException("only admin can add user")
}

return User.createNewUnActiveUser(id, null, role)
return User.createNewUnActiveUser(id, name, role, departmentId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class CourseService(val call: ApplicationCall) : IService {
// make sure students exist
studentIdList.forEachAsync { studentId ->
if (!mysql.users.exists { it.id.inList(studentId.lowerUpperNormal()) }) {
User.createNewUnActiveUser(studentId, null, UserRole.STUDENT)
User.createNewUnActiveUser(studentId, null, UserRole.STUDENT, 0)
}
}

Expand Down
47 changes: 40 additions & 7 deletions cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/service/Term.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package cn.edu.buaa.scs.service

import cn.edu.buaa.scs.controller.models.TermModel
import cn.edu.buaa.scs.error.BusinessException
import cn.edu.buaa.scs.model.Courses
import cn.edu.buaa.scs.model.Experiments
import cn.edu.buaa.scs.model.Term
import cn.edu.buaa.scs.model.terms
import cn.edu.buaa.scs.storage.mysql
import io.ktor.server.application.*
import org.ktorm.dsl.eq
import org.ktorm.entity.find
import org.ktorm.entity.first
import org.ktorm.entity.sortedByDescending
import org.ktorm.entity.toList
import org.ktorm.database.asIterable
import org.ktorm.dsl.*
import org.ktorm.entity.*

fun Term.Companion.id(id: Int): Term =
mysql.terms.find { it.id eq id } ?: throw BusinessException("find term($id) from mysql error")
Expand All @@ -20,11 +21,43 @@ val ApplicationCall.term
class TermService(val call: ApplicationCall) : IService {
companion object : IService.Caller<TermService>()

fun getAllTerms(): List<Term> {
return mysql.terms.toList().sortedByDescending { it.id }
fun getAllTerms(): List<TermModel> {
val terms = mysql.useConnection { conn ->
val sql = """
select term.id, term.name, count(1), sum(c.exp_cnt)
from term left join (
select c.id, c.term_id, count(1) exp_cnt
from course c left join experiment e on c.id = e.course_id
group by c.id
) c on c.term_id=term.id
group by term.id
order by term.id desc
""".trimIndent()
conn.prepareStatement(sql).use { statement ->
statement.executeQuery().asIterable().map { TermModel(
id = it.getInt(1),
name = it.getString(2),
courseCount = it.getInt(3),
expCount = it.getInt(4),
) }
}
}
return terms
}

fun getLatestTerm(): Term {
return mysql.terms.sortedByDescending { it.id }.first()
}

fun createTerm(name: String) {
val term = Term {
this.name = name
}
mysql.terms.add(term)
}

fun deleteTerm(id: Int) {
val term = mysql.terms.find { it.id.eq(id) } ?: return
term.delete()
}
}
Loading

0 comments on commit cfa61a7

Please sign in to comment.