Skip to content

Commit

Permalink
🏁 v2.5.2.40
Browse files Browse the repository at this point in the history
1. 重构基础Controller,将基础Controller的读操作与其它操作分离,以支持“视图”相关Controller的编写。
  • Loading branch information
herodotus-ecosystem committed Jul 7, 2021
1 parent 3db2668 commit f0a28af
Show file tree
Hide file tree
Showing 31 changed files with 256 additions and 92 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<a href="https://nacos.io/zh-cn/index.html" target="_blank"><img src="https://img.shields.io/badge/Nacos-2.0.2-brightgreen" alt="Nacos 2.0.2"></a>
<a href="./LICENSE"><img src="https://img.shields.io/badge/License-Apache--2.0-blue" alt="License Apache 2.0"></a>
<a href="https://blog.csdn.net/Pointer_v" target="_blank"><img src="https://img.shields.io/badge/Author-%E7%A0%81%E5%8C%A0%E5%90%9B-orange" alt="码匠君"></a>
<a href="#" target="_blank"><img src="https://img.shields.io/badge/Version-2.5.2.30-red" alt="Version 2.5.2.30"></a>
<a href="#" target="_blank"><img src="https://travis-ci.com/herodotus-cloud/eurynome-cloud.svg?branch=master" alt="Version 2.5.2.30"></a>
<a href="#" target="_blank"><img src="https://img.shields.io/badge/Version-2.5.2.40-red" alt="Version 2.5.2.40"></a>
<a href="#" target="_blank"><img src="https://travis-ci.com/herodotus-cloud/eurynome-cloud.svg?branch=master" alt="Version 2.5.2.40"></a>
<a href="https://gitee.com/herodotus/eurynome-cloud"><img src="https://gitee.com/herodotus/eurynome-cloud/badge/star.svg?theme=dark" alt="Gitee star"></a>
<a href="https://gitee.com/herodotus/eurynome-cloud"><img src="https://gitee.com/herodotus/eurynome-cloud/badge/fork.svg?theme=dark" alt="Gitee fork"></a>
</p>
Expand Down
2 changes: 1 addition & 1 deletion configurations/docker/docker-compose/herodotus/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=2.5.2.30
VERSION=2.5.2.40
GATEWAY_SERVICE_NAME=eurynome-cloud-gateway
GATEWAY_SERVICE_PORT=8847
UAA_SERVICE_NAME=eurynome-cloud-uaa
Expand Down
2 changes: 1 addition & 1 deletion dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

<groupId>cn.herodotus.eurynome</groupId>
<artifactId>dependencies</artifactId>
<version>2.5.2.30</version>
<version>2.5.2.40</version>
<packaging>pom</packaging>

<name>eurynome-cloud-dependencies</name>
Expand Down
4 changes: 2 additions & 2 deletions packages/eurynome-cloud-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
<parent>
<artifactId>packages</artifactId>
<groupId>cn.herodotus.eurynome</groupId>
<version>2.5.2.30</version>
<version>2.5.2.40</version>
</parent>

<artifactId>eurynome-cloud-common</artifactId>
<version>2.5.2.30</version>
<version>2.5.2.40</version>
<packaging>jar</packaging>

<description>结对基础通用的工具类包,此包的定位是放在任何工程中都可以使用,而且尽可能依赖少的存在上下文组件</description>
Expand Down
4 changes: 2 additions & 2 deletions packages/eurynome-cloud-crud/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
<parent>
<artifactId>packages</artifactId>
<groupId>cn.herodotus.eurynome</groupId>
<version>2.5.2.30</version>
<version>2.5.2.40</version>
</parent>

<artifactId>eurynome-cloud-crud</artifactId>
<version>2.5.2.30</version>
<version>2.5.2.40</version>
<packaging>jar</packaging>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,30 @@

package cn.herodotus.eurynome.crud.controller;

import cn.herodotus.eurynome.common.domain.Result;
import cn.herodotus.eurynome.common.definition.entity.AbstractEntity;
import cn.herodotus.eurynome.crud.service.BaseService;
import org.springframework.data.domain.Page;
import cn.herodotus.eurynome.common.domain.Result;
import cn.herodotus.eurynome.crud.service.BaseReadableService;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
* <p> Description : BaseController 单独提取出一些公共方法,是为了解决某些支持feign的controller,requestMapping 不方便统一编写的问题。 </p>
* <p> Description : 通用Controller </p>
*
* 单独提取出一些公共方法,是为了解决某些支持feign的controller,requestMapping 不方便统一编写的问题。
*
* @author : gengwei.zheng
* @date : 2020/4/30 11:00
*/
public abstract class BaseController<E extends AbstractEntity, ID extends Serializable> extends AbstractController {
public abstract class BaseController<E extends AbstractEntity, ID extends Serializable> implements WriteableController<E, ID>, ReadableController<E, ID> {

/**
* 获取Service
*
* @return Service
*/
public abstract BaseService<E, ID> getBaseService();

public Result<Map<String, Object>> findByPage(Integer pageNumber, Integer pageSize) {
Page<E> pages = getBaseService().findByPage(pageNumber, pageSize);
return result(pages);
@Override
public BaseReadableService<E, ID> getBaseReadableService() {
return this.getBaseService();
}

public Result<E> saveOrUpdate(E domain) {
Expand All @@ -60,9 +58,4 @@ public Result<String> delete(ID id) {
getBaseService().deleteById(id);
return result;
}

public Result<List<E>> findAll() {
List<E> domains = getBaseService().findAll();
return result(domains);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2019-2021 Gengwei Zheng ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project Name: eurynome-cloud
* Module Name: eurynome-cloud-crud
* File Name: BaseReadableRestController.java
* Author: gengwei.zheng
* Date: 2021/07/07 17:38:07
*/

package cn.herodotus.eurynome.crud.controller;

import cn.herodotus.eurynome.common.definition.entity.AbstractEntity;
import cn.herodotus.eurynome.common.domain.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.Serializable;
import java.util.Map;

/**
* <p>Description: 只读RestController </p>
*
* @author : gengwei.zheng
* @date : 2021/7/7 17:38
*/
public abstract class BaseReadableRestController<E extends AbstractEntity, ID extends Serializable> implements ReadableController<E, ID> {

@ApiOperation(value = "分页查询数据", notes = "通过pageNumber和pageSize获取分页数据", produces = "application/json")
@ApiImplicitParams({
@ApiImplicitParam(name = "pageNumber", required = true, value = "当前页数", dataType = "int", paramType = "query"),
@ApiImplicitParam(name = "pageSize", required = true, value = "每页显示数据条目", dataType = "int", paramType = "query")
})
@GetMapping
@Override
public Result<Map<String, Object>> findByPage(
@RequestParam("pageNumber") Integer pageNumber,
@RequestParam("pageSize") Integer pageSize) {
return ReadableController.super.findByPage(pageNumber, pageSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,29 @@

package cn.herodotus.eurynome.crud.controller;

import cn.herodotus.eurynome.common.domain.Result;
import cn.herodotus.eurynome.common.definition.entity.AbstractEntity;
import cn.herodotus.eurynome.common.domain.Result;
import cn.herodotus.eurynome.crud.service.BaseReadableService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.Serializable;
import java.util.Map;

/**
* <p> Description : BaseRestController </p>
*
* @author : gengwei.zheng
* @date : 2020/2/29 15:28
*/
public abstract class BaseRestController<E extends AbstractEntity, ID extends Serializable> extends BaseController<E, ID> {
public abstract class BaseRestController<E extends AbstractEntity, ID extends Serializable> extends BaseReadableRestController<E, ID> implements WriteableController<E, ID> {

@ApiOperation(value = "分页查询数据", notes = "通过pageNumber和pageSize获取分页数据", produces = "application/json")
@ApiImplicitParams({
@ApiImplicitParam(name = "pageNumber", required = true, value = "当前页数", dataType = "int", paramType = "query"),
@ApiImplicitParam(name = "pageSize", required = true, value = "每页显示数据条目", dataType = "int", paramType = "query")
})
@GetMapping
@Override
public Result<Map<String, Object>> findByPage(
@RequestParam("pageNumber") Integer pageNumber,
@RequestParam("pageSize") Integer pageSize) {
return super.findByPage(pageNumber, pageSize);
public BaseReadableService<E, ID> getBaseReadableService() {
return this.getBaseService();
}

@ApiOperation(value = "保存或更新数据", notes = "接收JSON数据,转换为实体,进行保存或更新", produces = "application/json", consumes = "application/json")
Expand All @@ -60,7 +54,7 @@ public Result<Map<String, Object>> findByPage(
@PostMapping
@Override
public Result<E> saveOrUpdate(@RequestBody E domain) {
return super.saveOrUpdate(domain);
return WriteableController.super.saveOrUpdate(domain);
}

@ApiOperation(value = "删除数据", notes = "根据实体ID删除数据,以及相关联的关联数据", consumes = "application/json")
Expand All @@ -70,6 +64,6 @@ public Result<E> saveOrUpdate(@RequestBody E domain) {
@DeleteMapping
@Override
public Result<String> delete(@RequestBody ID id) {
return super.delete(id);
return WriteableController.super.delete(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2021 Gengwei Zheng([email protected])
* Copyright (c) 2019-2021 Gengwei Zheng ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,15 +15,15 @@
*
* Project Name: eurynome-cloud
* Module Name: eurynome-cloud-crud
* File Name: AbstractController.java
* File Name: Controller.java
* Author: gengwei.zheng
* Date: 2021/05/07 11:28:07
* Date: 2021/07/07 17:24:07
*/

package cn.herodotus.eurynome.crud.controller;

import cn.herodotus.eurynome.common.domain.Result;
import cn.herodotus.eurynome.common.definition.entity.AbstractEntity;
import cn.herodotus.eurynome.common.domain.Result;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
Expand All @@ -35,14 +35,16 @@
import java.util.Map;

/**
* <p> Description : BaseController </p>
* <p>Description: Controller基础定义 </p>
* <p>
* 这里只在方法上做了泛型,主要是考虑到返回的结果数据可以是各种类型,而不一定受限于某一种类型。
*
* @author : gengwei.zheng
* @date : 2020/4/29 18:56
* @date : 2021/7/7 17:24
*/
public abstract class AbstractController {
public interface Controller {

protected <E extends AbstractEntity> Result<E> result(E domain) {
default <E extends AbstractEntity> Result<E> result(E domain) {
Result<E> result = new Result<>();
if (ObjectUtils.isNotEmpty(domain)) {
return result.ok().message("操作成功!").data(domain);
Expand All @@ -51,7 +53,7 @@ protected <E extends AbstractEntity> Result<E> result(E domain) {
}
}

protected <E extends AbstractEntity> Result<List<E>> result(List<E> domains) {
default <E extends AbstractEntity> Result<List<E>> result(List<E> domains) {
Result<List<E>> result = new Result<>();
if (CollectionUtils.isNotEmpty(domains)) {
return result.ok().message("查询数据成功!").data(domains);
Expand All @@ -60,7 +62,7 @@ protected <E extends AbstractEntity> Result<List<E>> result(List<E> domains) {
}
}

protected <E extends AbstractEntity> Result<Map<String, Object>> result(Page<E> pages) {
default <E extends AbstractEntity> Result<Map<String, Object>> result(Page<E> pages) {
Result<Map<String, Object>> result = new Result<>();
if (ObjectUtils.isNotEmpty(pages)) {
return result.ok().message("查询数据成功!").data(getPageInfoMap(pages));
Expand All @@ -69,7 +71,7 @@ protected <E extends AbstractEntity> Result<Map<String, Object>> result(Page<E>
}
}

protected Result<Map<String, Object>> result(Map<String, Object> map) {
default Result<Map<String, Object>> result(Map<String, Object> map) {
Result<Map<String, Object>> result = new Result<>();
if (MapUtils.isNotEmpty(map)) {
return result.ok().message("查询数据成功!").data(map);
Expand All @@ -78,7 +80,7 @@ protected Result<Map<String, Object>> result(Map<String, Object> map) {
}
}

protected <ID extends Serializable> Result<String> result(ID parameter) {
default <ID extends Serializable> Result<String> result(ID parameter) {
Result<String> result = new Result<>();
if (ObjectUtils.isNotEmpty(parameter)) {
return result.ok().message("操作成功!");
Expand All @@ -87,11 +89,11 @@ protected <ID extends Serializable> Result<String> result(ID parameter) {
}
}

protected <E extends AbstractEntity> Map<String, Object> getPageInfoMap(Page<E> pages) {
default <E extends AbstractEntity> Map<String, Object> getPageInfoMap(Page<E> pages) {
return getPageInfoMap(pages.getContent(), pages.getTotalPages(), pages.getTotalElements());
}

protected <E extends AbstractEntity> Map<String, Object> getPageInfoMap(List<E> content, int totalPages, long totalElements) {
default <E extends AbstractEntity> Map<String, Object> getPageInfoMap(List<E> content, int totalPages, long totalElements) {
Map<String, Object> result = new HashMap<>(8);
result.put("content", content);
result.put("totalPages", totalPages);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2019-2021 Gengwei Zheng ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project Name: eurynome-cloud
* Module Name: eurynome-cloud-crud
* File Name: ReadableController.java
* Author: gengwei.zheng
* Date: 2021/07/07 17:27:07
*/

package cn.herodotus.eurynome.crud.controller;

import cn.herodotus.eurynome.common.definition.entity.AbstractEntity;
import cn.herodotus.eurynome.common.domain.Result;
import cn.herodotus.eurynome.crud.service.BaseReadableService;
import org.springframework.data.domain.Page;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
* <p>Description: 只读Controller </p>
*
* @author : gengwei.zheng
* @date : 2021/7/7 17:27
*/
public interface ReadableController<E extends AbstractEntity, ID extends Serializable> extends Controller {

/**
* 获取Service
*
* @return Service
*/
BaseReadableService<E, ID> getBaseReadableService();

default Result<Map<String, Object>> findByPage(Integer pageNumber, Integer pageSize) {
Page<E> pages = getBaseReadableService().findByPage(pageNumber, pageSize);
return result(pages);
}

default Result<List<E>> findAll() {
List<E> domains = getBaseReadableService().findAll();
return result(domains);
}

default Result<E> findById(ID id) {
E domain = getBaseReadableService().findById(id);
return result(domain);
}
}
Loading

0 comments on commit f0a28af

Please sign in to comment.