parent
2174464e3f
commit
84d2b44504
@ -1,27 +1,65 @@
|
|||||||
// const articleRouter = require('express').Router()
|
|
||||||
import express from 'express'
|
import express from 'express'
|
||||||
import Article from '../models/article.js'
|
import Article from '../models/article.js'
|
||||||
|
import result from '../utils/result.js'
|
||||||
|
|
||||||
const articleRouter = express.Router()
|
const articleRouter = express.Router()
|
||||||
|
|
||||||
articleRouter.get('/articles', function(req, res, next) {
|
// 获取所有文章
|
||||||
Article.find((err, data) => {
|
articleRouter.get('/', async (req, res) => {
|
||||||
const {...copy} = data
|
console.log(req.query)
|
||||||
res.status(200).json(copy)
|
const { currentPage, pageSize } = req.query
|
||||||
})
|
const articleList = await Article.find({})
|
||||||
|
.skip((currentPage - 1) * pageSize)
|
||||||
|
.limit(pageSize)
|
||||||
|
const count = await Article.countDocuments({})
|
||||||
|
console.log(count)
|
||||||
|
res.status(200).json(result(200, '查询成功', { articleList, count }))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 根据id获取文章信息
|
||||||
articleRouter.get('/:id', (req, res) => {
|
articleRouter.get('/:id', (req, res) => {
|
||||||
const article = {
|
Article.findById(req.params.id, (err, doc) => {
|
||||||
id: req.params.id,
|
if (err) {
|
||||||
cover: 'https://typora-lyx.oss-cn-guangzhou.aliyuncs.com/typora/wallhaven-6dqemx.jpg',
|
res.status(500).json(result(500, '查询失败啦', doc))
|
||||||
title: '测试',
|
} else {
|
||||||
content: '## 这是一篇测试文章\noh yes~',
|
res.status(200).json(result(200, '查询成功', doc))
|
||||||
tagList: ['Web', 'Vue3', 'Pinia'],
|
}
|
||||||
category: '学习笔记'
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// 更新或发布文章
|
||||||
|
articleRouter.post('/', async (req, res) => {
|
||||||
|
const data = req.body._value
|
||||||
|
if (data._id === '') {
|
||||||
|
// 发布
|
||||||
|
const article = new Article({
|
||||||
|
cover: data.cover,
|
||||||
|
title: data.title,
|
||||||
|
content: data.content,
|
||||||
|
tagList: data.tagList,
|
||||||
|
category: data.category
|
||||||
|
})
|
||||||
|
await article.save()
|
||||||
|
res.status(200).json(result(200, '发布成功', null))
|
||||||
|
} else {
|
||||||
|
// 更新
|
||||||
|
await Article.updateOne({ _id: data._id }, data)
|
||||||
|
res.status(200).json(result(200, '更新成功', null))
|
||||||
}
|
}
|
||||||
res.status(200).json(article)
|
})
|
||||||
|
|
||||||
|
// 根据id数组批量删除
|
||||||
|
articleRouter.delete('/', async (req, res) => {
|
||||||
|
const idArray = req.body
|
||||||
|
await Article.remove({ _id: { $in: idArray } })
|
||||||
|
res.status(200).json(result(200, '批量删除成功', null))
|
||||||
|
})
|
||||||
|
|
||||||
|
// 根据id删除
|
||||||
|
articleRouter.delete('/:id', async (req, res) => {
|
||||||
|
await Article.deleteOne({ _id: req.params.id })
|
||||||
|
res.status(200).json(result(200, '删除成功', null))
|
||||||
})
|
})
|
||||||
|
|
||||||
// module.exports = articleRouter
|
// module.exports = articleRouter
|
||||||
export default articleRouter
|
export default articleRouter
|
||||||
|
Loading…
Reference in new issue