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

import cn.reghao.devops.common.docker.model.DockerAuth;
import cn.reghao.devops.mgr.build.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.security.access.prepost.PreAuthorize;
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/build")
public class DockerRegistryController {
    private final DockerRegistryService dockerRegistryService;

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

    @ApiOperation(value = "添加/修改 DockerRegistry 配置", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping(value = "/registry", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addOrUpdateRepoConfig(@Validated DockerAuth dockerAuth) {
        dockerRegistryService.addOrUpdate(dockerAuth);
        return WebResult.success();
    }

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