File size: 2,155 Bytes
11f4e50 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | import { Router, Response } from 'express';
import { Project } from '../models';
import { authMiddleware, AuthRequest } from '../middleware/auth';
const router = Router();
router.use(authMiddleware);
// POST /api/projects
router.post('/', async (req: AuthRequest, res: Response) => {
try {
const { name, defaultPlatform, defaultFormat } = req.body;
const project = new Project({
userId: req.userId,
name,
defaultPlatform,
defaultFormat,
});
await project.save();
res.status(201).json(project);
} catch (error) {
res.status(500).json({ error: 'Failed to create project.' });
}
});
// GET /api/projects
router.get('/', async (req: AuthRequest, res: Response) => {
try {
const projects = await Project.find({ userId: req.userId })
.populate('videos')
.sort({ createdAt: -1 });
res.json(projects);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch projects.' });
}
});
// GET /api/projects/:id
router.get('/:id', async (req: AuthRequest, res: Response) => {
try {
const project = await Project.findOne({
_id: req.params.id,
userId: req.userId,
}).populate('videos');
if (!project) {
res.status(404).json({ error: 'Project not found.' });
return;
}
res.json(project);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch project.' });
}
});
// DELETE /api/projects/:id
router.delete('/:id', async (req: AuthRequest, res: Response) => {
try {
const project = await Project.findOneAndDelete({
_id: req.params.id,
userId: req.userId,
});
if (!project) {
res.status(404).json({ error: 'Project not found.' });
return;
}
res.json({ message: 'Project deleted.' });
} catch (error) {
res.status(500).json({ error: 'Failed to delete project.' });
}
});
export default router;
|