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

import cn.reghao.oss.web.app.model.po.UserKey;
import cn.reghao.oss.web.app.model.vo.KeyValue;
import cn.reghao.oss.web.app.service.StoreNodeService;
import cn.reghao.oss.web.app.service.UserKeyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2024-02-27 10:24:31
 */
@Api(tags = "我的帐号页面")
@Controller
@RequestMapping("/store/my/key")
public class UserKeyPageController {
    private final StoreNodeService storeNodeService;
    private final UserKeyService userKeyService;

    public UserKeyPageController(StoreNodeService storeNodeService, UserKeyService userKeyService) {
        this.storeNodeService = storeNodeService;
        this.userKeyService = userKeyService;
    }

    @ApiOperation(value = "我的帐号页面")
    @GetMapping(value = "")
    public String userKeyPage(Model model) {
        List<UserKey> list = userKeyService.getUserKeys();
        model.addAttribute("list", list);
        return "/userkey/index1";
    }

    @ApiOperation(value = "帐号创建页面")
    @GetMapping(value = "/add")
    public String userKeyAddPage(Model model) {
        List<KeyValue> storeNodes = storeNodeService.getStoreNodes().stream()
                .map(storeNode -> {
                    String domain = storeNode.getDomain();
                    return new KeyValue(domain, domain);
                }).collect(Collectors.toList());
        model.addAttribute("storeNodes", storeNodes);
        return "/userkey/add";
    }
}
