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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.ims.entity.resource.Bucket;
import com.bocloud.ims.interfaces.resource.BucketService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/v1/buckets")
@Api(value = "存储桶管理", tags = "存储桶管理")
public class BucketController {

	@Autowired
	private BucketService bucketService;

	/**
	 * 列表
	 * 
	 * @param pager
	 * @param context
	 * @return
	 */
	@GetMapping
	@ApiOperation(value = "存储桶列表", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return bucketService.list(pager.getPage(), pager.getRows(), pager.getParams(), pager.getSorter(),
				pager.getSimple(), context);
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = { RequestMethod.DELETE })
	@ApiOperation(value = "存储桶删除", response = BocloudResult.class, httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return bucketService.remove(id, context);
	}

	/**
	 * 详情
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = { RequestMethod.GET })
	@ApiOperation(value = "存储桶详情", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult detail(@PathVariable(value = Common.ID) Long id) {
		return bucketService.detail(id);
	}

	/**
	 * 创建
	 * 
	 * @param params
	 * @param user
	 * @return
	 */
	@PostMapping
	@ApiOperation(value = "存储桶新增", response = BocloudResult.class, httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult create(@RequestBody Bucket bucket, @Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != bucket) {
			return bucketService.create(bucket, context);
		}
		return ResultTools.formatErrResult();
	}

	/**
	 * 查询存储空间是否存在
	 * 
	 * @param params
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/exists", method = { RequestMethod.GET })
	@ApiOperation(value = "查询存储空间是否存在", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult exists(@RequestParam String name, @RequestParam Long vendorId, @RequestParam String regionId,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return bucketService.exists(name, vendorId, regionId, context);
	}

}
