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

import cn.reghao.jutil.jdk.result.WebResult;
import cn.reghao.oss.console.app.service.OssStoreService;
import cn.reghao.oss.storeapi.dto.ObjectChannel;
import cn.reghao.oss.storeapi.dto.StoreNodeDto;
import cn.reghao.oss.storeapi.dto.StoreProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2024-03-01 09:02:37
 */
@Api(tags = "oss-store 调用的接口")
@RestController
@RequestMapping("/api/oss/store")
public class OssStoreController {
    private final OssStoreService ossStoreService;

    public OssStoreController(OssStoreService ossStoreService) {
        this.ossStoreService = ossStoreService;
    }

    @ApiOperation(value = "注册存储节点")
    @PostMapping(value = "/register", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addStoreNode(@RequestBody @Validated StoreNodeDto storeNodeDto) {
        ossStoreService.registerNode(storeNodeDto);
        return WebResult.success();
    }

    @ApiOperation(value = "获取某个节点上所有的 UploadChannel")
    @GetMapping(value = "/channels", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getChannels(@RequestParam("nodeAddr") String nodeAddr) {
        List<ObjectChannel> list = ossStoreService.getObjectChannels(nodeAddr);
        return WebResult.success(list);
    }

    @ApiOperation(value = "获取 store 的配置")
    @GetMapping(value = "/properties", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getStoreProperties(@RequestParam("nodeAddr") String nodeAddr) {
        StoreProperties storeProperties = ossStoreService.getStoreProperties(nodeAddr);
        return WebResult.success(storeProperties);
    }
}
