BaseController.java 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * 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.
 */

package top.charles7c.cnadmin.common.base;

import java.util.List;

import lombok.NoArgsConstructor;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import top.charles7c.cnadmin.common.model.query.PageQuery;
32
import top.charles7c.cnadmin.common.model.vo.PageDataVO;
33 34 35 36 37 38 39 40 41 42 43 44 45 46
import top.charles7c.cnadmin.common.model.vo.R;

/**
 * 控制器基类
 *
 * @param <S>
 *            业务接口
 * @param <V>
 *            列表信息
 * @param <D>
 *            详情信息
 * @param <Q>
 *            查询条件
 * @param <C>
47
 *            创建或修改信息
48 49 50 51
 * @author Charles7c
 * @since 2023/1/26 10:45
 */
@NoArgsConstructor
52
public abstract class BaseController<S extends BaseService<V, D, Q, C>, V, D, Q, C extends BaseRequest> {
53 54 55 56 57 58 59 60 61 62 63

    @Autowired
    protected S baseService;

    /**
     * 分页查询列表
     *
     * @param query
     *            查询条件
     * @param pageQuery
     *            分页查询条件
64
     * @return 分页信息
65 66
     */
    @Operation(summary = "分页查询列表")
67
    @ResponseBody
68
    @GetMapping
69 70 71
    protected R<PageDataVO<V>> page(@Validated Q query, @Validated PageQuery pageQuery) {
        PageDataVO<V> pageDataVO = baseService.page(query, pageQuery);
        return R.ok(pageDataVO);
72 73 74 75 76 77 78 79 80 81
    }

    /**
     * 查询列表
     *
     * @param query
     *            查询条件
     * @return 列表信息
     */
    @Operation(summary = "查询列表")
82
    @ResponseBody
83 84 85 86 87 88 89 90 91 92 93 94 95
    @GetMapping("/all")
    protected R<List<V>> list(@Validated Q query) {
        List<V> list = baseService.list(query);
        return R.ok(list);
    }

    /**
     * 查看详情
     *
     * @param id
     *            ID
     * @return 详情信息
     */
96
    @Operation(summary = "查看详情")
97
    @Parameter(name = "id", description = "ID", in = ParameterIn.PATH)
98
    @ResponseBody
99
    @GetMapping("/{id}")
100 101
    protected R<D> get(@PathVariable Long id) {
        D detail = baseService.get(id);
102 103 104 105 106 107 108 109 110 111
        return R.ok(detail);
    }

    /**
     * 新增
     *
     * @param request
     *            创建信息
     * @return 自增 ID
     */
112
    @Operation(summary = "新增数据")
113
    @ResponseBody
114
    @PostMapping
115
    protected R<Long> create(@Validated(BaseRequest.Create.class) @RequestBody C request) {
116 117 118 119 120 121 122 123 124 125 126
        Long id = baseService.create(request);
        return R.ok("新增成功", id);
    }

    /**
     * 修改
     *
     * @param request
     *            修改信息
     * @return /
     */
127
    @Operation(summary = "修改数据")
128 129 130 131
    @ResponseBody
    @PutMapping
    protected R update(@Validated(BaseRequest.Update.class) @RequestBody C request) {
        baseService.update(request);
132 133 134 135 136 137 138 139 140 141
        return R.ok("修改成功");
    }

    /**
     * 删除
     *
     * @param ids
     *            ID 列表
     * @return /
     */
142
    @Operation(summary = "删除数据")
143
    @Parameter(name = "ids", description = "ID 列表", in = ParameterIn.PATH)
144
    @ResponseBody
145 146 147 148 149 150
    @DeleteMapping("/{ids}")
    protected R delete(@PathVariable List<Long> ids) {
        baseService.delete(ids);
        return R.ok("删除成功");
    }
}