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

import com.hc.business.model.ZccmNews;
import com.hc.business.service.ZccmNewsService;
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;


/**
 * 新闻表
 * (ZccmNews)表控制层
 *
 * @author java
 * @since 2020-11-20 11:48:39
 */

@RestController
@RequestMapping("/new/api/back/news")
public class BackNewsController {

    /**
     * 服务对象
     */
    private final ZccmNewsService newsService;

    public BackNewsController(ZccmNewsService newsService) {
        this.newsService = newsService;
    }

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

    @PostMapping("/insert")
    @ApiOperation(value = "新增 ZccmNews", response = Result.class)
    public Result addBackNews(@Valid @RequestBody ZccmNews news) {
        return Result.success(newsService.insert(news));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改 ZccmNews", response = Result.class)
    public Result updateBackNews(@RequestBody ZccmNews news) {
        return Result.success(newsService.update(news));
    }

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

    @GetMapping("/tag/{tagId}")
    @ApiOperation(value = "依据tagId获取 ZccmNews", response = Result.class)
    public Result selectBackByTagId(@ApiParam(value = "标签id") @PathVariable("tagId") Integer tagId,
                                    @RequestParam(defaultValue = "0") Integer page,
                                    @RequestParam(defaultValue = "0") Integer size) {
        return Result.success(this.newsService.queryListByTagId(tagId, page, size));
    }

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


}