package com.hc.gpweb.controller.back;

import com.hc.gpbusiness.model.WebNews;
import com.hc.gpbusiness.service.WebNewsService;
import com.hc.gpcore.utils.result.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 新闻表
 * (News)表控制层
 *
 * @author java
 * @since 2020-07-29 16:23:10
 */

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

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

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

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

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

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

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

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


}