package com.persagy.dmp.rwd.version.controller;

import cn.hutool.core.util.StrUtil;
import com.persagy.dmp.common.constant.ResponseCode;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.common.utils.ResultHelper;
import com.persagy.dmp.rwd.version.entity.VersionDefine;
import com.persagy.dmp.rwd.version.service.IVersionDefineService;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

/**
 * 版本定义Controller
 * @author Charlie Yu
 * @date 2021-06-23
 */
@RestController
@RequestMapping("/rwdedit/version")
public class VersionDefineController {

    @Autowired
    private IVersionDefineService service;

    @PostMapping("/create")
    public CommonResult<VersionDefine> create(@RequestBody VersionDefine define) {
        if(StrUtil.isBlank(define.getVersion())) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        define = service.insertVersion(define);
        return ResultHelper.single(define);
    }

    @PostMapping("/update")
    public CommonResult<VersionDefine> update(@RequestBody VersionDefine define) {
        if(StrUtil.isBlank(define.getVersion())) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        define = service.updateVersion(define);
        return ResultHelper.single(define);
    }

    @PostMapping("/delete")
    public CommonResult delete(@RequestBody String id) {
        service.deleteVersion(id);
        return ResultHelper.success();
    }
}
