文章相关API

master
liuyx 2 years ago
parent 2174464e3f
commit 84d2b44504

@ -2,11 +2,25 @@
import mongoose from 'mongoose'
const ArticleSchema = new mongoose.Schema({
category: String,
cover: {
type: String
},
title: String,
content: String,
cover: String,
category: String,
tagList: [],
title: String,
views: {
type: Number,
default: 0
},
createTime: {
type: Date,
default: Date.now()
},
updateTime: {
type: Date,
default: Date.now()
}
})
export default mongoose.model('Article', ArticleSchema)
export default mongoose.model('Article', ArticleSchema, 'article')

@ -1,26 +1,64 @@
// const articleRouter = require('express').Router()
import express from 'express'
import Article from '../models/article.js'
import result from '../utils/result.js'
const articleRouter = express.Router()
articleRouter.get('/articles', function(req, res, next) {
Article.find((err, data) => {
const {...copy} = data
res.status(200).json(copy)
})
// 获取所有文章
articleRouter.get('/', async (req, res) => {
console.log(req.query)
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) => {
const article = {
id: req.params.id,
cover: 'https://typora-lyx.oss-cn-guangzhou.aliyuncs.com/typora/wallhaven-6dqemx.jpg',
title: '测试',
content: '## 这是一篇测试文章\noh yes~',
tagList: ['Web', 'Vue3', 'Pinia'],
category: '学习笔记'
Article.findById(req.params.id, (err, doc) => {
if (err) {
res.status(500).json(result(500, '查询失败啦', doc))
} else {
res.status(200).json(result(200, '查询成功', doc))
}
})
})
// 更新或发布文章
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

Loading…
Cancel
Save