package com.eshore.gringotts.chain.controller;

import com.eshore.gringotts.chain.service.KVDatasourceService;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lanyuanxiaoyao
 * @date 2024-10-10
 */
@RestController
@RequestMapping("kv_datasource")
public class KVDatasourceController {
    private static final Logger logger = LoggerFactory.getLogger(KVDatasourceController.class);

    private final KVDatasourceService kvDatasourceService;

    public KVDatasourceController(KVDatasourceService kvDatasourceService) {
        this.kvDatasourceService = kvDatasourceService;
    }

    @GetMapping("create")
    public String createDatasource() throws ContractException {
        return kvDatasourceService.createDatasource();
    }

    @PostMapping("set")
    public void setValue(@RequestParam("address") String address, @RequestParam("key") String key, @RequestBody String value) {
        kvDatasourceService.set(address, key, value);
    }

    @GetMapping("get")
    public String getValue(@RequestParam("address") String address, @RequestParam("key") String key) throws ContractException {
        return kvDatasourceService.get(address, key);
    }
}
