diff --git a/src/main/kotlin/com/ray650128/plugins/ArMaterialRouting.kt b/src/main/kotlin/com/ray650128/plugins/ArMaterialRouting.kt index 1e5dad1..0de063b 100644 --- a/src/main/kotlin/com/ray650128/plugins/ArMaterialRouting.kt +++ b/src/main/kotlin/com/ray650128/plugins/ArMaterialRouting.kt @@ -6,6 +6,7 @@ import com.ray650128.model.dto.MaterialDto import com.ray650128.model.dto.UserDto import com.ray650128.model.pojo.Material import com.ray650128.model.pojo.NewMaterial +import com.ray650128.model.pojo.User import com.ray650128.service.MaterialService import com.ray650128.service.UserService import io.ktor.http.* @@ -28,14 +29,14 @@ fun Application.configureArMaterialRouting() { route("/upload") { get("/{name}") { val fileName = call.parameters["name"] ?: run { - call.sendNotFound(ErrorResponse.NOT_FOUND_RESPONSE) + call.sendNotFound() return@get } println("[File]filename: $fileName") val file = File("./upload/$fileName") println("[File]file: ${file.absolutePath}") if(!file.exists()) { - call.sendNotFound(ErrorResponse.NOT_FOUND_RESPONSE) + call.sendNotFound() return@get } call.respondFile(file) @@ -46,13 +47,26 @@ fun Application.configureArMaterialRouting() { route("/api") { route("/v1") { route("/materials") { + get { + val account = call.authentication.principal()?.account ?: run { + call.sendUnauthorized() + return@get + } + val user = userService.findByAccount(account) ?: run { + call.sendUnauthorized() + return@get + } + val list = materialService.findByOwnerId(user.id.toString()) + call.sendSuccess(list.map(Material::toDto)) + } + post("/upload") { val account = call.authentication.principal()?.account ?: run { - call.sendUnauthorized(ErrorResponse.UNAUTHORIZED_RESPONSE) + call.sendUnauthorized() return@post } val user = userService.findByAccount(account) ?: run { - call.sendUnauthorized(ErrorResponse.UNAUTHORIZED_RESPONSE) + call.sendUnauthorized() return@post } var materialId: String? = null @@ -118,6 +132,46 @@ fun Application.configureArMaterialRouting() { call.sendBadRequest(ErrorResponse("Add material fail.")) } } + + put("/{id}") { + call.authentication.principal()?.account ?: run { + call.sendUnauthorized() + return@put + } + val body = call.receive() + val id = call.parameters["id"].toString() + val material = materialService.findById(id) ?: run { + call.sendNotFound() + return@put + } + material.apply { + name = body.name + fileTag = body.fileTag + updatedAt = System.currentTimeMillis() + } + val isSuccess = materialService.updateById(id, material) + call.sendSuccess(mapOf("success" to isSuccess)) + } + + delete("/{id}") { + call.authentication.principal()?.account ?: run { + call.sendUnauthorized() + return@delete + } + val id = call.parameters["id"].toString() + val material = materialService.findById(id) ?: run { + call.sendNotFound() + return@delete + } + val filePath = material.path + val file = File(".$filePath") + println(file.absolutePath) + if (file.exists()) { + file.delete() + } + val isSuccess = materialService.deleteById(id) + call.sendSuccess(mapOf("success" to isSuccess)) + } } } } diff --git a/src/main/kotlin/com/ray650128/service/MaterialService.kt b/src/main/kotlin/com/ray650128/service/MaterialService.kt index 7941a74..6126c3b 100644 --- a/src/main/kotlin/com/ray650128/service/MaterialService.kt +++ b/src/main/kotlin/com/ray650128/service/MaterialService.kt @@ -1,6 +1,7 @@ package com.ray650128.service import com.ray650128.model.pojo.Material +import com.ray650128.model.pojo.User import org.bson.types.ObjectId import org.litote.kmongo.* import org.litote.kmongo.id.toId @@ -22,6 +23,11 @@ class MaterialService { return userCollection.findOne(Material::id eq bsonId) } + fun findByOwnerId(ownerId: String): List { + val bsonId: Id = ObjectId(ownerId).toId() + return userCollection.find(Material::ownerId eq bsonId).toList() + } + fun findByName(name: String): List { val caseSensitiveTypeSafeFilter = Material::name regex name return userCollection.find(caseSensitiveTypeSafeFilter).toList()