e-scooter-rental-system/server/routes/approvals.js

48 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const express = require('express');
const router = express.Router();
const Approval = require('../models/Approval');
const { authMiddleware, requireRole } = require('../middleware/auth');
// 获取所有审批(仅 admin
router.get('/', authMiddleware, requireRole('admin'), async (req, res) => {
try {
const approvals = await Approval.find();
res.json({ success: true, data: approvals });
} catch (error) {
res.status(500).json({ success: false, message: '服务器内部错误' });
}
});
// 创建审批(仅 admin
router.post('/', authMiddleware, requireRole('admin'), async (req, res) => {
try {
const approval = new Approval(req.body);
await approval.save();
res.json({ success: true, data: approval });
} catch (error) {
res.status(400).json({ success: false, message: '服务器内部错误' });
}
});
// 更新审批(仅 admin
router.put('/:id', authMiddleware, requireRole('admin'), async (req, res) => {
try {
const approval = await Approval.findByIdAndUpdate(req.params.id, req.body, { new: true });
res.json({ success: true, data: approval });
} catch (error) {
res.status(400).json({ success: false, message: '服务器内部错误' });
}
});
// 删除审批(仅 admin
router.delete('/:id', authMiddleware, requireRole('admin'), async (req, res) => {
try {
await Approval.findByIdAndDelete(req.params.id);
res.json({ success: true });
} catch (error) {
res.status(400).json({ success: false, message: '服务器内部错误' });
}
});
module.exports = router;