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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
import top.charles7c.cnadmin.common.model.vo.R;

/**
 * 控制器基类
 *
 * @param <S>
 *            业务接口
 * @param <V>
 *            列表信息
 * @param <D>
 *            详情信息
 * @param <Q>
 *            查询条件
 * @param <C>
 *            创建信息
 * @param <U>
 *            修改信息
 * @author Charles7c
 * @since 2023/1/26 10:45
 */
@NoArgsConstructor
public abstract class BaseController<S extends BaseService<V, D, Q, C, U>, V, D, Q, C, U> {

    @Autowired
    protected S baseService;

    /**
     * 分页查询列表
     *
     * @param query
     *            查询条件
     * @param pageQuery
     *            分页查询条件
     * @return 分页列表信息
     */
    @Operation(summary = "分页查询列表")
    @GetMapping
70 71 72
    protected R<PageDataVO<V>> page(@Validated Q query, @Validated PageQuery pageQuery) {
        PageDataVO<V> pageDataVO = baseService.page(query, pageQuery);
        return R.ok(pageDataVO);
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    }

    /**
     * 查询列表
     *
     * @param query
     *            查询条件
     * @return 列表信息
     */
    @Operation(summary = "查询列表")
    @GetMapping("/all")
    protected R<List<V>> list(@Validated Q query) {
        List<V> list = baseService.list(query);
        return R.ok(list);
    }

    /**
     * 查看详情
     *
     * @param id
     *            ID
     * @return 详情信息
     */
    @Operation(summary = "查看详情")
    @Parameter(name = "id", description = "ID", in = ParameterIn.PATH)
    @GetMapping("/{id}")
    protected R<D> detail(@PathVariable Long id) {
        D detail = baseService.detail(id);
        return R.ok(detail);
    }

    /**
     * 新增
     *
     * @param request
     *            创建信息
     * @return 自增 ID
     */
    @Operation(summary = "新增")
    @PostMapping
    protected R<Long> create(@Validated @RequestBody C request) {
        Long id = baseService.create(request);
        return R.ok("新增成功", id);
    }

    /**
     * 修改
     *
     * @param id
     *            ID
     * @param request
     *            修改信息
     * @return /
     */
    @Operation(summary = "修改")
    @Parameter(name = "id", description = "ID", in = ParameterIn.PATH)
    @PutMapping("/{id}")
    protected R update(@PathVariable Long id, @Validated @RequestBody U request) {
        baseService.update(id, request);
        return R.ok("修改成功");
    }

    /**
     * 删除
     *
     * @param ids
     *            ID 列表
     * @return /
     */
    @Operation(summary = "删除")
    @Parameter(name = "ids", description = "ID 列表", in = ParameterIn.PATH)
    @DeleteMapping("/{ids}")
    protected R delete(@PathVariable List<Long> ids) {
        baseService.delete(ids);
        return R.ok("删除成功");
    }
}