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

48 lines
1.6 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 Application = require('../models/Application');
const { authMiddleware, requireRole } = require('../middleware/auth');
// 获取所有申请admin 或 store
router.get('/', authMiddleware, requireRole('admin', 'store'), async (req, res) => {
try {
const apps = await Application.find().populate('store');
res.json({ success: true, data: apps });
} catch (error) {
res.status(500).json({ success: false, message: '服务器内部错误' });
}
});
// 创建申请admin 或 store
router.post('/', authMiddleware, requireRole('admin', 'store'), async (req, res) => {
try {
const app = new Application(req.body);
await app.save();
res.json({ success: true, data: app });
} catch (error) {
res.status(400).json({ success: false, message: '服务器内部错误' });
}
});
// 更新申请admin 或 store
router.put('/:id', authMiddleware, requireRole('admin', 'store'), async (req, res) => {
try {
const app = await Application.findByIdAndUpdate(req.params.id, req.body, { new: true });
res.json({ success: true, data: app });
} catch (error) {
res.status(400).json({ success: false, message: '服务器内部错误' });
}
});
// 删除申请admin 或 store
router.delete('/:id', authMiddleware, requireRole('admin', 'store'), async (req, res) => {
try {
await Application.findByIdAndDelete(req.params.id);
res.json({ success: true });
} catch (error) {
res.status(400).json({ success: false, message: '服务器内部错误' });
}
});
module.exports = router;