package com.bocloud.ims.booter.controller.resource;

import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.Common;
import com.bocloud.ims.entity.resource.YumRepo;
import com.bocloud.ims.interfaces.resource.YumRepoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/v1/repos/yum")
@Api(value = "软件源", tags = "软件源")
public class YumController {

    @Autowired
    private YumRepoService yumRepoService;

    @GetMapping
    @ApiOperation(value = "查询软件源")
    public BocloudResult list(@RequestParam(value = Common.NAME, required = false) String name,
                              @RequestParam(value = Common.TYPE, required = false) String type) {
        return yumRepoService.list(name, type);
    }

    @PostMapping
    @ApiOperation(value = "新增软件源")
    public BocloudResult create(@RequestBody YumRepo params,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return yumRepoService.create(params, context.getTarget());
    }

    @DeleteMapping(value = "/{name}")
    @ApiOperation(value = "删除软件源")
    public BocloudResult delete(@PathVariable(value = Common.NAME) String name,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return yumRepoService.delete(name, context.getTarget());
    }

    @PutMapping(value = "/{name}")
    @ApiOperation(value = "修改软件源")
    public BocloudResult modify(@PathVariable(value = Common.NAME) String name,
                                @RequestBody YumRepo params,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        params.setName(name);
        return yumRepoService.modify(params, context.getTarget());
    }

    @PatchMapping(value = "/{name}")
    @ApiOperation(value = "配置软件源")
    public BocloudResult configure(@PathVariable(value = Common.NAME) String name,
                                   @RequestBody YumRepo params,
                                   @Value(Common.REQ_CONTEXT) RequestContext context) {
        params.setName(name);
        return yumRepoService.configure(params, context);
    }
}
