diff --git a/test/dbTester.js b/test/dbTester.js index 4c37eaa..7dbaeb1 100644 --- a/test/dbTester.js +++ b/test/dbTester.js @@ -7,6 +7,14 @@ async function printProjects() { console.log('Projects:', projects); } +async function testCreateDestroyProject() { + const project = await databaseUtils.createProject('Test project', 'Test description', new Date(), 0); + console.log('Project created:', project); + + await databaseUtils.deleteProject(project.id); + console.log('Project deleted'); +} + try { printProjects(); } catch (error) { diff --git a/utils/dbUtils.js b/utils/dbUtils.js index 68227be..0abf82f 100644 --- a/utils/dbUtils.js +++ b/utils/dbUtils.js @@ -1,71 +1,85 @@ const db = require('../db'); // Assurez-vous que le chemin est correct const projectsTable = { - - addProject: async (name, description) => { - const query = ` - INSERT INTO projects (name, description) - VALUES ($1, $2) - RETURNING *; - `; - try { - const result = await db.query(query, [name, description]); - return result.rows[0]; - } catch (error) { - console.error('Error adding project:', error); - } + create: async (name, description, startDate, status) => { + const query = 'INSERT INTO public.projects (name, description, start_date, status) VALUES ($1, $2, $3, $4) RETURNING *'; + const values = [name, description, startDate, status]; + const res = await db.query(query, values); + return res.rows[0]; }, - - getProjects: async () => { - const query = ` - SELECT * FROM projects; - `; - try { - const result = await db.query(query); - return result.rows; - } catch (error) { - console.error('Error fetching projects:', error); - } + update: async (id, name, description, startDate, status) => { + const query = 'UPDATE public.projects SET name = $1, description = $2, start_date = $3, status = $4 WHERE id = $5 RETURNING *'; + const values = [name, description, startDate, status, id]; + const res = await db.query(query, values); + return res.rows[0]; }, - - getProjectById: async (id) => { - const query = ` - SELECT * FROM projects WHERE id = $1; - `; - try { - const result = await db.query(query, [id]); - return result.rows[0]; - } catch (error) { - console.error('Error fetching project by id:', error); - } + delete: async (id) => { + const query = 'DELETE FROM public.projects WHERE id = $1'; + const values = [id]; + await db.query(query, values); }, - - updateProject: async (id, name, description) => { - const query = ` - UPDATE projects - SET name = $1, description = $2 - WHERE id = $3 - RETURNING *; - `; - try { - const result = await db.query(query, [name, description, id]); - return result.rows[0]; - } catch (error) { - console.error('Error updating project:', error); - } - }, - - deleteProject: async (id) => { - const query = ` - DELETE FROM projects WHERE id = $1; - `; - try { - await db.query(query, [id]); - console.log('Project deleted successfully'); - } catch (error) { - console.error('Error deleting project:', error); - } + getById: async (id) => { + const query = 'SELECT * FROM public.projects WHERE id = $1'; + const values = [id]; + const res = await db.query(query, values); + return res.rows[0]; } }; -module.exports = projectsTable; \ No newline at end of file +const measurementsTable = { + create: async (projectId, timestamp, imagePath, temperature, humidity, completed) => { + const query = 'INSERT INTO public.measurements (project_id, timestamp, image_path, temperature, humidity, completed) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *'; + const values = [projectId, timestamp, imagePath, temperature, humidity, completed]; + const res = await db.query(query, values); + return res.rows[0]; + }, + update: async (id, projectId, timestamp, imagePath, temperature, humidity, completed) => { + const query = 'UPDATE public.measurements SET project_id = $1, timestamp = $2, image_path = $3, temperature = $4, humidity = $5, completed = $6 WHERE id = $7 RETURNING *'; + const values = [projectId, timestamp, imagePath, temperature, humidity, completed, id]; + const res = await db.query(query, values); + return res.rows[0]; + }, + delete: async (id) => { + const query = 'DELETE FROM public.measurements WHERE id = $1'; + const values = [id]; + await db.query(query, values); + }, + getById: async (id) => { + const query = 'SELECT * FROM public.measurements WHERE id = $1'; + const values = [id]; + const res = await db.query(query, values); + return res.rows[0]; + } +}; + +const videosTable = { + create: async (projectId, measurementIds, videoPath, startTimestamp, endTimestamp, imageCount, resolution, duration, fps, status, name) => { + const query = 'INSERT INTO public.videos (project_id, measurement_ids, video_path, start_timestamp, end_timestamp, image_count, resolution, duration, fps, status, name) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING *'; + const values = [projectId, measurementIds, videoPath, startTimestamp, endTimestamp, imageCount, resolution, duration, fps, status, name]; + const res = await db.query(query, values); + return res.rows[0]; + }, + update: async (id, projectId, measurementIds, videoPath, startTimestamp, endTimestamp, imageCount, resolution, duration, fps, status, name) => { + const query = 'UPDATE public.videos SET project_id = $1, measurement_ids = $2, video_path = $3, start_timestamp = $4, end_timestamp = $5, image_count = $6, resolution = $7, duration = $8, fps = $9, status = $10, name = $11 WHERE id = $12 RETURNING *'; + const values = [projectId, measurementIds, videoPath, startTimestamp, endTimestamp, imageCount, resolution, duration, fps, status, name, id]; + const res = await db.query(query, values); + return res.rows[0]; + }, + delete: async (id) => { + const query = 'DELETE FROM public.videos WHERE id = $1'; + const values = [id]; + await db.query(query, values); + }, + getById: async (id) => { + const query = 'SELECT * FROM public.videos WHERE id = $1'; + const values = [id]; + const res = await db.query(query, values); + return res.rows[0]; + } +}; + +module.exports = { + projectsTable, + measurementsTable, + videosTable +}; \ No newline at end of file