| import { Router, Response } from 'express';
|
| import { Project } from '../models';
|
| import { authMiddleware, AuthRequest } from '../middleware/auth';
|
|
|
| const router = Router();
|
|
|
| router.use(authMiddleware);
|
|
|
|
|
| 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.' });
|
| }
|
| });
|
|
|
|
|
| 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.' });
|
| }
|
| });
|
|
|
|
|
| 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.' });
|
| }
|
| });
|
|
|
|
|
| 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;
|
|
|