package com.ruoyi.platform.controller.codeConfig;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.GenericsAjaxResult;
import com.ruoyi.platform.domain.CodeConfig;
import com.ruoyi.platform.service.CodeConfigService;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("codeConfig")
@Api("代码配置")
public class CodeConfigController extends BaseController {

    @Resource
    private CodeConfigService codeConfigService;

    /**
     * 分页查询
     *
     * @param codeConfig 筛选条件
     * @param page       页数
     * @param size       每页大小
     * @return 查询结果
     */
    @GetMapping
    public GenericsAjaxResult<Page<CodeConfig>> queryByPage(@RequestParam("page") int page,
                                                            @RequestParam("size") int size,
                                                            @RequestParam(value = "code_repo_name", required = false) String codeRepoName
                                                            ) {
        PageRequest pageRequest = PageRequest.of(page, size);
        CodeConfig codeConfig = new CodeConfig();
        codeConfig.setCodeRepoName(codeRepoName);
        return genericsSuccess(this.codeConfigService.queryByPage(codeConfig, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id
     * @return 单条数据
     */
    @GetMapping("{id}")
    public GenericsAjaxResult<CodeConfig> queryById(@PathVariable("id") Long id) throws Exception {
        return genericsSuccess(this.codeConfigService.queryById(id));
    }


    @PostMapping
    public GenericsAjaxResult<CodeConfig> add(@RequestBody CodeConfig codeConfig) {
        return genericsSuccess(this.codeConfigService.insert(codeConfig));
    }

    @PutMapping
    public GenericsAjaxResult<CodeConfig> update(@RequestBody CodeConfig codeConfig) {
        return genericsSuccess(this.codeConfigService.update(codeConfig));
    }

    @DeleteMapping("{id}")
    public GenericsAjaxResult<String> delete(@PathVariable("id") Long id) throws Exception {
        return genericsSuccess(this.codeConfigService.removeById(id));
    }
}
