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

View File

@@ -20,10 +20,10 @@ interface IQueryApp<TView, TKey> : IApplication {
*/ */
var clazz: Class<TView> var clazz: Class<TView>
@GetMapping("/{key:.+}") @GetMapping("/{id:.+}")
fun get(@PathVariable key: TKey): HttpMessage { fun get(@PathVariable id: TKey): HttpMessage {
return this.doQuery("获取${this.name}数据失败") { 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>? var query: ISpecificQuery<TView, TKey>?
@GetMapping("/{key:.+}") @GetMapping("/{id:.+}")
fun get(@PathVariable key: TKey): HttpMessage { fun get(@PathVariable id: TKey): HttpMessage {
return this.doQuery("获取${this.name}数据失败") { 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 return result
} }
override fun <TView, TKey> get(key: TKey, clazz: Class<TView>): TView? { override fun <TView, TKey> get(id: TKey, clazz: Class<TView>): TView? {
return this.template.findOne(whereId(key), clazz, this.collection(clazz)) return this.template.findOne(whereId(id), clazz, this.collection(clazz))
} }
/** /**

View File

@@ -110,10 +110,10 @@ open class MongoSpecificQuery<TView>(
} else Page(1, 10) } else Page(1, 10)
} }
override fun get(key: String): TView? { override fun get(id: String): TView? {
this.check() this.check()
return if (this.clazz != null) { 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 view
} else null } else null
} }

View File

@@ -19,7 +19,20 @@ interface IService<TKey> {
fun add(command: ICommand): Message<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 * @since 2020-05-17
*/ */
open class Service<TAggregateRoot : IAggregateRoot<TKey>, TKey>( open class Service<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
protected open var clazz: Class<TAggregateRoot>, protected open var clazz: Class<TAggregateRoot>,
protected open var repository: IRepository, protected open var repository: IRepository,
protected open var converter: IObjectConverter, protected open var converter: IObjectConverter,
override var logger: ILogger override var logger: ILogger
) : IService<TKey> { ) : 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> { override fun add(command: ICommand): Message<TKey> {
val msg = Message<TKey>() val msg = Message<TKey>()
val root = this.convert(command) val root = this.convert(command)
@@ -34,14 +56,21 @@ open class Service<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
return msg return msg
} }
override fun update(key: TKey, command: ICommand) { override fun update(id: TKey, command: ICommand) {
val root = this.convert(command) val root = this.convert(command)
root.id = key root.id = id
this.repository.update(root, this.clazz) this.repository.update(root, this.clazz)
} }
override fun remove(key: TKey) { override fun remove(id: TKey) {
this.repository.remove(key, this.clazz) 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 * @since 2020-05-15
*/ */
open class SpecificService<TAggregateRoot : IAggregateRoot<TKey>, TKey>( open class SpecificService<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
protected var clazz: Class<TAggregateRoot>, protected var clazz: Class<TAggregateRoot>,
protected var repository: ISpecificRepository<TAggregateRoot, TKey>, protected var repository: ISpecificRepository<TAggregateRoot, TKey>,
protected var converter: IObjectConverter, protected var converter: IObjectConverter,
override var logger: ILogger override var logger: ILogger
) : IService<TKey> { ) : IService<TKey> {
init { init {
this.repository.clazz = clazz 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> { override fun add(command: ICommand): Message<TKey> {
val msg = Message<TKey>() val msg = Message<TKey>()
val root = this.convert(command) val root = this.convert(command)
@@ -38,18 +60,25 @@ open class SpecificService<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
return msg return msg
} }
override fun update(key: TKey, command: ICommand) { override fun update(id: TKey, command: ICommand) {
val root = this.convert(command) val root = this.convert(command)
root.id = key root.id = id
this.repository.update(root) this.repository.update(root)
} }
override fun remove(key: TKey) { override fun remove(id: TKey) {
this.repository.remove(key) 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 { fun get(id: TKey): TAggregateRoot {
return this.repository.get(key) return this.repository.get(id)
} }
/** /**

View File

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