修改mongo查询集合名称由注解提供; 修改command app中throwExec为safeExec

This commit is contained in:
2020-05-26 00:46:16 +08:00
parent 6524ae97b5
commit 0bf9843a1f
3 changed files with 17 additions and 23 deletions

View File

@@ -22,7 +22,7 @@ interface ICommandApp<TCommand : ICommand, TKey> : IApplication {
@PostMapping
fun add(@RequestBody command: TCommand): HttpMessage {
return this.throwExecute("添加${this.name}数据失败 - ${if (jsonSerializer != null) jsonSerializer?.serialize(command) else ""}") {
return this.safeExecute("添加${this.name}数据失败 - ${if (jsonSerializer != null) jsonSerializer?.serialize(command) else ""}") {
if (this.service != null) {
val msg = this.service!!.add(command)
it.load(msg)
@@ -35,7 +35,7 @@ interface ICommandApp<TCommand : ICommand, TKey> : IApplication {
@DeleteMapping("/{key:.+}")
fun remove(@PathVariable key: TKey): HttpMessage {
return this.throwExecute("删除${this.name}失败[Key: $key]") {
return this.safeExecute("删除${this.name}失败[Key: $key]") {
if (this.service != null)
it.data = this.service!!.remove(key)
else {
@@ -47,7 +47,7 @@ interface ICommandApp<TCommand : ICommand, TKey> : IApplication {
@PutMapping("/{key:.+}")
fun update(@PathVariable key: TKey, @RequestBody command: TCommand): HttpMessage {
return this.throwExecute("更新${this.name}失败 - ${if (jsonSerializer != null) jsonSerializer?.serialize(command) else ""}") {
return this.safeExecute("更新${this.name}失败 - ${if (jsonSerializer != null) jsonSerializer?.serialize(command) else ""}") {
if (this.service != null)
this.service!!.update(key, command)
else {

View File

@@ -0,0 +1,3 @@
package com.synebula.gaea.mongo
annotation class Collection(val name: String = "")

View File

@@ -3,16 +3,13 @@ package com.synebula.gaea.mongo.query
import com.synebula.gaea.extension.fieldNames
import com.synebula.gaea.extension.firstCharLowerCase
import com.synebula.gaea.log.ILogger
import com.synebula.gaea.mongo.order
import com.synebula.gaea.mongo.select
import com.synebula.gaea.mongo.where
import com.synebula.gaea.mongo.whereId
import com.synebula.gaea.mongo.*
import com.synebula.gaea.mongo.Collection
import com.synebula.gaea.query.IQuery
import com.synebula.gaea.query.PagingData
import com.synebula.gaea.query.PagingParam
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.query.Query
import java.lang.RuntimeException
/**
* 实现IQuery的Mongo查询类
@@ -21,14 +18,6 @@ import java.lang.RuntimeException
open class MongoQuery(var repo: MongoTemplate, var logger: ILogger? = null) : IQuery {
/**
* 查询的集合名称
*
* 若没有为成员变量collection赋值会尝试使用View名称解析集合名称。
* 规则为移除View后缀并首字母小写此时使用
*/
var collection: String = ""
/**
* 使用View解析是collection时是否校验存在默认不校验
*/
@@ -73,15 +62,17 @@ open class MongoQuery(var repo: MongoTemplate, var logger: ILogger? = null) : IQ
* 获取collection
*/
protected fun <TView> collection(clazz: Class<TView>): String {
return if (this.collection.isEmpty()) {
this.logger?.info(this, "查询集合参数[collection]值为空, 尝试使用View<${clazz.name}>名称解析集合")
val collection = clazz.simpleName.removeSuffix("View").firstCharLowerCase()
if (!validViewCollection || this.repo.collectionExists(collection))
collection
val collection: Collection? = clazz.getDeclaredAnnotation(Collection::class.java)
return if (collection != null)
return collection.name
else {
this.logger?.info(this, "视图类没有标记[Collection]注解无法获取Collection名称。尝试使用View<${clazz.name}>名称解析集合")
val name = clazz.simpleName.removeSuffix("View").firstCharLowerCase()
if (!validViewCollection || this.repo.collectionExists(name))
name
else {
throw RuntimeException("找不到名为[$collection]的集合")
}
} else
this.collection
}
}
}