實作素材查詢、修改、刪除
This commit is contained in:
parent
07597ad5f2
commit
02849a7396
@ -6,6 +6,7 @@ import com.ray650128.model.dto.MaterialDto
|
|||||||
import com.ray650128.model.dto.UserDto
|
import com.ray650128.model.dto.UserDto
|
||||||
import com.ray650128.model.pojo.Material
|
import com.ray650128.model.pojo.Material
|
||||||
import com.ray650128.model.pojo.NewMaterial
|
import com.ray650128.model.pojo.NewMaterial
|
||||||
|
import com.ray650128.model.pojo.User
|
||||||
import com.ray650128.service.MaterialService
|
import com.ray650128.service.MaterialService
|
||||||
import com.ray650128.service.UserService
|
import com.ray650128.service.UserService
|
||||||
import io.ktor.http.*
|
import io.ktor.http.*
|
||||||
@ -28,14 +29,14 @@ fun Application.configureArMaterialRouting() {
|
|||||||
route("/upload") {
|
route("/upload") {
|
||||||
get("/{name}") {
|
get("/{name}") {
|
||||||
val fileName = call.parameters["name"] ?: run {
|
val fileName = call.parameters["name"] ?: run {
|
||||||
call.sendNotFound(ErrorResponse.NOT_FOUND_RESPONSE)
|
call.sendNotFound()
|
||||||
return@get
|
return@get
|
||||||
}
|
}
|
||||||
println("[File]filename: $fileName")
|
println("[File]filename: $fileName")
|
||||||
val file = File("./upload/$fileName")
|
val file = File("./upload/$fileName")
|
||||||
println("[File]file: ${file.absolutePath}")
|
println("[File]file: ${file.absolutePath}")
|
||||||
if(!file.exists()) {
|
if(!file.exists()) {
|
||||||
call.sendNotFound(ErrorResponse.NOT_FOUND_RESPONSE)
|
call.sendNotFound()
|
||||||
return@get
|
return@get
|
||||||
}
|
}
|
||||||
call.respondFile(file)
|
call.respondFile(file)
|
||||||
@ -46,13 +47,26 @@ fun Application.configureArMaterialRouting() {
|
|||||||
route("/api") {
|
route("/api") {
|
||||||
route("/v1") {
|
route("/v1") {
|
||||||
route("/materials") {
|
route("/materials") {
|
||||||
|
get {
|
||||||
|
val account = call.authentication.principal<UserDto>()?.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") {
|
post("/upload") {
|
||||||
val account = call.authentication.principal<UserDto>()?.account ?: run {
|
val account = call.authentication.principal<UserDto>()?.account ?: run {
|
||||||
call.sendUnauthorized(ErrorResponse.UNAUTHORIZED_RESPONSE)
|
call.sendUnauthorized()
|
||||||
return@post
|
return@post
|
||||||
}
|
}
|
||||||
val user = userService.findByAccount(account) ?: run {
|
val user = userService.findByAccount(account) ?: run {
|
||||||
call.sendUnauthorized(ErrorResponse.UNAUTHORIZED_RESPONSE)
|
call.sendUnauthorized()
|
||||||
return@post
|
return@post
|
||||||
}
|
}
|
||||||
var materialId: String? = null
|
var materialId: String? = null
|
||||||
@ -118,6 +132,46 @@ fun Application.configureArMaterialRouting() {
|
|||||||
call.sendBadRequest(ErrorResponse("Add material fail."))
|
call.sendBadRequest(ErrorResponse("Add material fail."))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
put("/{id}") {
|
||||||
|
call.authentication.principal<UserDto>()?.account ?: run {
|
||||||
|
call.sendUnauthorized()
|
||||||
|
return@put
|
||||||
|
}
|
||||||
|
val body = call.receive<MaterialDto>()
|
||||||
|
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<UserDto>()?.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))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package com.ray650128.service
|
package com.ray650128.service
|
||||||
|
|
||||||
import com.ray650128.model.pojo.Material
|
import com.ray650128.model.pojo.Material
|
||||||
|
import com.ray650128.model.pojo.User
|
||||||
import org.bson.types.ObjectId
|
import org.bson.types.ObjectId
|
||||||
import org.litote.kmongo.*
|
import org.litote.kmongo.*
|
||||||
import org.litote.kmongo.id.toId
|
import org.litote.kmongo.id.toId
|
||||||
@ -22,6 +23,11 @@ class MaterialService {
|
|||||||
return userCollection.findOne(Material::id eq bsonId)
|
return userCollection.findOne(Material::id eq bsonId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun findByOwnerId(ownerId: String): List<Material> {
|
||||||
|
val bsonId: Id<User> = ObjectId(ownerId).toId()
|
||||||
|
return userCollection.find(Material::ownerId eq bsonId).toList()
|
||||||
|
}
|
||||||
|
|
||||||
fun findByName(name: String): List<Material> {
|
fun findByName(name: String): List<Material> {
|
||||||
val caseSensitiveTypeSafeFilter = Material::name regex name
|
val caseSensitiveTypeSafeFilter = Material::name regex name
|
||||||
return userCollection.find(caseSensitiveTypeSafeFilter).toList()
|
return userCollection.find(caseSensitiveTypeSafeFilter).toList()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user