Files
timelapse-backend/routes/projectRoutes.js

83 lines
2.8 KiB
JavaScript

const express = require('express');
const router = express.Router();
const projectManager = require('../src/project/projectManager');
const serverError = require('../utils/serverError');
router.get('/projects', async (req, res) => {
try {
const projects = await projectManager.getAllProjects();
res.json(projects);
} catch (error) {
serverError.sendError('Error getting all projects:', res, error, 500);
}
});
router.get('/projects/:id', async (req, res) => {
const projectId = req.params.id;
if (!projectId || isNaN(projectId)) {
return res.status(400).json({ error: 'Invalid project ID' });
}
try {
const project = await projectManager.getProjectById(projectId);
res.json(project);
} catch (error) {
serverError.sendError('Error getting project by ID:', res, error, 500);
}
});
router.get('/projects/:id/videos', async (req, res) => {
const projectId = req.params.id;
if (!projectId || isNaN(projectId)) {
return res.status(400).json({ error: 'Invalid project ID' });
}
try {
const videos = await projectManager.getVideosByProjectId(projectId);
res.json(videos);
} catch (error) {
serverError.sendError('Error getting videos by project ID:', res, error, 500);
}
});
router.get('/projects/:id/measurements', async (req, res) => {
const projectId = req.params.id;
if (!projectId || isNaN(projectId)) {
return res.status(400).json({ error: 'Invalid project ID' });
}
try {
const measurements = await projectManager.getMeasurementsByProjectId(projectId);
res.json(measurements);
} catch (error) {
serverError.sendError('Error getting measurements by project ID:', res, error, 500);
}
});
router.post('/projects', async (req, res) => {
const { name, description } = req.body;
if (!name || !description) {
return res.status(400).json({ error: 'Name and description are required' });
}
try {
const project = await projectManager.createProject(name, description, new Date(), 0);
projectManager.createProjectDirectory(project.id);
res.status(201).json({ message: 'Project added successfully', id: project.id });
} catch (error) {
serverError.sendError('Error creating project:', res, error, 500);
}
});
router.delete('/projects/:id', async (req, res) => {
const projectId = req.params.id;
if (!projectId || isNaN(projectId)) {
return res.status(400).json({ error: 'Invalid project ID' });
}
try {
projectManager.deleteProjectDirectory(projectId);
projectManager.deleteProjectById(projectId);
res.status(200).json({ message: 'Project deleted successfully', id: projectId });
} catch (error) {
serverError.sendError('Error deleting project:', res, error, 500);
}
});
module.exports = router;