package cn.reghao.oss.web.app.controller.page;

import cn.reghao.oss.web.app.db.repository.UploadChannelRepository;
import cn.reghao.oss.web.app.model.po.UploadChannel;
import cn.reghao.oss.web.app.service.StoreNodeService;
import cn.reghao.oss.web.util.DefaultSetting;
import cn.reghao.oss.web.util.db.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.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author reghao
 * @date 2024-02-27 09:12:14
 */
@Api(tags = "存储对象页面")
@Controller
@RequestMapping("/store/object")
public class StoreObjectPageController {
    private final UploadChannelRepository uploadChannelRepository;
    private final StoreNodeService storeNodeService;

    public StoreObjectPageController(UploadChannelRepository uploadChannelRepository, StoreNodeService storeNodeService) {
        this.uploadChannelRepository = uploadChannelRepository;
        this.storeNodeService = storeNodeService;
    }

    @ApiOperation(value = "文件列表页面")
    @GetMapping(value = "/list")
    public String index(@RequestParam(value = "env", required = false) String env,
                        @RequestParam(value = "type", required = false) String type,
                        @RequestParam(value = "appName", required = false) String appName, Model model) throws Exception {
        if (env == null) {
            env = DefaultSetting.getDefaultAppEnv();
        }

        if (type == null) {
            type = DefaultSetting.getDefaultAppType();
        }

        PageRequest pageRequest = PageSort.pageRequest();
        Page<UploadChannel> page = uploadChannelRepository.findAll(pageRequest);

        model.addAttribute("env", env);
        model.addAttribute("type", type);
        model.addAttribute("page", page);
        model.addAttribute("list", page.getContent());
        return "/object/index";
    }
}
