refactor(extension/crud): PageDataResp => PageResp

This commit is contained in:
2024-01-03 21:34:48 +08:00
parent d68d88db21
commit 38d28004d6
4 changed files with 36 additions and 41 deletions

View File

@@ -32,7 +32,7 @@ import top.charles7c.continew.starter.extension.crud.annotation.CrudRequestMappi
import top.charles7c.continew.starter.extension.crud.enums.Api;
import top.charles7c.continew.starter.extension.crud.model.query.PageQuery;
import top.charles7c.continew.starter.extension.crud.model.query.SortQuery;
import top.charles7c.continew.starter.extension.crud.model.resp.PageDataResp;
import top.charles7c.continew.starter.extension.crud.model.resp.PageResp;
import top.charles7c.continew.starter.extension.crud.model.resp.R;
import java.util.List;
@@ -64,10 +64,9 @@ public abstract class BaseController<S extends BaseService<L, D, Q, C>, L, D, Q,
@Operation(summary = "分页查询列表", description = "分页查询列表")
@ResponseBody
@GetMapping
public R<PageDataResp<L>> page(Q query, @Validated PageQuery pageQuery) {
public R<PageResp<L>> page(Q query, @Validated PageQuery pageQuery) {
this.checkPermission(Api.LIST);
PageDataResp<L> pageData = baseService.page(query, pageQuery);
return R.ok(pageData);
return R.ok(baseService.page(query, pageQuery));
}
/**
@@ -82,8 +81,7 @@ public abstract class BaseController<S extends BaseService<L, D, Q, C>, L, D, Q,
@GetMapping("/tree")
public R<List<Tree<Long>>> tree(Q query, SortQuery sortQuery) {
this.checkPermission(Api.LIST);
List<Tree<Long>> list = baseService.tree(query, sortQuery, false);
return R.ok(list);
return R.ok(baseService.tree(query, sortQuery, false));
}
/**
@@ -98,8 +96,7 @@ public abstract class BaseController<S extends BaseService<L, D, Q, C>, L, D, Q,
@GetMapping("/list")
public R<List<L>> list(Q query, SortQuery sortQuery) {
this.checkPermission(Api.LIST);
List<L> list = baseService.list(query, sortQuery);
return R.ok(list);
return R.ok(baseService.list(query, sortQuery));
}
/**
@@ -114,8 +111,7 @@ public abstract class BaseController<S extends BaseService<L, D, Q, C>, L, D, Q,
@GetMapping("/{id}")
public R<D> get(@PathVariable Long id) {
this.checkPermission(Api.LIST);
D detail = baseService.get(id);
return R.ok(detail);
return R.ok(baseService.get(id));
}
/**
@@ -129,8 +125,7 @@ public abstract class BaseController<S extends BaseService<L, D, Q, C>, L, D, Q,
@PostMapping
public R<Long> add(@Validated(ValidateGroup.Crud.Add.class) @RequestBody C req) {
this.checkPermission(Api.ADD);
Long id = baseService.add(req);
return R.ok("新增成功", id);
return R.ok("新增成功", baseService.add(req));
}
/**

View File

@@ -20,7 +20,7 @@ import cn.hutool.core.lang.tree.Tree;
import jakarta.servlet.http.HttpServletResponse;
import top.charles7c.continew.starter.extension.crud.model.query.PageQuery;
import top.charles7c.continew.starter.extension.crud.model.query.SortQuery;
import top.charles7c.continew.starter.extension.crud.model.resp.PageDataResp;
import top.charles7c.continew.starter.extension.crud.model.resp.PageResp;
import java.util.List;
@@ -43,7 +43,7 @@ public interface BaseService<L, D, Q, C extends BaseReq> {
* @param pageQuery 分页查询条件
* @return 分页列表信息
*/
PageDataResp<L> page(Q query, PageQuery pageQuery);
PageResp<L> page(Q query, PageQuery pageQuery);
/**
* 查询树列表

View File

@@ -41,7 +41,7 @@ import top.charles7c.continew.starter.data.mybatis.plus.query.QueryHelper;
import top.charles7c.continew.starter.extension.crud.annotation.TreeField;
import top.charles7c.continew.starter.extension.crud.model.query.PageQuery;
import top.charles7c.continew.starter.extension.crud.model.query.SortQuery;
import top.charles7c.continew.starter.extension.crud.model.resp.PageDataResp;
import top.charles7c.continew.starter.extension.crud.model.resp.PageResp;
import top.charles7c.continew.starter.extension.crud.util.TreeUtils;
import top.charles7c.continew.starter.file.excel.util.ExcelUtils;
@@ -78,12 +78,12 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO,
}
@Override
public PageDataResp<L> page(Q query, PageQuery pageQuery) {
public PageResp<L> page(Q query, PageQuery pageQuery) {
QueryWrapper<T> queryWrapper = QueryHelper.build(query);
IPage<T> page = baseMapper.selectPage(pageQuery.toPage(), queryWrapper);
PageDataResp<L> pageDataResp = PageDataResp.build(page, listClass);
pageDataResp.getList().forEach(this::fill);
return pageDataResp;
PageResp<L> pageResp = PageResp.build(page, listClass);
pageResp.getList().forEach(this::fill);
return pageResp;
}
@Override

View File

@@ -36,7 +36,7 @@ import java.util.List;
*/
@Data
@Schema(description = "分页信息")
public class PageDataResp<L> implements Serializable {
public class PageResp<L> implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@@ -62,14 +62,14 @@ public class PageDataResp<L> implements Serializable {
* @param <L> 目标列表数据类型
* @return 分页信息
*/
public static <T, L> PageDataResp<L> build(IPage<T> page, Class<L> targetClass) {
public static <T, L> PageResp<L> build(IPage<T> page, Class<L> targetClass) {
if (null == page) {
return empty();
}
PageDataResp<L> pageDataResp = new PageDataResp<>();
pageDataResp.setList(BeanUtil.copyToList(page.getRecords(), targetClass));
pageDataResp.setTotal(page.getTotal());
return pageDataResp;
PageResp<L> pageResp = new PageResp<>();
pageResp.setList(BeanUtil.copyToList(page.getRecords(), targetClass));
pageResp.setTotal(page.getTotal());
return pageResp;
}
/**
@@ -79,14 +79,14 @@ public class PageDataResp<L> implements Serializable {
* @param <L> 列表数据类型
* @return 分页信息
*/
public static <L> PageDataResp<L> build(IPage<L> page) {
public static <L> PageResp<L> build(IPage<L> page) {
if (null == page) {
return empty();
}
PageDataResp<L> pageDataResp = new PageDataResp<>();
pageDataResp.setList(page.getRecords());
pageDataResp.setTotal(page.getTotal());
return pageDataResp;
PageResp<L> pageResp = new PageResp<>();
pageResp.setList(page.getRecords());
pageResp.setTotal(page.getTotal());
return pageResp;
}
/**
@@ -98,23 +98,23 @@ public class PageDataResp<L> implements Serializable {
* @param <L> 列表数据类型
* @return 分页信息
*/
public static <L> PageDataResp<L> build(int page, int size, List<L> list) {
public static <L> PageResp<L> build(int page, int size, List<L> list) {
if (CollUtil.isEmpty(list)) {
return empty();
}
PageDataResp<L> pageDataResp = new PageDataResp<>();
pageDataResp.setTotal(list.size());
PageResp<L> pageResp = new PageResp<>();
pageResp.setTotal(list.size());
// 对列表数据进行分页
int fromIndex = (page - 1) * size;
int toIndex = page * size + size;
if (fromIndex > list.size()) {
pageDataResp.setList(new ArrayList<>(0));
pageResp.setList(new ArrayList<>(0));
} else if (toIndex >= list.size()) {
pageDataResp.setList(list.subList(fromIndex, list.size()));
pageResp.setList(list.subList(fromIndex, list.size()));
} else {
pageDataResp.setList(list.subList(fromIndex, toIndex));
pageResp.setList(list.subList(fromIndex, toIndex));
}
return pageDataResp;
return pageResp;
}
/**
@@ -123,9 +123,9 @@ public class PageDataResp<L> implements Serializable {
* @param <L> 列表数据类型
* @return 分页信息
*/
private static <L> PageDataResp<L> empty() {
PageDataResp<L> pageDataResp = new PageDataResp<>();
pageDataResp.setList(new ArrayList<>(0));
return pageDataResp;
private static <L> PageResp<L> empty() {
PageResp<L> pageResp = new PageResp<>();
pageResp.setList(new ArrayList<>(0));
return pageResp;
}
}