news.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const express = require('express');
  2. const router = express.Router();
  3. const { News } = require('../models');
  4. // 获取所有新闻列表
  5. router.get('/', async (req, res) => {
  6. try {
  7. const { page = 1, limit = 10, category, isPublished } = req.query;
  8. const offset = (page - 1) * limit;
  9. const where = {};
  10. if (category) {
  11. where.category = category;
  12. }
  13. if (isPublished !== undefined) {
  14. where.isPublished = isPublished === 'true';
  15. } else {
  16. // 默认只显示已发布的新闻
  17. where.isPublished = true;
  18. }
  19. const news = await News.findAndCountAll({
  20. where,
  21. order: [['publishedAt', 'DESC'], ['createdAt', 'DESC']],
  22. limit: parseInt(limit),
  23. offset: parseInt(offset)
  24. });
  25. res.json({
  26. data: news.rows,
  27. total: news.count,
  28. page: parseInt(page),
  29. limit: parseInt(limit),
  30. totalPages: Math.ceil(news.count / limit)
  31. });
  32. } catch (error) {
  33. res.status(500).json({ error: error.message });
  34. }
  35. });
  36. // 根据ID获取单条新闻
  37. router.get('/:id', async (req, res) => {
  38. try {
  39. const news = await News.findByPk(req.params.id);
  40. if (!news) {
  41. return res.status(404).json({ error: '新闻不存在' });
  42. }
  43. // 增加浏览次数
  44. await news.increment('viewCount');
  45. res.json(news);
  46. } catch (error) {
  47. res.status(500).json({ error: error.message });
  48. }
  49. });
  50. // 添加新新闻
  51. router.post('/', async (req, res) => {
  52. try {
  53. const newsData = {
  54. ...req.body,
  55. publishedAt: req.body.publishedAt || new Date()
  56. };
  57. const news = await News.create(newsData);
  58. res.status(201).json(news);
  59. } catch (error) {
  60. res.status(500).json({ error: error.message });
  61. }
  62. });
  63. // 更新新闻
  64. router.put('/:id', async (req, res) => {
  65. try {
  66. const news = await News.findByPk(req.params.id);
  67. if (!news) {
  68. return res.status(404).json({ error: '新闻不存在' });
  69. }
  70. await news.update(req.body);
  71. res.json(news);
  72. } catch (error) {
  73. res.status(500).json({ error: error.message });
  74. }
  75. });
  76. // 删除新闻
  77. router.delete('/:id', async (req, res) => {
  78. try {
  79. const news = await News.findByPk(req.params.id);
  80. if (!news) {
  81. return res.status(404).json({ error: '新闻不存在' });
  82. }
  83. await news.destroy();
  84. res.status(204).send();
  85. } catch (error) {
  86. res.status(500).json({ error: error.message });
  87. }
  88. });
  89. // 获取新闻分类列表
  90. router.get('/categories/list', async (req, res) => {
  91. try {
  92. const categories = await News.findAll({
  93. attributes: ['category'],
  94. where: {
  95. category: {
  96. [require('sequelize').Op.ne]: null
  97. }
  98. },
  99. group: ['category'],
  100. raw: true
  101. });
  102. const categoryList = categories
  103. .map(item => item.category)
  104. .filter(Boolean)
  105. .map((category, index) => ({
  106. id: index + 1,
  107. name: category
  108. }));
  109. res.json(categoryList);
  110. } catch (error) {
  111. res.status(500).json({ error: error.message });
  112. }
  113. });
  114. module.exports = router;