From ca13018c9fb3d2939ead4a0cb03435420655f0d9 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 27 Oct 2020 23:37:18 +0800 Subject: [PATCH] =?UTF-8?q?repo=E5=A2=9E=E5=8A=A0count=E6=96=B9=E6=B3=95?= =?UTF-8?q?=20=E4=BF=AE=E6=94=B9before=20remove=E7=9A=84=E7=9B=91=E5=90=AC?= =?UTF-8?q?=E5=99=A8=E8=BF=94=E5=9B=9E=E5=80=BC=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/synebula/gaea/app/IApplication.kt | 2 +- .../com/synebula/gaea/app/cmd/ICommandApp.kt | 2 +- .../gaea/mongo/repository/MongoRepository.kt | 16 ++++++++++++---- .../mongo/repository/MongoSpecificRepository.kt | 14 +++++++++++--- .../com/synebula/gaea/data/message/Status.kt | 6 +++--- .../gaea/domain/repository/IRepository.kt | 9 +++++++++ .../domain/repository/ISpecificRepository.kt | 9 +++++++++ .../com/synebula/gaea/domain/service/IService.kt | 2 +- .../com/synebula/gaea/domain/service/Service.kt | 15 ++++++++------- .../gaea/domain/service/SpecificService.kt | 15 ++++++++------- 10 files changed, 63 insertions(+), 27 deletions(-) diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/IApplication.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/IApplication.kt index b359834..1d2224e 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/IApplication.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/IApplication.kt @@ -27,7 +27,7 @@ interface IApplication { logger?.debug(this, "$name business execute success") } catch (ex: Exception) { 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}") } return msg 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 46236da..d888115 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 @@ -35,7 +35,7 @@ interface ICommandApp : IApplication { @DeleteMapping("/{id:.+}") fun remove(@PathVariable id: TKey): HttpMessage { - return this.safeExecute("删除${this.name}失败[id: $id]") { + return this.safeExecute("删除${this.name}[id: $id]失败") { if (this.service != null) it.data = this.service!!.remove(id) else { diff --git a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/repository/MongoRepository.kt b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/repository/MongoRepository.kt index d212882..93d035b 100644 --- a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/repository/MongoRepository.kt +++ b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/repository/MongoRepository.kt @@ -2,8 +2,10 @@ package com.synebula.gaea.mongo.repository import com.synebula.gaea.domain.model.IAggregateRoot import com.synebula.gaea.domain.repository.IRepository +import com.synebula.gaea.mongo.where import com.synebula.gaea.mongo.whereId import org.springframework.data.mongodb.core.MongoTemplate +import org.springframework.data.mongodb.core.query.Query /** * 实现ITypedRepository的mongo仓储类 @@ -16,15 +18,15 @@ open class MongoRepository(private var repo: MongoTemplate) : IRepository { } override fun , TKey> get( - id: TKey, - clazz: Class + id: TKey, + clazz: Class ): TAggregateRoot { return this.repo.findOne(whereId(id), clazz) as TAggregateRoot } override fun , TKey> update( - obj: TAggregateRoot, - clazz: Class + obj: TAggregateRoot, + clazz: Class ) { this.repo.save(obj) } @@ -32,4 +34,10 @@ open class MongoRepository(private var repo: MongoTemplate) : IRepository { override fun , TKey> add(obj: TAggregateRoot, clazz: Class) { this.repo.save(obj) } + + + override fun count(params: Map?, clazz: Class): Int { + val query = Query() + return this.repo.count(query.where(params, clazz), clazz).toInt() + } } diff --git a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/repository/MongoSpecificRepository.kt b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/repository/MongoSpecificRepository.kt index e90d55f..5ab616e 100644 --- a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/repository/MongoSpecificRepository.kt +++ b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/repository/MongoSpecificRepository.kt @@ -2,15 +2,17 @@ package com.synebula.gaea.mongo.repository import com.synebula.gaea.domain.model.IAggregateRoot import com.synebula.gaea.domain.repository.ISpecificRepository +import com.synebula.gaea.mongo.where import com.synebula.gaea.mongo.whereId import org.springframework.data.mongodb.core.MongoTemplate +import org.springframework.data.mongodb.core.query.Query /** * 实现IAggregateRoot的mongo仓储类 * @param repo MongoRepo对象 */ class MongoSpecificRepository>(private var repo: MongoTemplate) : - ISpecificRepository { + ISpecificRepository { /** * 仓储的对象类 @@ -43,9 +45,15 @@ class MongoSpecificRepository>(private v } override fun , TKey> get( - id: TKey, - clazz: Class + id: TKey, + clazz: Class ): TAggregateRoot { return this.repo.findOne(whereId(id.toString()), clazz) as TAggregateRoot } + + + override fun count(params: Map?): Int { + val query = Query() + return this.repo.count(query.where(params, this.clazz!!), this.clazz!!).toInt() + } } diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/data/message/Status.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/data/message/Status.kt index c61837d..179549e 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/data/message/Status.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/data/message/Status.kt @@ -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 } \ No newline at end of file diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/IRepository.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/IRepository.kt index ba452e9..2d6e582 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/IRepository.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/IRepository.kt @@ -39,4 +39,13 @@ interface IRepository { * @return 聚合根 */ fun , TKey> get(id: TKey, clazz: Class): TAggregateRoot + + + /** + * 根据条件查询符合条件记录的数量 + * + * @param params 查询条件。 + * @return int + */ + fun count(params: Map?, clazz: Class): Int } diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/ISpecificRepository.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/ISpecificRepository.kt index 9bc9df4..06e8564 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/ISpecificRepository.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/ISpecificRepository.kt @@ -58,4 +58,13 @@ interface ISpecificRepository, TKey> { */ fun , TKey> get(id: TKey, clazz: Class): T + + /** + * 根据条件查询符合条件记录的数量 + * + * @param params 查询条件。 + * @return int + */ + fun count(params: Map?): Int + } 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 80b5706..93449e3 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 @@ -28,7 +28,7 @@ interface IService { * @param key 监听器标志。 * @param func 监听方法。 */ - fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Boolean) + fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Message) /** * 移除一个删除对象前执行监听器。 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 d78d6f9..d65fdad 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 @@ -29,14 +29,14 @@ open class Service, TKey>( /** * 删除对象前执行监听器。 */ - protected val beforeRemoveListeners = mutableMapOf Boolean>() + protected val beforeRemoveListeners = mutableMapOf Message>() /** * 添加一个删除对象前执行监听器。 * @param key 监听器标志。 * @param func 监听方法。 */ - override fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Boolean) { + override fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Message) { this.beforeRemoveListeners[key] = func } @@ -63,14 +63,15 @@ open class Service, TKey>( } override fun remove(id: TKey) { - var exec = true val functions = this.beforeRemoveListeners.values + var msg: Message for (func in functions) { - exec = func(id) - if (!exec) break + msg = func(id) + if (!msg.success) { + throw java.lang.RuntimeException(msg.data) + } } - if (exec) - this.repository.remove(id, this.clazz) + 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 70659a5..9e9c43f 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 @@ -33,14 +33,14 @@ open class SpecificService, TKey>( /** * 删除对象前执行监听器。 */ - protected val beforeRemoveListeners = mutableMapOf Boolean>() + protected val beforeRemoveListeners = mutableMapOf Message>() /** * 添加一个删除对象前执行监听器。 * @param key 监听器标志。 * @param func 监听方法。 */ - override fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Boolean) { + override fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Message) { this.beforeRemoveListeners[key] = func } @@ -67,14 +67,15 @@ open class SpecificService, TKey>( } override fun remove(id: TKey) { - var exec = true val functions = this.beforeRemoveListeners.values + var msg: Message for (func in functions) { - exec = func(id) - if (!exec) break + msg = func(id) + if (!msg.success) { + throw java.lang.RuntimeException(msg.data) + } } - if (exec) - this.repository.remove(id) + this.repository.remove(id) } fun get(id: TKey): TAggregateRoot {