diff --git a/client/components/Map.jsx b/client/components/Map.jsx index 45dcbcf..c27593c 100644 --- a/client/components/Map.jsx +++ b/client/components/Map.jsx @@ -14,7 +14,7 @@ const Map = ({midpoint}) => {
{/* do markers go in here? */} @@ -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 b584b0a..35a7db9 100644 --- a/server/controllers/databaseController.js +++ b/server/controllers/databaseController.js @@ -6,31 +6,26 @@ const dbController = {}; const options = { provider: 'google', - apiKey: 'API-KEY-HERE', + apiKey: 'AIzaSyAG8pD29eYb7EnZNrNFinFbmMtJiqqnzKI', } 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,28 @@ 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: + res.locals.verified = boolean, + res.locals.message = string, + res.locals.user = userObj */ 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,24 +105,36 @@ dbController.addUser = async (req, res, next) => { // TODO! FINISH THIS METHOD // PUT / update a user's data +/* +Expects: + req.body = { user_id, newCoordinates } +Returns: + res.locals.user = userObj; +*/ 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 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); } } -// get list of all users EXCEPT current user +// 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) => { - // 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 +142,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 +151,23 @@ 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: + res.locals.user = { user_id: int } +Returns: + res.locals.notFriendList = [ userObj ] +*/ 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 +178,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,24 +196,25 @@ dbController.getCoords = async (req, res, next) => { } // adds a new friend to the current users friend list -/* -expect: -req.body: { user1_id, user2_id } +/* +Expects: + req.body = { user1_id: int, user2_id: int } +Returns: + 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 * + RETURNING user1_id as user_id `; const insert = await db.query(query, values); - res.locals.insert = insert.rows; + res.locals.user = insert.rows[0]; return next(); } - catch(err) { + catch (err) { return next(err); } } @@ -210,15 +222,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..3a654c1 100644 --- a/server/models/model.js +++ b/server/models/model.js @@ -1,18 +1,12 @@ 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({ 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 diff --git a/server/routes/database.js b/server/routes/database.js index 5cae34d..11fc0a0 100644 --- a/server/routes/database.js +++ b/server/routes/database.js @@ -2,26 +2,61 @@ 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: + 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) => { - // status - // verified - // message - // user - // friends return res.json(res.locals); }); // post/create a new user (encrypt password) -router.post('/signup', dbController.addUser, (req, res) => { +/* +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, dbController.getFriendList, dbController.getNotFriendList, (req, res) => { return res.status(201).json(res.locals); }); // put/update current user's data (location, interests) -// router.put('/', dbController.updateUser, (req, res) => { -// return res.status(201).json(res.locals.user); -// }) +/* +Expects: + req.body = { user_id, newCoordinates } +Returns: + userObj; +*/ +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: + 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) }) @@ -31,12 +66,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