Skip to content
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
3 changes: 1 addition & 2 deletions client/components/Map.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Map = ({midpoint}) => {

<div id="map-container" className='mapStyles'>
<GoogleMapReact
bootstrapURLKeys={{key: 'YOUR-API-KEY-HERE'}}
bootstrapURLKeys={{key: "AIzaSyAG8pD29eYb7EnZNrNFinFbmMtJiqqnzKI"}}
defaultCenter={midpoint}
defaultZoom={12}>
{/* do markers go in here? */}
Expand All @@ -25,5 +25,4 @@ const Map = ({midpoint}) => {
)
}


export default Map;
10 changes: 7 additions & 3 deletions client/components/Sidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,19 @@ const Sidebar = (props) => {

{/* when clicked, triggers action to get that friend's location and use it to find the midpoint */}
</div>
{/* eventual functionality to add a friend to user's friend list by name search */}
<div id="friend-list" className='center'>
<p>Add New Friends:</p>
{/* 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*/}


<div className='inputStyles' className='center'>{props.notFriendsList.map((notFriend, i) => {
<div className='inputStyles' className='center'>{props.notFriendsList.map((notFriend, i) => {
return (<div id={notFriend.user_id} key={notFriend.user_id} value={notFriend.username}>
<button onClick={() => props.addFriend(props.currentUserID, notFriend.user_id)}>
Add Friend
</button> {notFriend.username} </div>)
})}

</div>
</div>
</div>
)
Expand Down
133 changes: 67 additions & 66 deletions server/controllers/databaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -112,34 +105,44 @@ 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
JOIN users u2
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();
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -185,40 +196,30 @@ 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);
}
}

// 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;
8 changes: 1 addition & 7 deletions server/models/model.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
61 changes: 46 additions & 15 deletions server/routes/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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;