diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/UnionApp.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/UnionApp.kt index e646e67..460440f 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/UnionApp.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/UnionApp.kt @@ -1,7 +1,7 @@ package com.synebula.gaea.app import com.synebula.gaea.app.cmd.ICommandApp -import com.synebula.gaea.app.query.IQueryApp +import com.synebula.gaea.app.query.IQueryGenericApp import com.synebula.gaea.data.serialization.json.IJsonSerializer import com.synebula.gaea.domain.service.ICommand import com.synebula.gaea.domain.service.IService @@ -22,7 +22,7 @@ open class UnionApp( override var service: IService?, override var query: IQuery?, override var logger: ILogger) - : ICommandApp, IQueryApp { + : ICommandApp, IQueryGenericApp { @Resource override var jsonSerializer: IJsonSerializer? = null diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/UnionTypedApp.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/UnionTypedApp.kt index fcbae29..bb151f7 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/UnionTypedApp.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/UnionTypedApp.kt @@ -1,11 +1,13 @@ package com.synebula.gaea.app import com.synebula.gaea.app.cmd.ICommandApp +import com.synebula.gaea.app.query.IQueryGenericApp import com.synebula.gaea.app.query.IQueryTypedApp import com.synebula.gaea.data.serialization.json.IJsonSerializer import com.synebula.gaea.domain.service.ICommand import com.synebula.gaea.domain.service.IService import com.synebula.gaea.log.ILogger +import com.synebula.gaea.query.IQuery import com.synebula.gaea.query.IQueryTyped import javax.annotation.Resource 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 37d3508..82df92b 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 @@ -2,64 +2,50 @@ package com.synebula.gaea.app.query import com.synebula.gaea.app.IApplication import com.synebula.gaea.app.component.HttpMessage -import com.synebula.gaea.data.message.Status -import com.synebula.gaea.query.IQuery -import com.synebula.gaea.query.PagingParam import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.RequestParam -/** - * 应用类接口,提供实现Query服务的接口 - * 依赖查询接 return this.safeExecute("获取${this.name}数据失败") { -口 @see IQuery - * - * @author alex - * @version 0.1 - * @since 2020-05-15 - */ interface IQueryApp : IApplication { - /** - * 查询服务 - */ - var query: IQuery? @GetMapping("/{key:.+}") fun get(@PathVariable key: TKey): HttpMessage { - return this.safeExecute("获取${this.name}数据失败") { - if (this.query != null) - it.data = this.query!!.get(key) - else { - it.status = Status.Error - it.message = "没有对应服务,无法执行该操作" - } - } + return this.doGet(key) } @GetMapping - fun list(@RequestParam parameters: MutableMap): HttpMessage { - return this.safeExecute("获取${this.name}列表数据失败") { - if (this.query != null) - it.data = this.query!!.list(parameters) - else { - it.status = Status.Error - it.message = "没有对应服务,无法执行该操作" - } - } + fun list(@RequestParam params: MutableMap): HttpMessage { + return this.doList(params) } @GetMapping("/segments/{size}/pages/{page}") fun paging(@PathVariable size: Int, @PathVariable page: Int, @RequestParam parameters: MutableMap): HttpMessage { - return this.safeExecute("获取${this.name}分页数据[条数:$size,页码:$page]失败") { - if (this.query != null) { - val params = PagingParam(page, size) - params.parameters = parameters - it.data = this.query!!.paging(params) - } else { - it.status = Status.Error - it.message = "没有对应服务,无法执行该操作" - } - } + return this.doPaging(size, page, parameters) } -} + + /** + * 实际查询单条数据逻辑 + * + * @param key 数据的主键 + */ + fun doGet(key: TKey): HttpMessage + + /** + * 实际查询列表逻辑 + * + * @param params 查询参数 + */ + fun doList(params: Map): HttpMessage + + /** + * 实际分页逻辑 + * + * @param size 单页数量 + * @param page 分页页码 + * @param params 查询参数 + */ + fun doPaging(size: Int, page: Int, params: MutableMap): HttpMessage + + +} \ No newline at end of file diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/IQueryGenericApp.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/IQueryGenericApp.kt new file mode 100644 index 0000000..d3cdeff --- /dev/null +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/IQueryGenericApp.kt @@ -0,0 +1,81 @@ +package com.synebula.gaea.app.query + +import com.synebula.gaea.app.IApplication +import com.synebula.gaea.app.component.HttpMessage +import com.synebula.gaea.data.message.Status +import com.synebula.gaea.query.IQuery +import com.synebula.gaea.query.PagingParam +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.RequestParam + +/** + * 应用类接口,提供实现Query服务的接口 + * 依赖查询接口 @see IQuery + * + * @author alex + * @version 0.1 + * @since 2020-05-15 + */ +interface IQueryGenericApp : IQueryApp { + /** + * 查询服务 + */ + var query: IQuery? + + + /** + * 实际查询单条数据逻辑 + * + * @param key 数据的主键 + */ + override fun doGet(key: TKey): HttpMessage { + return this.doQuery("获取${this.name}数据失败") { + this.query!!.get(key) + } + } + + /** + * 实际查询列表逻辑 + * + * @param params 查询参数 + */ + override fun doList(params: Map): HttpMessage { + return this.doQuery("获取${this.name}列表数据失败") { + this.query!!.list(params) + } + } + + /** + * 实际分页逻辑 + * + * @param size 单页数量 + * @param page 分页页码 + * @param params 查询参数 + */ + override fun doPaging(size: Int, page: Int, params: MutableMap): HttpMessage { + return this.doQuery("获取${this.name}分页数据[条数:$size,页码:$page]失败") { + val data = PagingParam(page, size) + data.parameters = params + this.query!!.paging(data) + } + } + + /** + * 抽取查询业务判断功能 + * + * @param error 错误消息 + * @param biz 业务执行逻辑 + */ + fun doQuery(error: String, biz: (() -> Any?)): HttpMessage { + return this.safeExecute(error) { + if (this.query != null) { + it.data = biz() + } else { + it.status = Status.Error + it.message = "没有对应服务,无法执行该操作" + } + } + } + +} diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/IQueryTypedApp.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/IQueryTypedApp.kt index 107ba24..c9f6c10 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/IQueryTypedApp.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/IQueryTypedApp.kt @@ -5,9 +5,6 @@ import com.synebula.gaea.app.component.HttpMessage import com.synebula.gaea.data.message.Status import com.synebula.gaea.query.IQueryTyped import com.synebula.gaea.query.PagingParam -import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.PathVariable -import org.springframework.web.bind.annotation.RequestParam /** * 应用类接口,提供实现Query服务的接口. @@ -17,7 +14,7 @@ import org.springframework.web.bind.annotation.RequestParam * @version 0.1 * @since 2020-05-15 */ -interface IQueryTypedApp : IApplication { +interface IQueryTypedApp : IApplication, IQueryApp { /** * 查询服务 */ @@ -28,42 +25,40 @@ interface IQueryTypedApp : IApplication { */ var viewClass: Class - @GetMapping("/{key:.+}") - fun get(@PathVariable key: TKey): HttpMessage { - return this.safeExecute("获取${this.name}数据失败") { - if (this.query != null) - it.data = this.query!!.get(key, viewClass) - else { - it.status = Status.Error - it.message = "没有对应服务,无法执行该操作" - } + override fun doGet(key: TKey): HttpMessage { + return this.doQuery("获取${this.name}数据失败") { + this.query!!.get(key, viewClass) } } - @GetMapping - fun list(@RequestParam parameters: MutableMap): HttpMessage { - return this.safeExecute("获取${this.name}列表数据失败") { - if (this.query != null) - it.data = this.query!!.list(parameters, viewClass) - else { - it.status = Status.Error - it.message = "没有对应服务,无法执行该操作" - } + override fun doList(params: Map): HttpMessage { + return this.doQuery("获取${this.name}列表数据失败") { + this.query!!.list(params, viewClass) } } - @GetMapping("/split/{size}/pages/{page}") - fun paging(@PathVariable page: Int, @PathVariable size: Int, @RequestParam parameters: MutableMap): HttpMessage { - return this.safeExecute("获取${this.name}分页数据[条数:$size,页码:$page]失败") { + override fun doPaging(size: Int, page: Int, params: MutableMap): HttpMessage { + return this.doQuery("获取${this.name}分页数据[条数:$size,页码:$page]失败") { + val data = PagingParam(page, size) + data.parameters = params + this.query!!.paging(data, viewClass) + } + } + + /** + * 抽取查询业务判断功能 + * + * @param error 错误消息 + * @param biz 业务执行逻辑 + */ + fun doQuery(error: String, biz: (() -> Any?)): HttpMessage { + return this.safeExecute(error) { if (this.query != null) { - val params = PagingParam(page, size) - params.parameters = parameters - it.data = this.query!!.paging(params, viewClass) + it.data = biz() } else { it.status = Status.Error it.message = "没有对应服务,无法执行该操作" } } } - } diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/QueryApp.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/QueryGenericApp.kt similarity index 76% rename from src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/QueryApp.kt rename to src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/QueryGenericApp.kt index 59f47fc..eae969f 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/QueryApp.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/QueryGenericApp.kt @@ -10,8 +10,8 @@ import com.synebula.gaea.query.IQuery * @param query 业务查询服务 * @param logger 日志组件 */ -open class QueryApp( +open class QueryGenericApp( override var name: String, override var query: IQuery?, - override var logger: ILogger) : IQueryApp { + override var logger: ILogger) : IQueryGenericApp { } \ No newline at end of file