package cn.reghao.autodop.dmaster.app.controller.config;

import cn.reghao.autodop.dmaster.app.model.po.config.build.RepoAuthConfig;
import cn.reghao.autodop.dmaster.app.service.config.RepoAuthConfigService;
import cn.reghao.jdkutil.result.Result;
import cn.reghao.jdkutil.result.WebBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
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/app/config/build/repoauth")
public class RepoAuthConfigController {
    private final RepoAuthConfigService repoAuthConfigService;

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

    @ApiOperation(value = "添加/修改仓库认证配置")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public String addRepoConfig(@Validated RepoAuthConfig repoAuth) {
        Integer id = repoAuth.getId();
        Result result;
        if (id == null) {
            result = repoAuthConfigService.add(repoAuth);
        } else {
            result = repoAuthConfigService.modify(repoAuth);
        }

        return WebBody.result(result);
    }

    @ApiOperation(value = "删除仓库认证配置")
    @DeleteMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteRepoConfig(@PathVariable("id") RepoAuthConfig repoAuth) {
        repoAuthConfigService.delete(repoAuth);
        return WebBody.success();
    }
}
