Skip to content

Commit

Permalink
Add and use spotify display name.
Browse files Browse the repository at this point in the history
  • Loading branch information
feserr committed Oct 5, 2023
1 parent e4938ca commit 9067d3c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
4 changes: 4 additions & 0 deletions db/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const User = sequelize.define('user', {
type: Sequelize.STRING,
allowNull: false,
},
spotifyDisplayName: {
type: Sequelize.STRING,
allowNull: false,
},
});

module.exports = { User };
3 changes: 3 additions & 0 deletions src/controllers/trackController.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const trackController = {
artist: track.artist,
spotifyTrackId: track.spotifyTrackId,
spotifyUserId: user.spotifyUserId,
spotifyDisplayName: user.spotifyDisplayName,
likes: userLikes,
};
}),
Expand All @@ -47,6 +48,7 @@ const trackController = {
return res.status(200).send({
trackInfo: {
spotifyUserId: '',
spotifyDisplayName: '',
likes: [],
},
});
Expand All @@ -63,6 +65,7 @@ const trackController = {
res.send({
trackInfo: {
spotifyUserId: user.spotifyUserId,
spotifyDisplayName: user.spotifyDisplayName,
likes: userLikes,
},
});
Expand Down
12 changes: 7 additions & 5 deletions src/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down Expand Up @@ -73,6 +74,7 @@ const userController = {
artist: track.artist,
spotifyTrackId: track.spotifyTrackId,
spotifyUserId: user.spotifyUserId,
spotifyDisplayName: user.spotifyDisplayName,
likes: userLikes,
};
}),
Expand Down

0 comments on commit 9067d3c

Please sign in to comment.