articles.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. const express = require('express');
  2. const router = express.Router();
  3. const {Article} = require('../../models')
  4. const {Op} = require('sequelize')
  5. /*
  6. 查询文章列表
  7. GET /admin/articles
  8. */
  9. router.get('/', async function(req, res, next) {
  10. try {
  11. const query = req.query
  12. //当前是第几页,如果不传,那就是第一页
  13. const currentPage = Math.abs(Number(query.currentPage)) || 1
  14. //每页显示多少条数据,如果不传,那就显示10条
  15. const pageSize = Math.abs(Number(query.pageSize)) || 10
  16. //计算 offset
  17. const offset = (currentPage - 1) * pageSize
  18. const condition = {
  19. order:[['id','DESC']],
  20. limit:pageSize,
  21. offset
  22. }
  23. if(query.title){
  24. condition.where = {
  25. title:{
  26. [Op.like]:`%${query.title}%`
  27. }
  28. }
  29. }
  30. const {count ,rows} = await Article.findAndCountAll(condition)
  31. res.json({
  32. status:true,
  33. message:'成功',
  34. data:{
  35. articles:rows,
  36. pagination:{
  37. total:count,
  38. currentPage,
  39. pageSize
  40. }
  41. }
  42. });
  43. }catch(error){
  44. res.status(500).json({
  45. status:false,
  46. message:'失败',
  47. errors:[error.message]
  48. });
  49. }
  50. });
  51. /*
  52. 查询文章详情
  53. GET /admin/articles/:id
  54. */
  55. router.get('/:id', async function(req, res, next) {
  56. try {
  57. //获取文章id
  58. const {id} = req.params
  59. //查询文章
  60. const article = await Article.findByPk(id)
  61. if(article){
  62. res.json({
  63. status:true,
  64. message:'成功',
  65. data:article
  66. });
  67. }else{
  68. res.status(404).json({
  69. status:false,
  70. message:'文章未找到',
  71. });
  72. }
  73. }catch(error){
  74. res.status(500).json({
  75. status:false,
  76. message:'失败',
  77. errors:[error.message]
  78. });
  79. }
  80. });
  81. /*
  82. 创建文章
  83. POST /admin/articles/
  84. */
  85. router.post('/', async function(req, res, next) {
  86. try {
  87. //白名单过滤
  88. const body = filterBody(req)
  89. const article = await Article.create(body)
  90. res.status(201).json({
  91. status:true,
  92. message:'成功',
  93. data:article
  94. });
  95. }catch(error){
  96. if(error.name === 'SequelizeValidationError'){
  97. const errors = error.errors.map(e =>e.message)
  98. res.status(400).json({
  99. status:false,
  100. message:'请求参数错误',
  101. errors
  102. });
  103. }else{
  104. res.status(500).json({
  105. status:false,
  106. message:'失败',
  107. errors:[error.message]
  108. });
  109. }
  110. }
  111. });
  112. /*
  113. 删除文章
  114. GET /admin/articles/:id
  115. */
  116. router.delete('/:id', async function(req, res, next) {
  117. try {
  118. //获取文章id
  119. const {id} = req.params
  120. //查询文章
  121. const article = await Article.findByPk(id)
  122. if(article){
  123. await article.destroy()
  124. res.json({
  125. status:true,
  126. message:'成功',
  127. });
  128. }else{
  129. res.status(404).json({
  130. status:false,
  131. message:'文章未找到',
  132. });
  133. }
  134. }catch(error){
  135. res.status(500).json({
  136. status:false,
  137. message:'失败',
  138. errors:[error.message]
  139. });
  140. }
  141. });
  142. /*
  143. 更新文章
  144. GET /admin/articles/:id
  145. */
  146. router.put('/:id', async function(req, res, next) {
  147. try {
  148. //获取文章id
  149. const {id} = req.params
  150. //查询文章
  151. const article = await Article.findByPk(id)
  152. //白名单过滤
  153. const body = filterBody(req)
  154. if(article){
  155. await article.update(body)
  156. res.json({
  157. status:true,
  158. message:'成功',
  159. data:article
  160. });
  161. }else{
  162. res.status(404).json({
  163. status:false,
  164. message:'文章未找到',
  165. });
  166. }
  167. }catch(error){
  168. res.status(500).json({
  169. status:false,
  170. message:'失败',
  171. errors:[error.message]
  172. });
  173. }
  174. });
  175. function filterBody(req){
  176. return {
  177. title:req.body.title,
  178. content:req.body.content,
  179. type:req.body.type,
  180. img:req.body.img,
  181. date:req.body.date
  182. }
  183. }
  184. module.exports = router;