mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2025-12-06 09:44:06 +00:00
added playlist name in storage
This commit is contained in:
parent
7074009fab
commit
a634ea0fb2
@ -7,7 +7,7 @@ const typedefs = require("../typedefs");
|
|||||||
const userPlaylists = require("../models").userPlaylists;
|
const userPlaylists = require("../models").userPlaylists;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store user's playlists
|
* Sync user's stored playlists
|
||||||
* @param {typedefs.Req} req
|
* @param {typedefs.Req} req
|
||||||
* @param {typedefs.Res} res
|
* @param {typedefs.Res} res
|
||||||
*/
|
*/
|
||||||
@ -33,7 +33,12 @@ const updateUser = async (req, res) => {
|
|||||||
if (response.status >= 400 && response.status < 500)
|
if (response.status >= 400 && response.status < 500)
|
||||||
return res.status(response.status).send(response.data);
|
return res.status(response.status).send(response.data);
|
||||||
|
|
||||||
currentPlaylists = response.data.items.map(playlist => parseSpotifyUri(playlist.uri).id);
|
currentPlaylists = response.data.items.map(playlist => {
|
||||||
|
return {
|
||||||
|
playlistID: parseSpotifyUri(playlist.uri).id,
|
||||||
|
playlistName: playlist.name
|
||||||
|
}
|
||||||
|
});
|
||||||
nextURL = response.data.next;
|
nextURL = response.data.next;
|
||||||
|
|
||||||
// keep getting batches of 50 till exhausted
|
// keep getting batches of 50 till exhausted
|
||||||
@ -50,7 +55,12 @@ const updateUser = async (req, res) => {
|
|||||||
return res.status(response.status).send(response.data);
|
return res.status(response.status).send(response.data);
|
||||||
|
|
||||||
currentPlaylists.push(
|
currentPlaylists.push(
|
||||||
...nextResponse.data.items.map(playlist => parseSpotifyUri(playlist.uri).id)
|
...nextResponse.data.items.map(playlist => {
|
||||||
|
return {
|
||||||
|
playlistID: parseSpotifyUri(playlist.uri).id,
|
||||||
|
playlistName: playlist.name
|
||||||
|
}
|
||||||
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
nextURL = nextResponse.data.next;
|
nextURL = nextResponse.data.next;
|
||||||
@ -58,7 +68,7 @@ const updateUser = async (req, res) => {
|
|||||||
|
|
||||||
|
|
||||||
let oldPlaylists = await userPlaylists.findAll({
|
let oldPlaylists = await userPlaylists.findAll({
|
||||||
attributes: ["playlistID"],
|
attributes: ["playlistID", "playlistName"],
|
||||||
raw: true,
|
raw: true,
|
||||||
where: {
|
where: {
|
||||||
userID: userURI.id
|
userID: userURI.id
|
||||||
@ -68,12 +78,11 @@ const updateUser = async (req, res) => {
|
|||||||
let toRemove, toAdd;
|
let toRemove, toAdd;
|
||||||
if (oldPlaylists.length) {
|
if (oldPlaylists.length) {
|
||||||
// existing user
|
// existing user
|
||||||
oldPlaylists = oldPlaylists.map(pl => pl.playlistID);
|
const currentSet = new Set(currentPlaylists.map(pl => pl.playlistID));
|
||||||
const currentSet = new Set(currentPlaylists);
|
const oldSet = new Set(oldPlaylists.map(pl => pl.playlistID));
|
||||||
const oldSet = new Set(oldPlaylists);
|
|
||||||
|
|
||||||
toAdd = currentPlaylists.filter(current => !oldSet.has(current));
|
toAdd = currentPlaylists.filter(current => !oldSet.has(current.playlistID));
|
||||||
toRemove = oldPlaylists.filter(old => !currentSet.has(old));
|
toRemove = oldPlaylists.filter(old => !currentSet.has(old.playlistID));
|
||||||
} else {
|
} else {
|
||||||
// new user
|
// new user
|
||||||
toAdd = currentPlaylists;
|
toAdd = currentPlaylists;
|
||||||
@ -82,7 +91,7 @@ const updateUser = async (req, res) => {
|
|||||||
|
|
||||||
if (toRemove.length) {
|
if (toRemove.length) {
|
||||||
const cleanedUser = await userPlaylists.destroy({
|
const cleanedUser = await userPlaylists.destroy({
|
||||||
where: { playlistID: toRemove }
|
where: { playlistID: toRemove.map(pl => pl.playlistID) }
|
||||||
});
|
});
|
||||||
if (cleanedUser !== toRemove.length) {
|
if (cleanedUser !== toRemove.length) {
|
||||||
logger.error("Could not remove old playlists", { error: new Error("model.destroy failed?") });
|
logger.error("Could not remove old playlists", { error: new Error("model.destroy failed?") });
|
||||||
@ -92,7 +101,7 @@ const updateUser = async (req, res) => {
|
|||||||
|
|
||||||
if (toAdd.length) {
|
if (toAdd.length) {
|
||||||
const updatedUser = await userPlaylists.bulkCreate(
|
const updatedUser = await userPlaylists.bulkCreate(
|
||||||
toAdd.map((pl) => { return { playlistID: pl, userID: userURI.id } }),
|
toAdd.map(pl => { return { ...pl, userID: userURI.id } }),
|
||||||
{ validate: true }
|
{ validate: true }
|
||||||
);
|
);
|
||||||
if (updatedUser.length !== toAdd.length) {
|
if (updatedUser.length !== toAdd.length) {
|
||||||
@ -108,6 +117,31 @@ const updateUser = async (req, res) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch user's stored playlists
|
||||||
|
* @param {typedefs.Req} req
|
||||||
|
* @param {typedefs.Res} res
|
||||||
|
*/
|
||||||
|
const fetchUser = async (req, res) => {
|
||||||
|
try {
|
||||||
|
const userURI = parseSpotifyUri(req.session.user.uri);
|
||||||
|
|
||||||
|
let currentPlaylists = await userPlaylists.findAll({
|
||||||
|
attributes: ["playlistID", "playlistName"],
|
||||||
|
raw: true,
|
||||||
|
where: {
|
||||||
|
userID: userURI.id
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(200).send(currentPlaylists);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error('fetchUser', { error });
|
||||||
|
return res.sendStatus(500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
updateUser
|
updateUser,
|
||||||
|
fetchUser
|
||||||
};
|
};
|
||||||
|
|||||||
@ -12,6 +12,9 @@ module.exports = {
|
|||||||
playlistID: {
|
playlistID: {
|
||||||
type: Sequelize.STRING
|
type: Sequelize.STRING
|
||||||
},
|
},
|
||||||
|
playlistName: {
|
||||||
|
type: Sequelize.STRING
|
||||||
|
},
|
||||||
userID: {
|
userID: {
|
||||||
type: Sequelize.STRING
|
type: Sequelize.STRING
|
||||||
},
|
},
|
||||||
|
|||||||
@ -15,6 +15,7 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
}
|
}
|
||||||
userPlaylists.init({
|
userPlaylists.init({
|
||||||
playlistID: DataTypes.STRING,
|
playlistID: DataTypes.STRING,
|
||||||
|
playlistName: DataTypes.STRING,
|
||||||
userID: DataTypes.STRING
|
userID: DataTypes.STRING
|
||||||
}, {
|
}, {
|
||||||
sequelize,
|
sequelize,
|
||||||
|
|||||||
@ -1,12 +1,18 @@
|
|||||||
const router = require('express').Router();
|
const router = require('express').Router();
|
||||||
|
|
||||||
const { updateUser } = require('../controllers/operations');
|
const { updateUser, fetchUser } = require('../controllers/operations');
|
||||||
const { isAuthenticated } = require('../middleware/authCheck');
|
const { isAuthenticated } = require('../middleware/authCheck');
|
||||||
|
|
||||||
router.post(
|
router.put(
|
||||||
"/update",
|
"/update",
|
||||||
isAuthenticated,
|
isAuthenticated,
|
||||||
updateUser
|
updateUser
|
||||||
);
|
);
|
||||||
|
|
||||||
|
router.get(
|
||||||
|
"/fetch",
|
||||||
|
isAuthenticated,
|
||||||
|
fetchUser
|
||||||
|
);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user