From c0d07e3ec6d36c542f5a5d7b3ae88277e14c3968 Mon Sep 17 00:00:00 2001 From: Kerboul Date: Wed, 15 Jan 2025 15:14:08 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20des=20fonctions=20de=20gestion=20des=20?= =?UTF-8?q?projets=20dans=20dbUtils=20et=20cr=C3=A9ation=20d'un=20fichier?= =?UTF-8?q?=20de=20test=20pour=20valider=20les=20op=C3=A9rations=20de=20la?= =?UTF-8?q?=20base=20de=20donn=C3=A9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/dbTester.js | 14 ++++++++++ utils/dbUtils.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 test/dbTester.js diff --git a/test/dbTester.js b/test/dbTester.js new file mode 100644 index 0000000..4c37eaa --- /dev/null +++ b/test/dbTester.js @@ -0,0 +1,14 @@ +const databaseUtils = require('../utils/dbUtils'); + +console.log('Testing database functions...'); + +async function printProjects() { + const projects = await databaseUtils.getProjects(); + console.log('Projects:', projects); +} + +try { + printProjects(); +} catch (error) { + console.error('Error:', error); +} \ No newline at end of file diff --git a/utils/dbUtils.js b/utils/dbUtils.js index e69de29..68227be 100644 --- a/utils/dbUtils.js +++ b/utils/dbUtils.js @@ -0,0 +1,71 @@ +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); + } + }, + + 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); + } + }, + + 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); + } + }, + + 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); + } + } +}; + +module.exports = projectsTable; \ No newline at end of file