package cn.reghao.devops.mgr.app.controller;

import cn.reghao.devops.mgr.app.model.dto.DeployConfigDto;
import cn.reghao.devops.mgr.app.service.AppDeployService;
import cn.reghao.jutil.jdk.result.Result;
import cn.reghao.jutil.jdk.result.WebResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author reghao
 * @date 2019-11-27 11:29:43
 */
@Slf4j
@Api(tags = "应用部署配置 CRUD 接口")
@RestController
@RequestMapping("/api/app/config/app/deploy")
public class AppDeployConfigController {
    private final AppDeployService appDeployService;

    public AppDeployConfigController(AppDeployService appDeployService) {
        this.appDeployService = appDeployService;
    }

    @ApiOperation(value = "添加应用部署配置", notes = "N")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public String add(@Validated DeployConfigDto deployConfigDto) {
        Result result = appDeployService.addDeployConfig(deployConfigDto);
        return WebResult.result(result);
    }

    @ApiOperation(value = "修改应用部署配置", notes = "N")
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public String update(@Validated DeployConfigDto deployConfigDto) {
        Result result = appDeployService.updateStartScript(deployConfigDto);
        return WebResult.result(result);
    }

    @ApiOperation(value = "删除应用部署配置", notes = "N")
    @DeleteMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String delete(@PathVariable("id") Integer appDeployConfigId) {
        Result result = appDeployService.deleteDeployConfig(appDeployConfigId);
        return WebResult.result(result);
    }
}
