feat: add backend server
This commit is contained in:
65
server/controller/standard.js
Normal file
65
server/controller/standard.js
Normal file
@@ -0,0 +1,65 @@
|
||||
import express from 'express'
|
||||
import Standard from '../model/standard.js'
|
||||
import { requireAuth, requireAdmin } from './user.js'
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
// 获取对照品
|
||||
router.get('/', requireAuth, async (req, res) => {
|
||||
try {
|
||||
const standard = await Standard.find()
|
||||
res.json(standard)
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: '服务器错误', error: error.message })
|
||||
}
|
||||
})
|
||||
|
||||
// 创建对照品
|
||||
router.post('/', requireAuth, async (req, res) => {
|
||||
if (!req.body.batch) {
|
||||
return res.status(400).json({ message: '缺少必要字段' })
|
||||
}
|
||||
|
||||
try {
|
||||
const standard = await Standard.create({ ...req.body })
|
||||
if (standard) {
|
||||
res.json(standard)
|
||||
} else {
|
||||
res.status(400).json({ message: '资源创建失败' })
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: '服务器错误', error: error.message })
|
||||
}
|
||||
})
|
||||
|
||||
// 删除对照品
|
||||
router.delete('/:id', requireAuth, requireAdmin, async (req, res) => {
|
||||
try {
|
||||
const standard = await Standard.findByIdAndDelete(req.params.id)
|
||||
if (standard) {
|
||||
res.json({ message: '删除成功' })
|
||||
} else {
|
||||
res.status(404).json({ message: '删除成功' })
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: '服务器错误', error: error.message })
|
||||
}
|
||||
})
|
||||
|
||||
// 更新对照品
|
||||
router.patch('/:id', requireAuth, async (req, res) => {
|
||||
try {
|
||||
console.log(req.params.id, req.body)
|
||||
const standard = await Standard.findByIdAndUpdate(req.params.id, req.body, {
|
||||
returnDocument: 'after',
|
||||
})
|
||||
res.json(standard)
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
message: '服务器错误',
|
||||
error: error.message,
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
export { router as standardRouter }
|
||||
Reference in New Issue
Block a user