1.增加删除前监听事件 2.修改关键字key为id

This commit is contained in:
2020-10-27 16:46:32 +08:00
parent 21434d8cd3
commit d1c843cbae
10 changed files with 110 additions and 39 deletions

View File

@@ -33,11 +33,11 @@ interface ICommandApp<TCommand : ICommand, TKey> : IApplication {
}
}
@DeleteMapping("/{key:.+}")
fun remove(@PathVariable key: TKey): HttpMessage {
return this.safeExecute("删除${this.name}失败[Key: $key]") {
@DeleteMapping("/{id:.+}")
fun remove(@PathVariable id: TKey): HttpMessage {
return this.safeExecute("删除${this.name}失败[id: $id]") {
if (this.service != null)
it.data = this.service!!.remove(key)
it.data = this.service!!.remove(id)
else {
it.status = Status.Error
it.message = "没有对应服务,无法执行该操作"
@@ -45,11 +45,11 @@ interface ICommandApp<TCommand : ICommand, TKey> : IApplication {
}
}
@PutMapping("/{key:.+}")
fun update(@PathVariable key: TKey, @RequestBody command: TCommand): HttpMessage {
@PutMapping("/{id:.+}")
fun update(@PathVariable id: TKey, @RequestBody command: TCommand): HttpMessage {
return this.safeExecute("更新${this.name}失败 - ${if (jsonSerializer != null) jsonSerializer?.serialize(command) else ""}") {
if (this.service != null)
this.service!!.update(key, command)
this.service!!.update(id, command)
else {
it.status = Status.Error
it.message = "没有对应服务,无法执行该操作"

View File

@@ -20,10 +20,10 @@ interface IQueryApp<TView, TKey> : IApplication {
*/
var clazz: Class<TView>
@GetMapping("/{key:.+}")
fun get(@PathVariable key: TKey): HttpMessage {
@GetMapping("/{id:.+}")
fun get(@PathVariable id: TKey): HttpMessage {
return this.doQuery("获取${this.name}数据失败") {
this.query!!.get(key, clazz)
this.query!!.get(id, clazz)
}
}

View File

@@ -23,10 +23,10 @@ interface ISpecificQueryApp<TView, TKey> : IApplication {
*/
var query: ISpecificQuery<TView, TKey>?
@GetMapping("/{key:.+}")
fun get(@PathVariable key: TKey): HttpMessage {
@GetMapping("/{id:.+}")
fun get(@PathVariable id: TKey): HttpMessage {
return this.doQuery("获取${this.name}数据失败") {
this.query!!.get(key)
this.query!!.get(id)
}
}

View File

@@ -57,8 +57,8 @@ open class MongoQuery(var template: MongoTemplate, var logger: ILogger? = null)
return result
}
override fun <TView, TKey> get(key: TKey, clazz: Class<TView>): TView? {
return this.template.findOne(whereId(key), clazz, this.collection(clazz))
override fun <TView, TKey> get(id: TKey, clazz: Class<TView>): TView? {
return this.template.findOne(whereId(id), clazz, this.collection(clazz))
}
/**

View File

@@ -110,10 +110,10 @@ open class MongoSpecificQuery<TView>(
} else Page(1, 10)
}
override fun get(key: String): TView? {
override fun get(id: String): TView? {
this.check()
return if (this.clazz != null) {
val view = this.template.findOne(whereId(key), this.clazz!!, this.collection)
val view = this.template.findOne(whereId(id), this.clazz!!, this.collection)
view
} else null
}

View File

@@ -19,7 +19,20 @@ interface IService<TKey> {
fun add(command: ICommand): Message<TKey>
fun update(key: TKey, command: ICommand)
fun update(id: TKey, command: ICommand)
fun remove(key: TKey)
fun remove(id: TKey)
/**
* 添加一个删除对象前执行监听器。
* @param key 监听器标志。
* @param func 监听方法。
*/
fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Boolean)
/**
* 移除一个删除对象前执行监听器。
* @param key 监听器标志。
*/
fun removeBeforeRemoveListener(key: String)
}

View File

@@ -20,12 +20,34 @@ import com.synebula.gaea.log.ILogger
* @since 2020-05-17
*/
open class Service<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
protected open var clazz: Class<TAggregateRoot>,
protected open var repository: IRepository,
protected open var converter: IObjectConverter,
override var logger: ILogger
protected open var clazz: Class<TAggregateRoot>,
protected open var repository: IRepository,
protected open var converter: IObjectConverter,
override var logger: ILogger
) : IService<TKey> {
/**
* 删除对象前执行监听器。
*/
protected val beforeRemoveListeners = mutableMapOf<String, (id: TKey) -> Boolean>()
/**
* 添加一个删除对象前执行监听器。
* @param key 监听器标志。
* @param func 监听方法。
*/
override fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Boolean) {
this.beforeRemoveListeners[key] = func
}
/**
* 移除一个删除对象前执行监听器。
* @param key 监听器标志。
*/
override fun removeBeforeRemoveListener(key: String) {
this.beforeRemoveListeners.remove(key)
}
override fun add(command: ICommand): Message<TKey> {
val msg = Message<TKey>()
val root = this.convert(command)
@@ -34,14 +56,21 @@ open class Service<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
return msg
}
override fun update(key: TKey, command: ICommand) {
override fun update(id: TKey, command: ICommand) {
val root = this.convert(command)
root.id = key
root.id = id
this.repository.update(root, this.clazz)
}
override fun remove(key: TKey) {
this.repository.remove(key, this.clazz)
override fun remove(id: TKey) {
var exec = true
val functions = this.beforeRemoveListeners.values
for (func in functions) {
exec = func(id)
if (!exec) break
}
if (exec)
this.repository.remove(id, this.clazz)
}
/**

View File

@@ -20,16 +20,38 @@ import com.synebula.gaea.log.ILogger
* @since 2020-05-15
*/
open class SpecificService<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
protected var clazz: Class<TAggregateRoot>,
protected var repository: ISpecificRepository<TAggregateRoot, TKey>,
protected var converter: IObjectConverter,
override var logger: ILogger
protected var clazz: Class<TAggregateRoot>,
protected var repository: ISpecificRepository<TAggregateRoot, TKey>,
protected var converter: IObjectConverter,
override var logger: ILogger
) : IService<TKey> {
init {
this.repository.clazz = clazz
}
/**
* 删除对象前执行监听器。
*/
protected val beforeRemoveListeners = mutableMapOf<String, (id: TKey) -> Boolean>()
/**
* 添加一个删除对象前执行监听器。
* @param key 监听器标志。
* @param func 监听方法。
*/
override fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Boolean) {
this.beforeRemoveListeners[key] = func
}
/**
* 移除一个删除对象前执行监听器。
* @param key 监听器标志。
*/
override fun removeBeforeRemoveListener(key: String) {
this.beforeRemoveListeners.remove(key)
}
override fun add(command: ICommand): Message<TKey> {
val msg = Message<TKey>()
val root = this.convert(command)
@@ -38,18 +60,25 @@ open class SpecificService<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
return msg
}
override fun update(key: TKey, command: ICommand) {
override fun update(id: TKey, command: ICommand) {
val root = this.convert(command)
root.id = key
root.id = id
this.repository.update(root)
}
override fun remove(key: TKey) {
this.repository.remove(key)
override fun remove(id: TKey) {
var exec = true
val functions = this.beforeRemoveListeners.values
for (func in functions) {
exec = func(id)
if (!exec) break
}
if (exec)
this.repository.remove(id)
}
fun get(key: TKey): TAggregateRoot {
return this.repository.get(key)
fun get(id: TKey): TAggregateRoot {
return this.repository.get(id)
}
/**

View File

@@ -12,7 +12,7 @@ interface IQuery {
* @param key 对象Key。
* @return
*/
fun <TView, TKey> get(key: TKey, clazz: Class<TView>): TView?
fun <TView, TKey> get(id: TKey, clazz: Class<TView>): TView?
/**
* 根据实体类条件查询所有符合条件记录

View File

@@ -14,7 +14,7 @@ interface ISpecificQuery<TView, TKey> {
* @param key 对象Key。
* @return
*/
fun get(key: TKey): TView?
fun get(id: TKey): TView?
/**
* 根据实体类条件查询所有符合条件记录