package cn.reghao.devops.web.mgr.builds.controller;

import cn.reghao.devops.common.docker.po.DockerAuth;
import cn.reghao.devops.web.mgr.builds.service.DockerRegistryService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author reghao
 * @date 2024-07-26 19:15:15
 */
@Slf4j
@Api(tags = "DockerRegistry 配置接口")
@RestController
@RequestMapping("/api/docker")
public class DockerRegistryController {
    private final DockerRegistryService dockerRegistryService;

    public DockerRegistryController(DockerRegistryService dockerRegistryService) {
        this.dockerRegistryService = dockerRegistryService;
    }

    @ApiOperation(value = "添加/修改 DockerRegistry 配置")
    @PostMapping(value = "/registry", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addOrModifyRepoConfig(@Validated DockerAuth dockerAuth) {
        dockerRegistryService.addOrModify(dockerAuth);
        return WebResult.success();
    }

    @ApiOperation(value = "删除 DockerRegistry 配置")
    @DeleteMapping(value = "/registry/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteRepoConfig(@PathVariable("id") Integer id) {
        dockerRegistryService.delete(id);
        return WebResult.success();
    }
}
