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

import cn.reghao.devops.mgr.build.db.repository.DockerRegistryRepository;
import cn.reghao.devops.mgr.build.model.po.DockerRegistry;
import cn.reghao.devops.mgr.util.PageSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author reghao
 * @date 2024-07-26 17:36:37
 */
@Api(tags = "DockerRegistry 配置页面")
@Controller
@RequestMapping("/build")
public class DockerRegistryPageController {
    private final DockerRegistryRepository dockerAuthRepository;

    public DockerRegistryPageController(DockerRegistryRepository dockerAuthRepository) {
        this.dockerAuthRepository = dockerAuthRepository;
    }

    @ApiOperation(value = "DockerRegistry 列表页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/registry")
    public String repoAuthPage(Model model) {
        PageRequest pageRequest = PageSort.pageRequest();
        Page<DockerRegistry> page = dockerAuthRepository.findAll(pageRequest);

        model.addAttribute("page", page);
        model.addAttribute("list", page.getContent());
        return "/devops/build/docker/index";
    }

    @ApiOperation(value = "DockerRegistry 添加页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/registry/add")
    public String addRepoAuthPage(Model model) {
        return "/devops/build/docker/add";
    }

    @ApiOperation(value = "DockerRegistry 编辑页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/registry/edit/{id}")
    public String editRepoAuthPage(@PathVariable("id") DockerRegistry dockerRegistry, Model model) {
        dockerRegistry.setNull();
        model.addAttribute("dockerRegistry", dockerRegistry);
        return "/devops/build/docker/edit";
    }

    @ApiOperation(value = "DockerRegistry 详情页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/registry/detail/{id}")
    public String repoAuthDetailPage(@PathVariable("id") DockerRegistry dockerRegistry, Model model) {
        dockerRegistry.setPassword("******");
        model.addAttribute("dockerRegistry", dockerRegistry);
        return "/devops/build/docker/detail";
    }
}
