repo增加count方法
修改before remove的监听器返回值类型
This commit is contained in:
@@ -27,7 +27,7 @@ interface IApplication {
|
|||||||
logger?.debug(this, "$name business execute success")
|
logger?.debug(this, "$name business execute success")
|
||||||
} catch (ex: Exception) {
|
} catch (ex: Exception) {
|
||||||
msg.status = Status.Error
|
msg.status = Status.Error
|
||||||
msg.message = if (error.isEmpty()) ex.message ?: "" else error
|
msg.message = if (error.isEmpty()) ex.message ?: "" else "$error: ${ex.message}"
|
||||||
logger?.error(this, ex, "$error: ${ex.message}")
|
logger?.error(this, ex, "$error: ${ex.message}")
|
||||||
}
|
}
|
||||||
return msg
|
return msg
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ interface ICommandApp<TCommand : ICommand, TKey> : IApplication {
|
|||||||
|
|
||||||
@DeleteMapping("/{id:.+}")
|
@DeleteMapping("/{id:.+}")
|
||||||
fun remove(@PathVariable id: TKey): HttpMessage {
|
fun remove(@PathVariable id: TKey): HttpMessage {
|
||||||
return this.safeExecute("删除${this.name}失败[id: $id]") {
|
return this.safeExecute("删除${this.name}[id: $id]失败") {
|
||||||
if (this.service != null)
|
if (this.service != null)
|
||||||
it.data = this.service!!.remove(id)
|
it.data = this.service!!.remove(id)
|
||||||
else {
|
else {
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ package com.synebula.gaea.mongo.repository
|
|||||||
|
|
||||||
import com.synebula.gaea.domain.model.IAggregateRoot
|
import com.synebula.gaea.domain.model.IAggregateRoot
|
||||||
import com.synebula.gaea.domain.repository.IRepository
|
import com.synebula.gaea.domain.repository.IRepository
|
||||||
|
import com.synebula.gaea.mongo.where
|
||||||
import com.synebula.gaea.mongo.whereId
|
import com.synebula.gaea.mongo.whereId
|
||||||
import org.springframework.data.mongodb.core.MongoTemplate
|
import org.springframework.data.mongodb.core.MongoTemplate
|
||||||
|
import org.springframework.data.mongodb.core.query.Query
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实现ITypedRepository的mongo仓储类
|
* 实现ITypedRepository的mongo仓储类
|
||||||
@@ -16,15 +18,15 @@ open class MongoRepository(private var repo: MongoTemplate) : IRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> get(
|
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> get(
|
||||||
id: TKey,
|
id: TKey,
|
||||||
clazz: Class<TAggregateRoot>
|
clazz: Class<TAggregateRoot>
|
||||||
): TAggregateRoot {
|
): TAggregateRoot {
|
||||||
return this.repo.findOne(whereId(id), clazz) as TAggregateRoot
|
return this.repo.findOne(whereId(id), clazz) as TAggregateRoot
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> update(
|
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> update(
|
||||||
obj: TAggregateRoot,
|
obj: TAggregateRoot,
|
||||||
clazz: Class<TAggregateRoot>
|
clazz: Class<TAggregateRoot>
|
||||||
) {
|
) {
|
||||||
this.repo.save(obj)
|
this.repo.save(obj)
|
||||||
}
|
}
|
||||||
@@ -32,4 +34,10 @@ open class MongoRepository(private var repo: MongoTemplate) : IRepository {
|
|||||||
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> add(obj: TAggregateRoot, clazz: Class<TAggregateRoot>) {
|
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> add(obj: TAggregateRoot, clazz: Class<TAggregateRoot>) {
|
||||||
this.repo.save(obj)
|
this.repo.save(obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
override fun <TAggregateRoot> count(params: Map<String, Any>?, clazz: Class<TAggregateRoot>): Int {
|
||||||
|
val query = Query()
|
||||||
|
return this.repo.count(query.where(params, clazz), clazz).toInt()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,15 +2,17 @@ package com.synebula.gaea.mongo.repository
|
|||||||
|
|
||||||
import com.synebula.gaea.domain.model.IAggregateRoot
|
import com.synebula.gaea.domain.model.IAggregateRoot
|
||||||
import com.synebula.gaea.domain.repository.ISpecificRepository
|
import com.synebula.gaea.domain.repository.ISpecificRepository
|
||||||
|
import com.synebula.gaea.mongo.where
|
||||||
import com.synebula.gaea.mongo.whereId
|
import com.synebula.gaea.mongo.whereId
|
||||||
import org.springframework.data.mongodb.core.MongoTemplate
|
import org.springframework.data.mongodb.core.MongoTemplate
|
||||||
|
import org.springframework.data.mongodb.core.query.Query
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实现IAggregateRoot的mongo仓储类
|
* 实现IAggregateRoot的mongo仓储类
|
||||||
* @param repo MongoRepo对象
|
* @param repo MongoRepo对象
|
||||||
*/
|
*/
|
||||||
class MongoSpecificRepository<TAggregateRoot : IAggregateRoot<String>>(private var repo: MongoTemplate) :
|
class MongoSpecificRepository<TAggregateRoot : IAggregateRoot<String>>(private var repo: MongoTemplate) :
|
||||||
ISpecificRepository<TAggregateRoot, String> {
|
ISpecificRepository<TAggregateRoot, String> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 仓储的对象类
|
* 仓储的对象类
|
||||||
@@ -43,9 +45,15 @@ class MongoSpecificRepository<TAggregateRoot : IAggregateRoot<String>>(private v
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> get(
|
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> get(
|
||||||
id: TKey,
|
id: TKey,
|
||||||
clazz: Class<TAggregateRoot>
|
clazz: Class<TAggregateRoot>
|
||||||
): TAggregateRoot {
|
): TAggregateRoot {
|
||||||
return this.repo.findOne(whereId(id.toString()), clazz) as TAggregateRoot
|
return this.repo.findOne(whereId(id.toString()), clazz) as TAggregateRoot
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
override fun <TAggregateRoot> count(params: Map<String, Any>?): Int {
|
||||||
|
val query = Query()
|
||||||
|
return this.repo.count(query.where(params, this.clazz!!), this.clazz!!).toInt()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,15 +4,15 @@ object Status {
|
|||||||
/**
|
/**
|
||||||
* 成功
|
* 成功
|
||||||
*/
|
*/
|
||||||
val Success = 200
|
const val Success = 200
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 失败
|
* 失败
|
||||||
*/
|
*/
|
||||||
val Failure = 400
|
const val Failure = 400
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 错误
|
* 错误
|
||||||
*/
|
*/
|
||||||
val Error = 500
|
const val Error = 500
|
||||||
}
|
}
|
||||||
@@ -39,4 +39,13 @@ interface IRepository {
|
|||||||
* @return 聚合根
|
* @return 聚合根
|
||||||
*/
|
*/
|
||||||
fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> get(id: TKey, clazz: Class<TAggregateRoot>): TAggregateRoot
|
fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> get(id: TKey, clazz: Class<TAggregateRoot>): TAggregateRoot
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据条件查询符合条件记录的数量
|
||||||
|
*
|
||||||
|
* @param params 查询条件。
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
fun <TAggregateRoot> count(params: Map<String, Any>?, clazz: Class<TAggregateRoot>): Int
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,4 +58,13 @@ interface ISpecificRepository<TAggregateRoot : IAggregateRoot<TKey>, TKey> {
|
|||||||
*/
|
*/
|
||||||
fun <T : IAggregateRoot<TKey>, TKey> get(id: TKey, clazz: Class<T>): T
|
fun <T : IAggregateRoot<TKey>, TKey> get(id: TKey, clazz: Class<T>): T
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据条件查询符合条件记录的数量
|
||||||
|
*
|
||||||
|
* @param params 查询条件。
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
fun <TAggregateRoot> count(params: Map<String, Any>?): Int
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ interface IService<TKey> {
|
|||||||
* @param key 监听器标志。
|
* @param key 监听器标志。
|
||||||
* @param func 监听方法。
|
* @param func 监听方法。
|
||||||
*/
|
*/
|
||||||
fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Boolean)
|
fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Message<String>)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 移除一个删除对象前执行监听器。
|
* 移除一个删除对象前执行监听器。
|
||||||
|
|||||||
@@ -29,14 +29,14 @@ open class Service<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
|
|||||||
/**
|
/**
|
||||||
* 删除对象前执行监听器。
|
* 删除对象前执行监听器。
|
||||||
*/
|
*/
|
||||||
protected val beforeRemoveListeners = mutableMapOf<String, (id: TKey) -> Boolean>()
|
protected val beforeRemoveListeners = mutableMapOf<String, (id: TKey) -> Message<String>>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加一个删除对象前执行监听器。
|
* 添加一个删除对象前执行监听器。
|
||||||
* @param key 监听器标志。
|
* @param key 监听器标志。
|
||||||
* @param func 监听方法。
|
* @param func 监听方法。
|
||||||
*/
|
*/
|
||||||
override fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Boolean) {
|
override fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Message<String>) {
|
||||||
this.beforeRemoveListeners[key] = func
|
this.beforeRemoveListeners[key] = func
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,14 +63,15 @@ open class Service<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun remove(id: TKey) {
|
override fun remove(id: TKey) {
|
||||||
var exec = true
|
|
||||||
val functions = this.beforeRemoveListeners.values
|
val functions = this.beforeRemoveListeners.values
|
||||||
|
var msg: Message<String>
|
||||||
for (func in functions) {
|
for (func in functions) {
|
||||||
exec = func(id)
|
msg = func(id)
|
||||||
if (!exec) break
|
if (!msg.success) {
|
||||||
|
throw java.lang.RuntimeException(msg.data)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (exec)
|
this.repository.remove(id, this.clazz)
|
||||||
this.repository.remove(id, this.clazz)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -33,14 +33,14 @@ open class SpecificService<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
|
|||||||
/**
|
/**
|
||||||
* 删除对象前执行监听器。
|
* 删除对象前执行监听器。
|
||||||
*/
|
*/
|
||||||
protected val beforeRemoveListeners = mutableMapOf<String, (id: TKey) -> Boolean>()
|
protected val beforeRemoveListeners = mutableMapOf<String, (id: TKey) -> Message<String>>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加一个删除对象前执行监听器。
|
* 添加一个删除对象前执行监听器。
|
||||||
* @param key 监听器标志。
|
* @param key 监听器标志。
|
||||||
* @param func 监听方法。
|
* @param func 监听方法。
|
||||||
*/
|
*/
|
||||||
override fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Boolean) {
|
override fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Message<String>) {
|
||||||
this.beforeRemoveListeners[key] = func
|
this.beforeRemoveListeners[key] = func
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,14 +67,15 @@ open class SpecificService<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun remove(id: TKey) {
|
override fun remove(id: TKey) {
|
||||||
var exec = true
|
|
||||||
val functions = this.beforeRemoveListeners.values
|
val functions = this.beforeRemoveListeners.values
|
||||||
|
var msg: Message<String>
|
||||||
for (func in functions) {
|
for (func in functions) {
|
||||||
exec = func(id)
|
msg = func(id)
|
||||||
if (!exec) break
|
if (!msg.success) {
|
||||||
|
throw java.lang.RuntimeException(msg.data)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (exec)
|
this.repository.remove(id)
|
||||||
this.repository.remove(id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun get(id: TKey): TAggregateRoot {
|
fun get(id: TKey): TAggregateRoot {
|
||||||
|
|||||||
Reference in New Issue
Block a user