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