package cn.com.qmth.examcloud.core.questions.api.controller;


import cn.com.qmth.examcloud.core.questions.dao.SettingRepo;
import cn.com.qmth.examcloud.core.questions.service.bean.dto.SettingDto;
import com.google.gson.Gson;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * Created by songyue on 16/12/26.
 */

@RestController
@RequestMapping("${api_cqb}/")
public class SettingController {

    @Autowired
    Gson gson;

    @Autowired
    SettingRepo settingRepo;

    /**
     * 获取全部设置
     *
     * @return
     */
    @ApiOperation(value = "获取全部设置", notes = "获取全部设置")
    @GetMapping(value = "/setting")
    public ResponseEntity<Object> getAllSetting() {
        return new ResponseEntity<>(settingRepo.findAll(), HttpStatus.OK);
    }

    /**
     * 更新设置
     *
     * @param settingDto
     * @return
     */
    @ApiOperation(value = "更新设置", notes = "更新设置")
    @PutMapping(value = "/setting")
    public ResponseEntity<Object> updateSetting(@RequestBody SettingDto settingDto) {
        return new ResponseEntity<>(settingRepo.saveAll(settingDto.getSettingList()), HttpStatus.OK);
    }

    /**
     * 新增设置
     *
     * @param settingDto
     * @return
     */
    @ApiOperation(value = "新增设置", notes = "新增设置")
    @PostMapping(value = "/setting")
    public ResponseEntity<Object> addSetting(@RequestBody SettingDto settingDto) {
        return new ResponseEntity<>(settingRepo.saveAll(settingDto.getSettingList()), HttpStatus.OK);
    }

    /**
     * 删除设置
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除设置", notes = "删除设置")
    @DeleteMapping(value = "/setting/{id}")
    public ResponseEntity<Object> removeSetting(@PathVariable String id) {
        settingRepo.deleteById(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
