first commit
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { Router } from 'express';
|
||||
import db from '../db.js';
|
||||
import { auth } from '../middleware.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
// 家政服务列表
|
||||
router.get('/', auth, (req, res) => {
|
||||
const items = db
|
||||
.prepare('SELECT h.*, u.nickname, u.avatar FROM housekeeping h JOIN users u ON h.user_id = u.id ORDER BY h.created_at DESC')
|
||||
.all();
|
||||
res.json(items);
|
||||
});
|
||||
|
||||
// 发布家政服务
|
||||
router.post('/', auth, (req, res) => {
|
||||
const { title, description, price, phone } = req.body;
|
||||
if (!title || !description) {
|
||||
return res.status(400).json({ message: '请填写服务名称和描述' });
|
||||
}
|
||||
const info = db
|
||||
.prepare('INSERT INTO housekeeping (user_id, title, description, price, phone) VALUES (?, ?, ?, ?, ?)')
|
||||
.run(req.user.id, title, description, price || '', phone || '');
|
||||
res.json(db.prepare('SELECT * FROM housekeeping WHERE id = ?').get(info.lastInsertRowid));
|
||||
});
|
||||
|
||||
// 删除服务
|
||||
router.delete('/:id', auth, (req, res) => {
|
||||
const item = db.prepare('SELECT * FROM housekeeping WHERE id = ?').get(req.params.id);
|
||||
if (!item) return res.status(404).json({ message: '服务不存在' });
|
||||
if (item.user_id !== req.user.id && req.user.role !== 'admin') {
|
||||
return res.status(403).json({ message: '无权删除' });
|
||||
}
|
||||
db.prepare('DELETE FROM housekeeping WHERE id = ?').run(req.params.id);
|
||||
res.json({ message: '删除成功' });
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user