Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
justinchoo93 committed Jul 29, 2020
0 parents commit 6bf3320
Show file tree
Hide file tree
Showing 33 changed files with 12,801 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.DS_Store
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# scratch-project


Welcome to Goblin Shark territory. V2
10,855 changes: 10,855 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"name": "bob",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node ./server/server.js",
"build": "cross-env NODE_ENV=production webpack",
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot & nodemon ./server/server.js",
"dew": "concurrently \"cross-env NODE_ENV=development webpack-dev-server --open --hot --inline --progress --colors --watch --content-base ./\" \"nodemon ./server/server.js\"",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/the-goblin-shark/scratch-project.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/the-goblin-shark/scratch-project/issues"
},
"homepage": "https://github.com/the-goblin-shark/scratch-project#readme",
"devDependencies": {
"@babel/core": "^7.10.5",
"@babel/plugin-transform-runtime": "^7.10.5",
"@babel/preset-env": "^7.10.4",
"@babel/preset-react": "^7.10.4",
"@hot-loader/react-dom": "^16.13.0",
"@types/react": "^16.9.43",
"@types/react-dom": "^16.9.8",
"babel-loader": "^8.1.0",
"cross-env": "^7.0.2",
"css-loader": "^3.6.0",
"html-webpack-plugin": "^4.3.0",
"node-sass": "^4.14.1",
"nodemon": "^2.0.4",
"sass-loader": "^9.0.2",
"style-loader": "^1.2.1",
"ts-loader": "^8.0.1",
"typescript": "^3.9.7",
"webpack": "^4.44.0",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0"
},
"dependencies": {
"@babel/runtime": "^7.10.5",
"axios": "^0.19.2",
"bcrypt": "^5.0.0",
"bootstrap": "^4.5.0",
"cookie": "^0.4.1",
"cookie-parser": "^1.4.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-flash": "0.0.2",
"express-session": "^1.17.1",
"passport": "^0.4.1",
"passport-local": "^1.0.0",
"pg": "^8.3.0",
"react": "^16.13.1",
"react-bootstrap": "^1.3.0",
"react-bootstrap-typeahead": "^5.1.0",
"react-dom": "^16.13.1",
"react-hot-loader": "^4.12.21",
"react-router-dom": "^5.2.0"
}
}
15 changes: 15 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Scratch Project</title>
</head>

<body>
<div id="root"></div>
</body>

</html>
94 changes: 94 additions & 0 deletions server/Controllers/authController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const bcrypt = require('bcrypt');
const model = require('../Models/model.js');

const authController = {};

authController.register = async (req, res, next) => {
const { username, password, email, firstname, lastname } = req.body;
const queryText = `INSERT INTO User_credentials (username,password,email) VALUES ($1,$2,$3)`;
const usersQueryText = `INSERT INTO Users (username, firstname, lastname) VALUES($1,$2,$3)`;
const hashedPassWord = await bcrypt.hash(password, 10);
try {
await model.query(queryText, [username, hashedPassWord, email]);
await model.query(usersQueryText, [username, firstname, lastname]);
return next();
} catch (err) {
console.log(err);
return next({
log: `error occurred at register middleware. error message is: ${err}`,
status: 400,
message: { err: 'An error occurred' },
});
}
};

// ToDO : new columns in the Users table, about, linkedin, personal url
// new table to link users to tech stack(association table),
// signup new fields for firstname and lastname

// middleware for get profiles
authController.getProfile = async (req, res, next) => {
const { username } = req.params;
const queryText = `SELECT * FROM Users WHERE username=$1`;
try {
const userData = await model.query(queryText, [username]);
[res.locals.userData] = userData.rows;
return next();
} catch (err) {
console.log(err);
return next({
log: `error occurred at getProfile middleware. error message is: ${err}`,
status: 400,
message: { err: 'An error occurred' },
});
}
};

// middeware to edit profiles (INCOMPLETE)
authController.editProfile = async (req, res, next) => {
const {
username,
firstName,
lastName,
about,
profilepic,
githubHandle,
linkedIn,
personalPage,
} = req.body;

const queryText = `UPDATE Users
SET firstname=$1,
lastname=$2,
about=$3
profilepic=$4,
githubhandle=$5,
linkedin=$6,
personalpage=$7
WHERE username=$8`;

const queryValue = [
firstName,
lastName,
about,
profilepic,
githubHandle,
linkedIn,
personalPage,
username,
];

try {
await model.query(queryText, queryValue);
return next();
} catch (err) {
console.log(err);
return next({
log: `error occurred at getProfile middleware. error message is: ${err}`,
status: 400,
message: { err: 'An error occurred' },
});
}
};

module.exports = authController;
169 changes: 169 additions & 0 deletions server/Controllers/ideaController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
const model = require('../Models/model.js');

// controllers for explor page
const ideaController = {};

// middleware to get all ideas data from database
ideaController.getIdeas = (req, res, next) => {
/* query text will join tables for ideas, idea_tech_stacks, and tech_stacks
then aggregate the tech stack names into an array
*/
const queryText = `SELECT Ideas.*, array_agg(tech_stacks.name) AS techstacks FROM Ideas
JOIN Idea_tech_stacks ON Idea_tech_stacks.idea_id = Ideas.idea_id
JOIN tech_stacks ON tech_stacks.tech_id=Idea_tech_stacks.tech_id
GROUP BY Ideas.idea_id`;

model.query(queryText, (err, results) => {
if (err) {
console.log(err);
return next({
log: `error occurred at getIdeas middleware. error message is: ${err}`,
status: 400,
message: { err: 'An error occurred' },
});
}
// console.log('results', results.rows);
res.locals.ideas = results.rows;
return next();
});
};

// INSERT INTO Ideas (name, description, why, when_start, when_end, who, image, creator_username) VALUES ('scratch', 'scratch project', 'for fun', '2020-07-25', '2020-08-15', '3', 'image.png', 'hello1');

// we need to know who's submitting the idea
ideaController.submitIdea = (req, res, next) => {
const {
name,
description,
why,
techStack,
whenStart,
whenEnd,
teamNumber,
imageURL,
username,
} = req.body;

const teamNumberInt = Number(teamNumber);

// will need to get user's username (may need to modify database to grab user_id instead...)
// front end to modify date to following format YYYY-MM-DD
// if dateEnd is not given, front end to assign it as null

// if imageurl or endDate is falsy, then we have to omit from query text/value so that it will default to default image or date(null)
let queryText1;
let queryValue1;
if (!whenEnd && !imageURL) {
queryText1 = `INSERT INTO Ideas (name, description, why, when_start, who, creator_username) VALUES ($1, $2, $3, $4, $5, $6) RETURNING idea_id`;
queryValue1 = [name, description, why, whenStart, teamNumberInt, username];
} else if (!imageURL) {
queryText1 = `INSERT INTO Ideas (name, description, why, when_start, when_end, who, creator_username) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING idea_id`;
queryValue1 = [
name,
description,
why,
whenStart,
whenEnd,
teamNumberInt,
username,
];
} else if (!whenEnd) {
queryText1 = `INSERT INTO Ideas (name, description, why, when_start, who, image, creator_username) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING idea_id`;
queryValue1 = [
name,
description,
why,
whenStart,
teamNumberInt,
imageURL,
username,
];
} else {
queryText1 = `INSERT INTO Ideas (name, description, why, when_start, when_end, who, image, creator_username) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING idea_id`;
queryValue1 = [
name,
description,
why,
whenStart,
whenEnd,
teamNumberInt,
imageURL,
username,
];
}
let addedIdeaId;
model.query(queryText1, queryValue1, async (err, result) => {
if (err) {
console.log(err);
return next({
log: `error occurred at submitIdea middleware query1. error message is: ${err}`,
status: 400,
message: { err: 'An error occurred' },
});
}
addedIdeaId = result.rows[0].idea_id;

// separate query to insert tech stacks into idea_tech_stacks
let queryText2;
const quertValue2 = [];
for (let i = 0; i < techStack.length; i += 1) {
quertValue2.push([addedIdeaId, techStack[i]]);
}
// console.log(techStack);
for (let i = 0; i < techStack.length; i += 1) {
queryText2 = `INSERT INTO Idea_tech_stacks (idea_id, tech_id) VALUES ($1, $2)`;
await model.query(queryText2, quertValue2[i], (err) => {
if (err) {
console.log(err);
return next({
log: `error occurred at submitIdea middleware query2. error message is: ${err}`,
status: 400,
message: { err: 'An error occurred' },
});
}
});
}
return next();
});
};

// middleware to get one idea
// need to set up route for this
ideaController.getOneIdea = async (req, res, next) => {
const id = req.params.ideaID;
try {
const ideasQueryText = `SELECT * FROM Ideas
JOIN Users
ON ideas.creator_username = users.username
WHERE idea_id=${id}`;
const ideaDetail = await model.query(ideasQueryText);
// rows will only contain one. ok to destructure
[res.locals.idea] = ideaDetail.rows;

const participantQueryText = `SELECT *
FROM idea_participants
JOIN users
ON idea_participants.participant_username = users.username
WHERE idea_id = ${id}`;
const participants = await model.query(participantQueryText);
//will return array of objects
res.locals.idea = { ...res.locals.idea, participants: participants.rows };

const stackQueryText = `SELECT * FROM idea_tech_stacks
JOIN tech_stacks
ON tech_stacks.tech_id = idea_tech_stacks.tech_id
WHERE idea_id = ${id}`;
const techStacks = await model.query(stackQueryText);
res.locals.idea = { ...res.locals.idea, techStacks: techStacks.rows };

return next();
} catch (err) {
return next({
log: `error occurred at getOneIdea middleware. error message is: ${err}`,
status: 400,
message: { err: 'An error occurred' },
});
}
};

module.exports = ideaController;
22 changes: 22 additions & 0 deletions server/Controllers/techController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const model = require('../Models/model.js');

const techController = {};

techController.getTechs = (req, res, next) => {
const queryText = 'SELECT * FROM Tech_stacks';
model.query(queryText, (err, results) => {
if (err) {
console.log(err);
return next({
log: `error occurred at getTechs middleware. error message is: ${err}`,
status: 400,
message: { err: 'An error occurred' },
});
}

res.locals.techs = results.rows;
return next();
});
};

module.exports = techController;
15 changes: 15 additions & 0 deletions server/Models/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { Pool } = require('pg');
require('dotenv').config();

const PG_URI = process.env.elephantURI;

const pool = new Pool({
connectionString: PG_URI,
});

module.exports = {
query: (text, params, callback) => {
console.log('executed query', text);
return pool.query(text, params, callback);
},
};
Loading

0 comments on commit 6bf3320

Please sign in to comment.