From 021e7e39427d3a8f113dd03fc382e637a7cb0eb9 Mon Sep 17 00:00:00 2001 From: jwmaguire15 Date: Tue, 9 Nov 2021 12:31:12 -0500 Subject: [PATCH 1/5] james comments --- server/controllers/databaseController.js | 107 ++++++++++------------- server/models/model.js | 2 +- server/routes/database.js | 37 +++++--- 3 files changed, 72 insertions(+), 74 deletions(-) diff --git a/server/controllers/databaseController.js b/server/controllers/databaseController.js index b584b0a..9258c72 100644 --- a/server/controllers/databaseController.js +++ b/server/controllers/databaseController.js @@ -10,27 +10,22 @@ const options = { } const geocoder = NodeGeocoder(options); -// get / verify current user + +// verify an existing user /* -Expects: req.body = {username, password} -Returns: {verified: bool, message: string, user: userObject} -User Object: { - user_id: int, - username: string, - password: string, - created_on: timestamp, - coordinate: { - lat: num, - lng: num - } -} +Expects: + req.query = { username: string, password: string } +Returns: + res.locals.verified: boolean + res.locals. message: string + res.locals.user: userObject + + userObject: { user_id: int, username: string, password: string, coordinates: { lat: num, lng: num } } */ dbController.verifyUser = async (req, res, next) => { const { username, password } = req.query; - // if username / password is empty string / not a string throw error const query = `SELECT * FROM users WHERE users.username = $1` const values = [username]; - console.log('Query', req.query); try { // await query response const response = await db.query(query, values); @@ -52,7 +47,6 @@ dbController.verifyUser = async (req, res, next) => { res.locals.verified = false; res.locals.message = 'Invalid password'; res.locals.user = {}; - res.locals.friends = []; return next(); } // send object upon successful log-in @@ -69,29 +63,24 @@ dbController.verifyUser = async (req, res, next) => { } // post/create a new user (encrypt password) -/* -Expects: req.body: { username, password, coordinates } -Returns: [{ user_id: int, - username: string, - password: string, - created_on: timestamp, - coordinate: { - lat: num, - lng: num }] +/* +Expects: req.body = { username: string, password: string, address: string } +Returns: */ dbController.addUser = async (req, res, next) => { try { - // declare a new user object with name, password, coords const { username, password, address } = req.body; + // turns address into coordinates const geoData = await geocoder.geocode(address); const coordinates = { lat: geoData[0].latitude, lng: geoData[0].longitude }; - if (typeof username === 'string' && typeof password === 'string') { + if (typeof username === 'string' && typeof password === 'string' && username.length && password.length ) { + // encrypt the password const encrypted = await bcrypt.hash(password, 10); const query = `INSERT INTO users(username, password, coordinates) VALUES($1, $2, $3) RETURNING *`; const values = [username, encrypted, JSON.stringify(coordinates)]; const response = await db.query(query, values); const user = response.rows[0]; - + res.locals.verified = true; res.locals.message = 'User created!' res.locals.user = user; @@ -112,6 +101,10 @@ dbController.addUser = async (req, res, next) => { // TODO! FINISH THIS METHOD // PUT / update a user's data +/* +Expects: +Returns: +*/ dbController.updateUser = async (req, res, next) => { const { userID, newCoordinates } = req.body; const query = `UPDATE users SET user.coordinates = $2 WHERE user.user_id = $1` @@ -124,12 +117,15 @@ dbController.updateUser = async (req, res, next) => { } } -// get list of all users EXCEPT current user +// get list of all users on the current users friend list +/* +Expects: +Returns: +*/ dbController.getFriendList = async (req, res, next) => { - // declare a var to store our search query - // not equal -> <> OR != - const { user_id } = res.locals.user; - const query = ` + try { + const { user_id } = res.locals.user; + const query = ` SELECT u2.user_id, u2.username, u2.coordinates FROM users u1 JOIN friends ON u1.user_id = friends.user1_id @@ -137,9 +133,7 @@ dbController.getFriendList = async (req, res, next) => { ON u2.user_id = friends.user2_id WHERE u1.user_id = $1 `; - const values = [user_id]; - try { - // send data via res locals + const values = [user_id]; const response = await db.query(query, values); res.locals.friendList = response.rows; return next(); @@ -148,19 +142,21 @@ dbController.getFriendList = async (req, res, next) => { } } -// get list of all users not on current user's friends list +// get list of all users NOT on current user's friends list +/* +Expects: +Returns: +*/ dbController.getNotFriendList = async (req, res, next) => { - // declare a var to store our search query - // not equal -> <> OR != - const { user_id } = res.locals.user; - const query = ` + try { + const { user_id } = res.locals.user; + const query = ` SELECT * FROM users WHERE user_id != $1 AND user_id NOT IN (SELECT user2_id from users JOIN friends ON users.user_id = friends.user1_id WHERE users.user_id = $1) `; - const values = [user_id]; - try { + const values = [user_id]; // send data via res locals const response = await db.query(query, values); res.locals.notFriendList = response.rows; @@ -171,6 +167,10 @@ dbController.getNotFriendList = async (req, res, next) => { } // given an address as a string return the coordinates +/* +Expects: +Returns: +*/ dbController.getCoords = async (req, res, next) => { try { const { address } = req.body @@ -185,14 +185,14 @@ dbController.getCoords = async (req, res, next) => { } // adds a new friend to the current users friend list -/* -expect: -req.body: { user1_id, user2_id } +/* +Expects: +Returns: */ dbController.addFriend = async (req, res, next) => { try { const { user1_id, user2_id } = req.body; - res.locals.user = { user_id: user1_id}; + res.locals.user = { user_id: user1_id }; const values = [user1_id, user2_id]; const query = ` INSERT INTO friends (user1_id, user2_id) VALUES($1, $2) @@ -202,7 +202,7 @@ dbController.addFriend = async (req, res, next) => { res.locals.insert = insert.rows; return next(); } - catch(err) { + catch (err) { return next(err); } } @@ -210,15 +210,4 @@ dbController.addFriend = async (req, res, next) => { // TODOS // // DELETE user from friend list -// post to friends table with user1_id: current user, user2_id, selected user -// dbController.addFriends = async (req, res, next) => { -// try { - -// } -// catch(err) { - -// } -// } - - module.exports = dbController; \ No newline at end of file diff --git a/server/models/model.js b/server/models/model.js index 684ed72..74339fb 100644 --- a/server/models/model.js +++ b/server/models/model.js @@ -1,6 +1,6 @@ const { Pool } = require('pg'); -const PG_URI = 'POST-GRES-KEY-HERE' +const PG_URI = `postgres://gwzelhay:bpQW0XH3OXZFr34fAp1-Bj8x1997cnXS@fanny.db.elephantsql.com/gwzelhay` // create a new pool here using the connection string above const pool = new Pool({ diff --git a/server/routes/database.js b/server/routes/database.js index 5cae34d..4fa1273 100644 --- a/server/routes/database.js +++ b/server/routes/database.js @@ -2,26 +2,39 @@ const express = require('express'); const dbController = require('../controllers/databaseController'); const router = express.Router(); -// get/verify current user +// get/verify current user, get a list of all of their friends, and all of their NOT friends +/* +Expects: +Returns: +*/ router.get('/login', dbController.verifyUser, dbController.getFriendList, dbController.getNotFriendList, (req, res) => { - // status - // verified - // message - // user - // friends return res.json(res.locals); }); // post/create a new user (encrypt password) +/* +Expects: +Returns: +*/ router.post('/signup', dbController.addUser, (req, res) => { return res.status(201).json(res.locals); }); +// TODO! - get this going // put/update current user's data (location, interests) -// router.put('/', dbController.updateUser, (req, res) => { -// return res.status(201).json(res.locals.user); -// }) +/* +Expects: +Returns: +*/ +router.put('/', dbController.updateUser, (req, res) => { + return res.status(201).json(res.locals.user); +}) +// add two users to the "friends" table, get the new friends list of current user, get the new NOT friends list of current user +/* +Expects: +Returns: +*/ router.post('/friend', dbController.addFriend, dbController.getFriendList, dbController.getNotFriendList, (req, res) => { return res.status(201).json(res.locals) }) @@ -31,12 +44,8 @@ router.get('/coordinates', dbController.getCoords, (req, res) => { }) // TODOS // -// add/get/post user to friend list - // delete/remove user from friend list -// ???? post to friends table with user1_id: current user, user2_id, selected user - -// TODO! add friends, delete friends +// let other user confirm whether or not to be friends module.exports = router; \ No newline at end of file From b10923ec5e3c7a98b9beeead191c99dd8e6855de Mon Sep 17 00:00:00 2001 From: jwmaguire15 Date: Tue, 9 Nov 2021 14:11:46 -0500 Subject: [PATCH 2/5] progress on making comments --- server/controllers/databaseController.js | 25 ++++++++++++++++++------ server/models/model.js | 6 ------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/server/controllers/databaseController.js b/server/controllers/databaseController.js index 9258c72..d03c6d4 100644 --- a/server/controllers/databaseController.js +++ b/server/controllers/databaseController.js @@ -64,8 +64,12 @@ dbController.verifyUser = async (req, res, next) => { // post/create a new user (encrypt password) /* -Expects: req.body = { username: string, password: string, address: string } +Expects: + req.body = { username: string, password: string, address: string } Returns: + res.locals.verified = boolean, + res.locals.message = string, + res.locals.user = userObj */ dbController.addUser = async (req, res, next) => { try { @@ -73,7 +77,7 @@ dbController.addUser = async (req, res, next) => { // turns address into coordinates const geoData = await geocoder.geocode(address); const coordinates = { lat: geoData[0].latitude, lng: geoData[0].longitude }; - if (typeof username === 'string' && typeof password === 'string' && username.length && password.length ) { + if (typeof username === 'string' && typeof password === 'string' && username.length && password.length) { // encrypt the password const encrypted = await bcrypt.hash(password, 10); const query = `INSERT INTO users(username, password, coordinates) VALUES($1, $2, $3) RETURNING *`; @@ -102,16 +106,19 @@ dbController.addUser = async (req, res, next) => { // TODO! FINISH THIS METHOD // PUT / update a user's data /* -Expects: +Expects: + req.body = { user_id, newCoordinates } Returns: + res.locals.user = user; */ dbController.updateUser = async (req, res, next) => { - const { userID, newCoordinates } = req.body; - const query = `UPDATE users SET user.coordinates = $2 WHERE user.user_id = $1` - const values = [newCoordinates]; + const { user_id, newCoordinates } = req.body; + const query = `UPDATE users SET users.coordinates = $2 WHERE users.user_id = $1` + const values = [user_id, newCoordinates]; try { const response = await db.query(query, values); res.locals.user = response; + return next(); } catch (err) { return next(err); } @@ -120,7 +127,9 @@ dbController.updateUser = async (req, res, next) => { // get list of all users on the current users friend list /* Expects: + res.locals.user = { user_id: int } Returns: + res.locals.friendList = [ userObj ] */ dbController.getFriendList = async (req, res, next) => { try { @@ -145,7 +154,9 @@ dbController.getFriendList = async (req, res, next) => { // get list of all users NOT on current user's friends list /* Expects: + res.locals.user = { user_id: int } Returns: + res.locals.notFriendList = [ userObj ] */ dbController.getNotFriendList = async (req, res, next) => { try { @@ -187,7 +198,9 @@ dbController.getCoords = async (req, res, next) => { // adds a new friend to the current users friend list /* Expects: + req.body = { user1_id: int, user2_id: int } Returns: + res.locals.insert = [userObj] */ dbController.addFriend = async (req, res, next) => { try { diff --git a/server/models/model.js b/server/models/model.js index 74339fb..3a654c1 100644 --- a/server/models/model.js +++ b/server/models/model.js @@ -7,12 +7,6 @@ const pool = new Pool({ connectionString: PG_URI }); -/* -get the username, and the friends name -from every column where the user_id is in column 1 -OR the user_id is in column 2 -*/ - // We export an object that contains a property called query, // which is a function that returns the invocation of pool.query() after logging the query // This will be required in the controllers to be the access point to the database From 59c97d1a4cd5a7e2e0cd8ba8486d91f118d7a1a4 Mon Sep 17 00:00:00 2001 From: jwmaguire15 Date: Tue, 9 Nov 2021 14:21:14 -0500 Subject: [PATCH 3/5] progress on commenting backend --- server/controllers/databaseController.js | 11 +++---- server/routes/database.js | 39 +++++++++++++++++++----- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/server/controllers/databaseController.js b/server/controllers/databaseController.js index d03c6d4..54cb8cf 100644 --- a/server/controllers/databaseController.js +++ b/server/controllers/databaseController.js @@ -109,15 +109,15 @@ dbController.addUser = async (req, res, next) => { Expects: req.body = { user_id, newCoordinates } Returns: - res.locals.user = user; + res.locals.user = userObj; */ dbController.updateUser = async (req, res, next) => { const { user_id, newCoordinates } = req.body; - const query = `UPDATE users SET users.coordinates = $2 WHERE users.user_id = $1` + const query = `UPDATE users SET users.coordinates = $2 WHERE users.user_id = $1 RETURNING *` const values = [user_id, newCoordinates]; try { const response = await db.query(query, values); - res.locals.user = response; + res.locals.user = response.rows[0]; return next(); } catch (err) { return next(err); @@ -200,19 +200,18 @@ dbController.getCoords = async (req, res, next) => { Expects: req.body = { user1_id: int, user2_id: int } Returns: - res.locals.insert = [userObj] + res.locals.insert = userObj */ dbController.addFriend = async (req, res, next) => { try { const { user1_id, user2_id } = req.body; - res.locals.user = { user_id: user1_id }; const values = [user1_id, user2_id]; const query = ` INSERT INTO friends (user1_id, user2_id) VALUES($1, $2) RETURNING * `; const insert = await db.query(query, values); - res.locals.insert = insert.rows; + res.locals.user = insert.rows[0]; return next(); } catch (err) { diff --git a/server/routes/database.js b/server/routes/database.js index 4fa1273..211774b 100644 --- a/server/routes/database.js +++ b/server/routes/database.js @@ -4,8 +4,17 @@ const router = express.Router(); // get/verify current user, get a list of all of their friends, and all of their NOT friends /* -Expects: -Returns: +Expects: + req.query = { username: string, password: string } +Returns: { + verified: boolean + message: string + user: userObject + friendList: [ userObject ] + notFriendList: [ userObject ] + } + + userObject: { user_id: int, username: string, password: string, coordinates: { lat: num, lng: num } }Returns: */ router.get('/login', dbController.verifyUser, dbController.getFriendList, dbController.getNotFriendList, (req, res) => { return res.json(res.locals); @@ -13,18 +22,26 @@ router.get('/login', dbController.verifyUser, dbController.getFriendList, dbCont // post/create a new user (encrypt password) /* -Expects: -Returns: +Expects: + req.body = { username: string, password: string, address: string } +Returns: { + verified = boolean, + message = string, + user = userObj, + friendList: [ userObject ] + notFriendList: [ userObject ] + } */ -router.post('/signup', dbController.addUser, (req, res) => { +router.post('/signup', dbController.addUser, dbController.getFriendList, dbController.getNotFriendList, (req, res) => { return res.status(201).json(res.locals); }); -// TODO! - get this going // put/update current user's data (location, interests) /* -Expects: +Expects: + req.body = { user_id, newCoordinates } Returns: + userObj; */ router.put('/', dbController.updateUser, (req, res) => { return res.status(201).json(res.locals.user); @@ -33,7 +50,13 @@ router.put('/', dbController.updateUser, (req, res) => { // add two users to the "friends" table, get the new friends list of current user, get the new NOT friends list of current user /* Expects: -Returns: + req.body = { user1_id: int, user2_id: int } +Returns: { + user: userObj, + friendList: [ userObj ], + notFriendList: [ userObj ], +} + */ router.post('/friend', dbController.addFriend, dbController.getFriendList, dbController.getNotFriendList, (req, res) => { return res.status(201).json(res.locals) From 1c4065aa0bb7c6a45a568557d47498b6fb32cc1e Mon Sep 17 00:00:00 2001 From: jwmaguire15 Date: Tue, 9 Nov 2021 14:23:52 -0500 Subject: [PATCH 4/5] done? --- client/components/Map.jsx | 2 +- server/controllers/databaseController.js | 2 +- server/routes/database.js | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/client/components/Map.jsx b/client/components/Map.jsx index 45dcbcf..99351dd 100644 --- a/client/components/Map.jsx +++ b/client/components/Map.jsx @@ -14,7 +14,7 @@ const Map = ({midpoint}) => {
{/* do markers go in here? */} diff --git a/server/controllers/databaseController.js b/server/controllers/databaseController.js index 54cb8cf..37d3f61 100644 --- a/server/controllers/databaseController.js +++ b/server/controllers/databaseController.js @@ -6,7 +6,7 @@ const dbController = {}; const options = { provider: 'google', - apiKey: 'API-KEY-HERE', + apiKey: 'AIzaSyAG8pD29eYb7EnZNrNFinFbmMtJiqqnzKI', } const geocoder = NodeGeocoder(options); diff --git a/server/routes/database.js b/server/routes/database.js index 211774b..11fc0a0 100644 --- a/server/routes/database.js +++ b/server/routes/database.js @@ -56,7 +56,6 @@ Returns: { friendList: [ userObj ], notFriendList: [ userObj ], } - */ router.post('/friend', dbController.addFriend, dbController.getFriendList, dbController.getNotFriendList, (req, res) => { return res.status(201).json(res.locals) From c961113925c596df1b136d26a7b32ca74ff4c94e Mon Sep 17 00:00:00 2001 From: jwmaguire15 Date: Tue, 9 Nov 2021 14:33:48 -0500 Subject: [PATCH 5/5] got add friend working --- client/components/Map.jsx | 1 - client/components/Sidebar.jsx | 10 +++++++--- server/controllers/databaseController.js | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/client/components/Map.jsx b/client/components/Map.jsx index 99351dd..c27593c 100644 --- a/client/components/Map.jsx +++ b/client/components/Map.jsx @@ -25,5 +25,4 @@ const Map = ({midpoint}) => { ) } - export default Map; diff --git a/client/components/Sidebar.jsx b/client/components/Sidebar.jsx index f15678f..618edf4 100644 --- a/client/components/Sidebar.jsx +++ b/client/components/Sidebar.jsx @@ -60,15 +60,19 @@ const Sidebar = (props) => { {/* when clicked, triggers action to get that friend's location and use it to find the midpoint */}
- {/* eventual functionality to add a friend to user's friend list by name search */} +
+

Add New Friends:

+ {/* dropdown populated with users from friends list */} + {console.log('Props not friends list', props.notFriendsList)} {/* array of objects with user_id, username, and coordinates properties*/} + -
{props.notFriendsList.map((notFriend, i) => { +
{props.notFriendsList.map((notFriend, i) => { return (
{notFriend.username}
) })} - +
) diff --git a/server/controllers/databaseController.js b/server/controllers/databaseController.js index 37d3f61..35a7db9 100644 --- a/server/controllers/databaseController.js +++ b/server/controllers/databaseController.js @@ -208,7 +208,7 @@ dbController.addFriend = async (req, res, next) => { const values = [user1_id, user2_id]; const query = ` INSERT INTO friends (user1_id, user2_id) VALUES($1, $2) - RETURNING * + RETURNING user1_id as user_id `; const insert = await db.query(query, values); res.locals.user = insert.rows[0];