From 6209640b0bcc7bae5638ec00fc31d902a1b145a3 Mon Sep 17 00:00:00 2001 From: Rudraa Date: Thu, 9 May 2024 19:16:06 +0400 Subject: [PATCH] Projects Table CRUD --- backend/src/DatabaseCreds.env | 5 +++ backend/src/db.js | 21 +++++++++ backend/src/index.js | 7 ++- backend/src/projects.js | 83 +++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 backend/src/DatabaseCreds.env create mode 100644 backend/src/db.js create mode 100644 backend/src/projects.js diff --git a/backend/src/DatabaseCreds.env b/backend/src/DatabaseCreds.env new file mode 100644 index 0000000..40219bc --- /dev/null +++ b/backend/src/DatabaseCreds.env @@ -0,0 +1,5 @@ +Db_Host = 'localhost' +Db_Port = '5432' +Db_Username = 'postgres' +Db_Password = "rudy123" +Db_Database = 'TechClub_Website' diff --git a/backend/src/db.js b/backend/src/db.js new file mode 100644 index 0000000..70b2815 --- /dev/null +++ b/backend/src/db.js @@ -0,0 +1,21 @@ +//db.js + +/* +Consists of the code used to establish a database connection +*/ + +const {Pool} = require('pg') +require("dotenv").config({path: 'DatabaseCreds.env'}); + + +const pool = new Pool({ + user: process.env.Db_Username, + host: process.env.Db_Host, + database: process.env.Db_Database, + password: process.env.Db_Password, + port: process.env.Db_Port, +}); + +console.log(process.env.Password) + +module.exports = pool; diff --git a/backend/src/index.js b/backend/src/index.js index f5f395f..9ac7158 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -2,8 +2,11 @@ const express = require('express') const app = express() const port = 3000 -app.get('/', (req, res) => { - res.send('Hello World!'); +const db = require('./db.js') + +app.get('/', async (req, res) => { + + res.status(200).status('Working Good :)') }) app.listen(port, () => { diff --git a/backend/src/projects.js b/backend/src/projects.js new file mode 100644 index 0000000..3104f50 --- /dev/null +++ b/backend/src/projects.js @@ -0,0 +1,83 @@ +//projects.js + +const pool = require('./db.js') + +/* +The following file contains the code to +assemble the projects table, display functions and functions to +perform CRUD Operations + */ + +async function createTable() { + //ENUM for project status + + await pool.query(` + CREATE TYPE project_status as ENUM('Hiring', 'Completed','Ongoing'); + + `); + + //This code creates the table + + await pool.query(` + + CREATE TABLE Projects( + id integer, + name varchar(50) , + body varchar(200) , + description varchar(200), + tags varchar(200), + status project_status, + created_date date, + last_modified timestamp without time zone DEFAULT CURRENT_TIMESTAMP + ); + `); + + //This code is a trigger and a function code applied whenever a change + //is made on the Projects Table + + await pool.query(`CREATE OR REPLACE FUNCTION update_last_modified() + RETURNS trigger + LANGUAGE 'plpgsql' + + AS $$ + + BEGIN + NEW.last_modified = CURRENT_TIMESTAMP; + RETURN NEW; + + END; + $$; + `) + + await pool.query(`CREATE OR REPLACE TRIGGER trigger_update_last_modified + BEFORE UPDATE + ON Projects + FOR EACH ROW + EXECUTE FUNCTION update_last_modified(); + `) +}; + + +async function createProject(id,name,body, description, tags, status, created_date, last_modified){ + //This inserts an entry into the table + await pool.query(` + INSERT INTO Projects VALUES(${id},${name},${body},${description},${tags},${status},${created_date},${last_modified}); + `); +} + +/* Now some of the fetch functions to get a particular Project*/ + +async function fetchProjects(){ + const projects = await pool.query(` + SELECT * FROM Projects; + `) + return projects.rows[0]; +} + +async function fetchProjectsByID(id){ + const project = await pool.query(` + SELECT * FROM Projects WHERE id = ${id}; + `) + + return project.rows[0] +} \ No newline at end of file