From 9067d3c984bba730969b14514e014c569bcd1272 Mon Sep 17 00:00:00 2001 From: feserr Date: Thu, 5 Oct 2023 01:11:26 +0100 Subject: [PATCH] Add and use spotify display name. --- db/User.js | 4 ++++ src/controllers/trackController.js | 3 +++ src/controllers/userController.js | 12 +++++++----- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/db/User.js b/db/User.js index a961a8d..02494ec 100644 --- a/db/User.js +++ b/db/User.js @@ -11,6 +11,10 @@ const User = sequelize.define('user', { type: Sequelize.STRING, allowNull: false, }, + spotifyDisplayName: { + type: Sequelize.STRING, + allowNull: false, + }, }); module.exports = { User }; diff --git a/src/controllers/trackController.js b/src/controllers/trackController.js index 059e25e..0cbe1ce 100644 --- a/src/controllers/trackController.js +++ b/src/controllers/trackController.js @@ -24,6 +24,7 @@ const trackController = { artist: track.artist, spotifyTrackId: track.spotifyTrackId, spotifyUserId: user.spotifyUserId, + spotifyDisplayName: user.spotifyDisplayName, likes: userLikes, }; }), @@ -47,6 +48,7 @@ const trackController = { return res.status(200).send({ trackInfo: { spotifyUserId: '', + spotifyDisplayName: '', likes: [], }, }); @@ -63,6 +65,7 @@ const trackController = { res.send({ trackInfo: { spotifyUserId: user.spotifyUserId, + spotifyDisplayName: user.spotifyDisplayName, likes: userLikes, }, }); diff --git a/src/controllers/userController.js b/src/controllers/userController.js index e038954..0a9a634 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -4,11 +4,12 @@ const userController = { register: async (req, res) => { try { const { spotifyUserId } = req.params; - const user = await User.findOne({ where: { spotifyUserId } }); - if (user) return res.status(204).send({ message: 'User already exist' }); - - User.create({ spotifyUserId }); - res.send({ message: 'User created' }); + const { spotifyDisplayName } = req.body; + await User.findOne({ where: { spotifyUserId } }).then((obj) => { + if (obj) return obj.update({ spotifyUserId, spotifyDisplayName }); + return User.create({ spotifyUserId, spotifyDisplayName }); + }); + res.send({ message: 'Success' }); } catch (error) { global.logger.error(error.message); res.status(500).send({ message: 'Failed register the user' }); @@ -73,6 +74,7 @@ const userController = { artist: track.artist, spotifyTrackId: track.spotifyTrackId, spotifyUserId: user.spotifyUserId, + spotifyDisplayName: user.spotifyDisplayName, likes: userLikes, }; }),