Skip to content

Commit

Permalink
fixed seeding bug where users friended themselves
Browse files Browse the repository at this point in the history
  • Loading branch information
javidangarcia committed Aug 8, 2023
1 parent 8af531b commit 7ab2b36
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
5 changes: 3 additions & 2 deletions backend/.env.template
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Database Connection URL
# Rename this file to .env

# Enter the URL for your PostgreSQL database connection.
# Example: DATABASE=postgresql://username:password@localhost/db_name
DATABASE=

# Enter the URL for your frontend.
FRONTEND_URL=

# Enter prod for production and dev for development
# Enter prod for production and dev for development.
DEVELOPMENT=
2 changes: 1 addition & 1 deletion backend/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Instructions for Starting Express Backend

- Follow instructions in .env.template and rename to .env
- Follow instructions in .env.template
- OPTIONAL: Run node seed.js in terminal
- Run npm install in terminal
- Run npm run start in terminal
27 changes: 19 additions & 8 deletions backend/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ const seedDatabase = async () => {
const likeRelationships = [];
const dislikeRelationships = [];
const followRelationships = [];
const friendRelationships = [];
const friendRelationships = new Set();

createdUsers.forEach((user) => {
const numStocksToLike = Math.floor(
Expand All @@ -161,7 +161,7 @@ const seedDatabase = async () => {
Math.random() * (createdStocks.length + 1)
);
const numFriends = Math.floor(
Math.random() * (createdUsers.length + 1)
Math.random() * (createdUsers.length - 1)
);

const randomStocks = [...createdStocks];
Expand All @@ -186,7 +186,9 @@ const seedDatabase = async () => {
const stocksToLike = randomStocks.slice(0, numStocksToLike);
const stocksToDislike = randomStocks.slice(0, numStocksToDislike);
const stocksToFollow = randomStocks.slice(0, numStocksToFollow);
const friends = randomUsers.slice(0, numFriends);
const friends = randomUsers
.filter((randomUser) => randomUser.id !== user.id)
.slice(0, numFriends);

stocksToLike.forEach((stock) => {
likeRelationships.push({
Expand All @@ -210,13 +212,22 @@ const seedDatabase = async () => {
});

friends.forEach((friend) => {
friendRelationships.push({
UserId1: user.id,
UserId2: friend.id
});
const relationshipKey1 = `${user.id}-${friend.id}`;
const relationshipKey2 = `${friend.id}-${user.id}`;

friendRelationships.add(relationshipKey1);
friendRelationships.add(relationshipKey2);
});
});

const friendRelationshipsArray = Array.from(friendRelationships).map((relationshipKey) => {
const [UserId1, UserId2] = relationshipKey.split('-');
return {
UserId1: parseInt(UserId1),
UserId2: parseInt(UserId2)
};
});

await Like.bulkCreate(likeRelationships);
console.log("Like relationships have been seeded!");

Expand All @@ -226,7 +237,7 @@ const seedDatabase = async () => {
await Follow.bulkCreate(followRelationships);
console.log("Follow relationships have been seeded!");

await Friend.bulkCreate(friendRelationships);
await Friend.bulkCreate(friendRelationshipsArray);
console.log("Friend relationships have been seeded!");
} catch (error) {
console.error("Error seeding data:", error);
Expand Down
2 changes: 1 addition & 1 deletion frontend/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Instructions for Starting React Frontend

- Follow instructions in .env.template and rename to .env
- Follow instructions in .env.template
- Run npm install in terminal
- Run npm run dev in terminal

0 comments on commit 7ab2b36

Please sign in to comment.