package cn.reghao.devops.mgr.build.controller;

import cn.reghao.devops.mgr.build.model.po.RepoAuthConfig;
import cn.reghao.devops.mgr.build.service.RepoAuthConfigService;
import cn.reghao.jutil.jdk.result.Result;
import cn.reghao.jutil.jdk.result.WebResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author reghao
 * @date 2019-08-30 18:49:15
 */
@Slf4j
@Api(tags = "仓库认证配置接口")
@RestController
@RequestMapping("/api/build/repoauth")
public class RepoAuthConfigController {
    private final RepoAuthConfigService repoAuthConfigService;

    public RepoAuthConfigController(RepoAuthConfigService repoAuthConfigService) {
        this.repoAuthConfigService = repoAuthConfigService;
    }

    @ApiOperation(value = "添加/修改仓库认证配置", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public String addOrUpdateRepoConfig(@Validated RepoAuthConfig repoAuth) {
        Result result = repoAuthConfigService.addOrUpdate(repoAuth);
        return WebResult.result(result);
    }

    @ApiOperation(value = "删除仓库认证配置", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @DeleteMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteRepoConfig(@PathVariable("id") int id) {
        Result result = repoAuthConfigService.delete(id);
        return WebResult.result(result);
    }
}
