Skip to content

Commit

Permalink
adding user friend and friend code system
Browse files Browse the repository at this point in the history
  • Loading branch information
sord-dev committed Aug 8, 2023
1 parent e10067e commit 814c70d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/models/UserModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ User.beforeCreate(async (user) => {
user.username = generatedUsername;
});

User.belongsToMany(User, {
as: 'friends',
through: 'UserFriends',
foreignKey: 'userId',
otherKey: 'friendId'
});

User.belongsToMany(Team, { through: 'UserTeam' });
Team.belongsToMany(User, { through: 'UserTeam' });

Expand Down
26 changes: 25 additions & 1 deletion src/routes/UserRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ router.get('/', async (req, res) => {
try {
const user = await UserService.query(req.userId)

res.json(user)
const formatted = { ...user.dataValues, friends: user.friends.map(t => t.username) }

res.json(formatted)
} catch (error) {
console.log(error)
res.json({ message: error.message })
Expand All @@ -24,5 +26,27 @@ router.get('/teams', async (req, res) => {
}
})

router.get('/friends/add/:friendCode', async (req, res) => {
try {
const friends = await UserService.addFriend(req.params.friendCode, req.userId)

res.json(friends.map(t => t.username))
} catch (error) {
console.log(error)
res.json({ message: error.message })
}
})

router.get('/friends', async (req, res) => {
try {
const friends = await UserService.getUserFriends(req.userId)

res.json(friends.map(t => t.username))
} catch (error) {
console.log(error)
res.json({ message: error.message })
}
})


module.exports = router;
35 changes: 34 additions & 1 deletion src/services/UserService.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ class UserService {

static async query(id) {
try {
const users = await User.findByPk(id, { attributes: { exclude: ['password', 'updatedAt'] } })
const users = await User.findByPk(id, {
include: {
model: User,
as: 'friends',
attributes: { exclude: ['password', 'id', 'updatedAt'] } // Exclude the 'password' attribute
}, attributes: { exclude: ['password', 'updatedAt'] }
})

return users;
} catch (error) {
Expand All @@ -31,6 +37,33 @@ class UserService {
}
}

static async addFriend(friendCode, userId) {
try {
const user1 = await User.findByPk(userId)
const user2 = await User.findOne({ where: { friend_code: friendCode } })

await user1.addFriend(user2)
const friends = await user1.getFriends()

return friends
} catch (error) {
console.log(error)
throw new Error(error.message)
}
}

static async getUserFriends(userId) {
try {
const user = await User.findByPk(userId)
const friends = await user.getFriends()

return friends
} catch (error) {
console.log(error)
throw new Error(error.message)
}
}

static async getUserTeams(userId) {
try {
const user = await User.findByPk(userId);
Expand Down

0 comments on commit 814c70d

Please sign in to comment.