package com.ruoyi.web.controller.homepage;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.News;
import com.ruoyi.system.service.INewsService;

@RestController
@RequestMapping("/homepage/news")
public class NewsController extends BaseController
{
    @Autowired
    private INewsService newsService;

    @PreAuthorize("@ss.hasPermi('homepage:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(News news)
    {
        startPage();
        List<News> list = newsService.selectNewsList(news);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('homepage:news:query')")
    @GetMapping(value = "/{newsId}")
    public AjaxResult getInfo(@PathVariable Long newsId)
    {
        return success(newsService.selectNewsById(newsId));
    }

    @PreAuthorize("@ss.hasPermi('homepage:news:add')")
    @Log(title = "新闻动态", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody News news)
    {
        news.setCreateBy(getUsername());
        return toAjax(newsService.insertNews(news));
    }

    @PreAuthorize("@ss.hasPermi('homepage:news:edit')")
    @Log(title = "新闻动态", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody News news)
    {
        news.setUpdateBy(getUsername());
        return toAjax(newsService.updateNews(news));
    }

    @PreAuthorize("@ss.hasPermi('homepage:news:remove')")
    @Log(title = "新闻动态", businessType = BusinessType.DELETE)
    @DeleteMapping("/{newsIds}")
    public AjaxResult remove(@PathVariable Long[] newsIds)
    {
        return toAjax(newsService.deleteNewsByIds(newsIds));
    }
}
