From d1c843cbaede5f540481ee7675cecbd649bb781a Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 27 Oct 2020 16:46:32 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=A2=9E=E5=8A=A0=E5=88=A0=E9=99=A4=E5=89=8D?= =?UTF-8?q?=E7=9B=91=E5=90=AC=E4=BA=8B=E4=BB=B6=202.=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=85=B3=E9=94=AE=E5=AD=97key=E4=B8=BAid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/synebula/gaea/app/cmd/ICommandApp.kt | 14 +++--- .../com/synebula/gaea/app/query/IQueryApp.kt | 6 +-- .../gaea/app/query/ISpecificQueryApp.kt | 6 +-- .../synebula/gaea/mongo/query/MongoQuery.kt | 4 +- .../gaea/mongo/query/MongoSpecificQuery.kt | 4 +- .../synebula/gaea/domain/service/IService.kt | 17 ++++++- .../synebula/gaea/domain/service/Service.kt | 45 ++++++++++++++--- .../gaea/domain/service/SpecificService.kt | 49 +++++++++++++++---- .../kotlin/com/synebula/gaea/query/IQuery.kt | 2 +- .../com/synebula/gaea/query/ISpecificQuery.kt | 2 +- 10 files changed, 110 insertions(+), 39 deletions(-) diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/ICommandApp.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/ICommandApp.kt index 277ef8f..46236da 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/ICommandApp.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/ICommandApp.kt @@ -33,11 +33,11 @@ interface ICommandApp : 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 : 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 = "没有对应服务,无法执行该操作" diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/IQueryApp.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/IQueryApp.kt index d11f0e8..0d7d640 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/IQueryApp.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/IQueryApp.kt @@ -20,10 +20,10 @@ interface IQueryApp : IApplication { */ var clazz: Class - @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) } } diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/ISpecificQueryApp.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/ISpecificQueryApp.kt index e3eb68c..a0c6d30 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/ISpecificQueryApp.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/ISpecificQueryApp.kt @@ -23,10 +23,10 @@ interface ISpecificQueryApp : IApplication { */ var query: ISpecificQuery? - @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) } } diff --git a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/query/MongoQuery.kt b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/query/MongoQuery.kt index a98e1e1..b848053 100644 --- a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/query/MongoQuery.kt +++ b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/query/MongoQuery.kt @@ -57,8 +57,8 @@ open class MongoQuery(var template: MongoTemplate, var logger: ILogger? = null) return result } - override fun get(key: TKey, clazz: Class): TView? { - return this.template.findOne(whereId(key), clazz, this.collection(clazz)) + override fun get(id: TKey, clazz: Class): TView? { + return this.template.findOne(whereId(id), clazz, this.collection(clazz)) } /** diff --git a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/query/MongoSpecificQuery.kt b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/query/MongoSpecificQuery.kt index b888f37..7e4524c 100644 --- a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/query/MongoSpecificQuery.kt +++ b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/query/MongoSpecificQuery.kt @@ -110,10 +110,10 @@ open class MongoSpecificQuery( } 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 } diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/IService.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/IService.kt index cc8fc3a..80b5706 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/IService.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/IService.kt @@ -19,7 +19,20 @@ interface IService { fun add(command: ICommand): Message - 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) } diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/Service.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/Service.kt index a1c978a..d78d6f9 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/Service.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/Service.kt @@ -20,12 +20,34 @@ import com.synebula.gaea.log.ILogger * @since 2020-05-17 */ open class Service, TKey>( - protected open var clazz: Class, - protected open var repository: IRepository, - protected open var converter: IObjectConverter, - override var logger: ILogger + protected open var clazz: Class, + protected open var repository: IRepository, + protected open var converter: IObjectConverter, + override var logger: ILogger ) : IService { + /** + * 删除对象前执行监听器。 + */ + protected val beforeRemoveListeners = mutableMapOf 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 { val msg = Message() val root = this.convert(command) @@ -34,14 +56,21 @@ open class Service, 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) } /** diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/SpecificService.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/SpecificService.kt index c490df4..70659a5 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/SpecificService.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/SpecificService.kt @@ -20,16 +20,38 @@ import com.synebula.gaea.log.ILogger * @since 2020-05-15 */ open class SpecificService, TKey>( - protected var clazz: Class, - protected var repository: ISpecificRepository, - protected var converter: IObjectConverter, - override var logger: ILogger + protected var clazz: Class, + protected var repository: ISpecificRepository, + protected var converter: IObjectConverter, + override var logger: ILogger ) : IService { init { this.repository.clazz = clazz } + /** + * 删除对象前执行监听器。 + */ + protected val beforeRemoveListeners = mutableMapOf 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 { val msg = Message() val root = this.convert(command) @@ -38,18 +60,25 @@ open class SpecificService, 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) } /** diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/query/IQuery.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/query/IQuery.kt index 0e6a26e..de72e5f 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/query/IQuery.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/query/IQuery.kt @@ -12,7 +12,7 @@ interface IQuery { * @param key 对象Key。 * @return */ - fun get(key: TKey, clazz: Class): TView? + fun get(id: TKey, clazz: Class): TView? /** * 根据实体类条件查询所有符合条件记录 diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/query/ISpecificQuery.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/query/ISpecificQuery.kt index 5145aa0..4b075d6 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/query/ISpecificQuery.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/query/ISpecificQuery.kt @@ -14,7 +14,7 @@ interface ISpecificQuery { * @param key 对象Key。 * @return */ - fun get(key: TKey): TView? + fun get(id: TKey): TView? /** * 根据实体类条件查询所有符合条件记录