package cn.reghao.oss.console.app.controller;

import cn.reghao.jutil.jdk.result.WebResult;
import cn.reghao.oss.console.app.service.UploadChannelService;
import cn.reghao.oss.console.util.AuthKeyContext;
import cn.reghao.oss.storeapi.dto.ObjectChannel;
import cn.reghao.oss.storeapi.dto.ServerInfo;
import cn.reghao.oss.console.app.rpc.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author reghao
 * @date 2024-02-23 09:27:36
 */
@Api(tags = "OSS 接口")
@RestController
@RequestMapping("/api/oss/server")
public class OssServerController {
    private final OssService ossService;
    private final UploadChannelService uploadChannelService;

    public OssServerController(OssService ossService, UploadChannelService uploadChannelService) {
        this.ossService = ossService;
        this.uploadChannelService = uploadChannelService;
    }

    @ApiOperation(value = "获取 oss-store 节点")
    @GetMapping(value = "/info", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getServerInfo(@RequestParam("channelId") int channelId) throws Exception {
        ServerInfo serverInfo = ossService.getServerInfo(channelId);
        return WebResult.success(serverInfo);
    }

    @ApiOperation(value = "根据 channel_id 获取 UploadChannel")
    @GetMapping(value = "/channel", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getChannel(@RequestParam("channelId") int channelId) {
        int loginUser = AuthKeyContext.getUser();
        ObjectChannel objectChannel = uploadChannelService.getObjectChannelByChannelId(channelId, loginUser);
        return WebResult.success(objectChannel);
    }
}
