refactor(extension/crud): 重构删除接口,以解决批量删除时 URL 中 ID 列表过长问题

This commit is contained in:
2025-04-04 17:29:00 +08:00
parent a87104830f
commit 45da758dee
4 changed files with 104 additions and 11 deletions

View File

@@ -30,7 +30,8 @@ import top.continew.starter.extension.crud.enums.Api;
import top.continew.starter.extension.crud.handler.CrudApiHandler; import top.continew.starter.extension.crud.handler.CrudApiHandler;
import top.continew.starter.extension.crud.model.query.PageQuery; import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.query.SortQuery; import top.continew.starter.extension.crud.model.query.SortQuery;
import top.continew.starter.extension.crud.model.resp.BaseIdResp; import top.continew.starter.extension.crud.model.req.IdsReq;
import top.continew.starter.extension.crud.model.resp.IdResp;
import top.continew.starter.extension.crud.model.resp.BasePageResp; import top.continew.starter.extension.crud.model.resp.BasePageResp;
import top.continew.starter.extension.crud.service.BaseService; import top.continew.starter.extension.crud.service.BaseService;
import top.continew.starter.extension.crud.validation.CrudValidationGroup; import top.continew.starter.extension.crud.validation.CrudValidationGroup;
@@ -123,8 +124,8 @@ public abstract class AbstractBaseController<S extends BaseService<L, D, Q, C>,
@Operation(summary = "创建数据", description = "创建数据") @Operation(summary = "创建数据", description = "创建数据")
@ResponseBody @ResponseBody
@PostMapping @PostMapping
public BaseIdResp<Long> create(@Validated(CrudValidationGroup.Create.class) @RequestBody C req) { public IdResp<Long> create(@Validated(CrudValidationGroup.Create.class) @RequestBody C req) {
return new BaseIdResp<>(baseService.create(req)); return new IdResp<>(baseService.create(req));
} }
/** /**
@@ -145,15 +146,14 @@ public abstract class AbstractBaseController<S extends BaseService<L, D, Q, C>,
/** /**
* 删除 * 删除
* *
* @param ids ID 列表 * @param req 删除参数
*/ */
@CrudApi(Api.DELETE) @CrudApi(Api.DELETE)
@Operation(summary = "删除数据", description = "删除数据") @Operation(summary = "删除数据", description = "删除数据")
@Parameter(name = "ids", description = "ID 列表", example = "1,2", in = ParameterIn.PATH)
@ResponseBody @ResponseBody
@DeleteMapping("/{ids}") @DeleteMapping
public void delete(@PathVariable("ids") List<Long> ids) { public void delete(@Validated @RequestBody IdsReq req) {
baseService.delete(ids); baseService.delete(req.getIds());
} }
/** /**

View File

@@ -0,0 +1,46 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.
*/
package top.continew.starter.extension.crud.model.req;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import java.io.Serializable;
/**
* ID 请求参数
*
* @author Charles7c
* @since 2.11.0
*/
public class IdReq implements Serializable {
/**
* ID
*/
@Schema(description = "ID", example = "1")
@NotNull(message = "ID 不能为空")
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.
*/
package top.continew.starter.extension.crud.model.req;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.util.List;
/**
* ID 列表请求参数
*
* @author Charles7c
* @since 2.11.0
*/
public class IdsReq implements Serializable {
/**
* ID
*/
@Schema(description = "ID", example = "[1,2]")
@NotEmpty(message = "ID 不能为空")
private List<Long> ids;
public List<Long> getIds() {
return ids;
}
public void setIds(List<Long> ids) {
this.ids = ids;
}
}

View File

@@ -26,7 +26,7 @@ import java.io.Serializable;
* @author Charles7c * @author Charles7c
* @since 2.5.0 * @since 2.5.0
*/ */
public class BaseIdResp<T extends Serializable> implements Serializable { public class IdResp<T extends Serializable> implements Serializable {
/** /**
* ID * ID
@@ -42,10 +42,10 @@ public class BaseIdResp<T extends Serializable> implements Serializable {
this.id = id; this.id = id;
} }
public BaseIdResp() { public IdResp() {
} }
public BaseIdResp(final T id) { public IdResp(final T id) {
this.id = id; this.id = id;
} }
} }