|
- package com.example.webapi.controller;
-
- import com.example.webapi.dto.TransportCompanyQueryDTO;
- import com.example.webapi.entity.TransportCompany;
- import com.example.webapi.service.TransportCompanyService;
- import com.example.webapi.dto.ApiResponse;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import io.swagger.annotations.ApiParam;
- import io.swagger.v3.oas.annotations.media.Content;
- import io.swagger.v3.oas.annotations.media.Schema;
- import io.swagger.v3.oas.annotations.responses.ApiResponses;
- import io.swagger.v3.oas.annotations.tags.Tag;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.domain.Page;
- import org.springframework.web.bind.annotation.*;
-
- import java.util.Optional;
-
- @Api(tags = "运输公司管理")
- @Tag(name = "运输公司管理", description = "提供运输公司的增删改查接口")
- @RestController
- @RequestMapping("/transport-company")
- public class TransportCompanyController {
-
- @Autowired
- private TransportCompanyService service;
-
- @ApiOperation(value = "条件查询运输公司(分页)", notes = "根据提供的查询条件分页查询运输公司")
- @ApiResponses(value = {
- @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功",
- content = @Content(mediaType = "application/json",
- schema = @Schema(implementation = com.example.webapi.dto.ApiResponse.class))),
- @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
- })
- @PostMapping("/page/conditions")
- public ApiResponse<Page<TransportCompany>> pageByConditions(
- @ApiParam(value = "查询条件DTO,包含分页信息和过滤条件", required = true)
- @RequestBody TransportCompanyQueryDTO queryDTO) {
- return ApiResponse.success(service.findByConditions(queryDTO));
- }
-
- @ApiOperation(value = "根据ID查询运输公司", notes = "根据ID获取单条运输公司详情")
- @ApiResponses(value = {
- @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功",
- content = @Content(mediaType = "application/json",
- schema = @Schema(implementation = com.example.webapi.dto.ApiResponse.class))),
- @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "记录不存在"),
- @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
- })
- @GetMapping("/{id}")
- public ApiResponse<TransportCompany> getById(
- @ApiParam(value = "运输公司ID", required = true, example = "1")
- @PathVariable Integer id) {
- Optional<TransportCompany> result = service.findById(id);
- return result.map(ApiResponse::success).orElseGet(() -> ApiResponse.error("未找到该运输公司"));
- }
-
- @ApiOperation(value = "新增运输公司", notes = "创建新的运输公司")
- @ApiResponses(value = {
- @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "创建成功",
- content = @Content(mediaType = "application/json",
- schema = @Schema(implementation = com.example.webapi.dto.ApiResponse.class))),
- @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "请求参数错误"),
- @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
- })
- @PostMapping
- public ApiResponse<TransportCompany> create(
- @ApiParam(value = "运输公司实体", required = true)
- @RequestBody TransportCompany entity) {
- return ApiResponse.success(service.save(entity));
- }
-
- @ApiOperation(value = "更新运输公司", notes = "根据ID更新运输公司信息")
- @ApiResponses(value = {
- @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "更新成功",
- content = @Content(mediaType = "application/json",
- schema = @Schema(implementation = com.example.webapi.dto.ApiResponse.class))),
- @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "请求参数错误"),
- @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "记录不存在"),
- @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
- })
- @PutMapping("/{id}")
- public ApiResponse<TransportCompany> update(
- @ApiParam(value = "运输公司ID", required = true, example = "1")
- @PathVariable Integer id,
- @ApiParam(value = "更新后的运输公司实体", required = true)
- @RequestBody TransportCompany entity) {
- entity.setId(id);
- return ApiResponse.success(service.save(entity));
- }
-
- @ApiOperation(value = "删除运输公司", notes = "根据ID删除运输公司")
- @ApiResponses(value = {
- @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "删除成功",
- content = @Content(mediaType = "application/json",
- schema = @Schema(implementation = com.example.webapi.dto.ApiResponse.class))),
- @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "记录不存在"),
- @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误")
- })
- @DeleteMapping("/{id}")
- public ApiResponse<Void> delete(
- @ApiParam(value = "运输公司ID", required = true, example = "1")
- @PathVariable Integer id) {
- service.deleteById(id);
- return ApiResponse.success();
- }
- }
|