From 814c70d8acb8891a1bb1fdab966b7e88576ef23b Mon Sep 17 00:00:00 2001 From: sord-dev <75338985+sord-dev@users.noreply.github.com> Date: Tue, 8 Aug 2023 19:59:45 +0100 Subject: [PATCH] adding user friend and friend code system --- src/models/UserModel.js | 7 +++++++ src/routes/UserRouter.js | 26 +++++++++++++++++++++++++- src/services/UserService.js | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/models/UserModel.js b/src/models/UserModel.js index ef8f0b2..c432cf1 100644 --- a/src/models/UserModel.js +++ b/src/models/UserModel.js @@ -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' }); diff --git a/src/routes/UserRouter.js b/src/routes/UserRouter.js index 608c122..12c7bde 100644 --- a/src/routes/UserRouter.js +++ b/src/routes/UserRouter.js @@ -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 }) @@ -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; \ No newline at end of file diff --git a/src/services/UserService.js b/src/services/UserService.js index 1c9af4e..a3885e8 100644 --- a/src/services/UserService.js +++ b/src/services/UserService.js @@ -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) { @@ -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);