Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Projects Table CRUD #29

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions backend/src/DatabaseCreds.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Db_Host = 'localhost'
Db_Port = '5432'
Db_Username = 'postgres'
Db_Password = "rudy123"
Db_Database = 'TechClub_Website'
21 changes: 21 additions & 0 deletions backend/src/db.js
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 5 additions & 2 deletions backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand Down
83 changes: 83 additions & 0 deletions backend/src/projects.js
Original file line number Diff line number Diff line change
@@ -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]
}