package cn.teammodel.controller.frontend;

import cn.teammodel.common.IdRequest;
import cn.teammodel.common.R;
import cn.teammodel.model.dto.news.CreateNewsDto;
import cn.teammodel.model.dto.news.UpdateNewsDto;
import cn.teammodel.model.entity.news.News;
import cn.teammodel.service.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author winter
 * @create 2024-02-26 17:04
 */
@RestController
@RequestMapping("/news")
@Api(tags = "新闻设置")
public class NewsController {
    @Resource
    private NewsService newsService;

    @GetMapping("list/{periodId}")
    @ApiOperation("查询新闻")
    public R<List<News>> listNews(@PathVariable String periodId) {
        List<News> Newss = newsService.listNews(periodId);
        return R.success(Newss);
    }

    @GetMapping("get/{newsId}")
    @ApiOperation("根据id查询新闻")
    public R<News> getNewsById(@PathVariable String newsId) {
        News news = newsService.getNewsById(newsId);
        return R.success(news);
    }


    @PostMapping("create")
    @ApiOperation("创建新闻")
    public R<News> createNews(@RequestBody @Valid CreateNewsDto createNewsDto) {
        News News = newsService.createNews(createNewsDto);
        return R.success(News);
    }

    @PostMapping("update")
    @ApiOperation("更新新闻")
    public R<News> updateNews(@RequestBody @Valid UpdateNewsDto updateNewsDto) {
        News News = newsService.updateNews(updateNewsDto);
        return R.success(News);
    }

    @PostMapping("remove")
    @ApiOperation("删除新闻")
    public R<String> deleteNews(@RequestBody @Valid IdRequest idRequest) {
        newsService.deleteNews(idRequest);
        return R.success("删除新闻成功");
    }
}
