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

import cn.reghao.devops.web.mgr.builds.model.po.BuildDir;
import cn.reghao.devops.web.util.PageSort;
import cn.reghao.devops.web.mgr.builds.service.BuildDirService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author reghao
 * @date 2019-08-30 18:49:15
 */
@Slf4j
@Api(tags = "构建目录配置页面")
@Controller
@RequestMapping("/build/dir")
public class BuildDirPageController {
    private final BuildDirService buildDirService;

    public BuildDirPageController(BuildDirService buildDirService) {
        this.buildDirService = buildDirService;
    }

    @ApiOperation(value = "构建目录页面")
    @GetMapping
    public String buildDirPage(Model model) {
        BuildDir buildDir = buildDirService.get();
        List<BuildDir> list = List.of(buildDir);

        PageRequest pageRequest = PageSort.pageRequest();
        Page<BuildDir> page = new PageImpl<>(list, pageRequest, list.size());

        model.addAttribute("page", page);
        model.addAttribute("list", list);
        return "/devops/build/builddir";
    }

    @ApiOperation(value = "目录容量详情页面")
    @GetMapping("/usage")
    public String usagePage(Model model) {
        BuildDir buildDir = buildDirService.get();
        List<BuildDir> list = List.of(buildDir);

        PageRequest pageRequest = PageSort.pageRequest();
        Page<BuildDir> page = new PageImpl<>(list, pageRequest, list.size());

        model.addAttribute("page", page);
        model.addAttribute("list", list);
        return "/devops/build/builddir";
    }
}
