package com.hc.web.controller.browser.back;

import com.hc.business.model.Question;
import com.hc.business.service.QuestionService;
import com.hc.core.utils.result.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 习题表(Question)表控制层
 *
 * @author java
 * @since 2020-12-07 11:35:59
 */

@RestController
@RequestMapping("/new/api/back/question")
public class BackQuestionController {

    /**
     * 服务对象
     */
    private final QuestionService questionService;

    public BackQuestionController(QuestionService questionService) {
        this.questionService = questionService;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "依据主键获取 Question", response = Result.class)
    public Result selectBackQuestion(@PathVariable("id") Integer id) {
        return Result.success(this.questionService.queryById(id));
    }

    @PostMapping("/insert")
    @ApiOperation(value = "新增 Question", response = Result.class)
    public Result addBackQuestion(@Valid @RequestBody Question question) {
        return Result.success(questionService.insert(question));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改 Question", response = Result.class)
    public Result updateBackQuestion(@RequestBody Question question) {
        return Result.success(questionService.update(question));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "依据主键删除 Question", response = Result.class)
    public Result deleteBackQuestion(@PathVariable("id") Integer id) {
        return Result.success(questionService.deleteById(id));
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取 Question列表(分页)", response = Result.class)
    public Result listBackQuestion(@RequestParam(defaultValue = "0") Integer page,
                                   @RequestParam(defaultValue = "0") Integer size) {
        return Result.success(questionService.queryAllByLimit(page, size));
    }

    @GetMapping("/list/tagId")
    @ApiOperation(value = "根据tagId 获取 Question列表(分页)", response = Result.class)
    public Result listBackQuestionTag(@ApiParam(value = "标签id") @RequestParam Integer tagId,
                                      @RequestParam(defaultValue = "0") Integer page,
                                      @RequestParam(defaultValue = "0") Integer size) {
        return Result.success(questionService.queryByTagLimit(tagId, page, size));
    }

}