mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2025-12-06 07:54:07 +00:00
get playlist details (first 100 tracks)
This commit is contained in:
parent
3bb88977ec
commit
10e9020f31
@ -1,4 +1,4 @@
|
||||
const { authInstance } = require("../axios");
|
||||
const { authInstance } = require("../utils/axios");
|
||||
|
||||
const typedefs = require("../typedefs");
|
||||
const { scopes, stateKey, accountsAPIURL } = require('../constants');
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
const logger = require("../utils/logger")(module);
|
||||
|
||||
const typedefs = require("../typedefs");
|
||||
const { axiosInstance } = require('../axios');
|
||||
const { axiosInstance } = require('../utils/axios');
|
||||
|
||||
/**
|
||||
* Retrieve list of all of user's playlists
|
||||
@ -26,15 +26,12 @@ const getUserPlaylists = async (req, res) => {
|
||||
}
|
||||
);
|
||||
|
||||
/** @type {typedefs.SimplifiedPlaylist[]} */
|
||||
playlists.items = response.data.items.map((playlist) => {
|
||||
return {
|
||||
name: playlist.name,
|
||||
description: playlist.description,
|
||||
owner: playlist.owner.display_name,
|
||||
images: playlist.images.map((image) => image.url),
|
||||
link: playlist.external_urls.spotify,
|
||||
collaborative: playlist.collaborative,
|
||||
public: playlist.public,
|
||||
id: playlist.id,
|
||||
}
|
||||
});
|
||||
@ -59,10 +56,6 @@ const getUserPlaylists = async (req, res) => {
|
||||
name: playlist.name,
|
||||
description: playlist.description,
|
||||
owner: playlist.owner.display_name,
|
||||
images: playlist.images.map((image) => image.url),
|
||||
link: playlist.external_urls.spotify,
|
||||
collaborative: playlist.collaborative,
|
||||
public: playlist.public,
|
||||
id: playlist.id,
|
||||
}
|
||||
})
|
||||
@ -73,11 +66,55 @@ const getUserPlaylists = async (req, res) => {
|
||||
|
||||
return res.status(200).send(playlists);
|
||||
} catch (error) {
|
||||
logger.error('Error', { error });
|
||||
logger.error('getUserPlaylists', { error });
|
||||
return res.status(500).send({ message: "Server Error. Try again." });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve single playlist
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
const getUserPlaylist = async (req, res) => {
|
||||
try {
|
||||
/** @type {typedefs.Playlist} */
|
||||
let playlist = {};
|
||||
|
||||
const response = await axiosInstance.get(
|
||||
"/playlists/" + req.query.playlist_id,
|
||||
{
|
||||
headers: { ...req.authHeader }
|
||||
}
|
||||
);
|
||||
|
||||
// TODO: this whole section needs to be DRYer
|
||||
playlist.uri = response.data.uri
|
||||
playlist.name = response.data.name
|
||||
playlist.description = response.data.description
|
||||
let { display_name, uri, id, ...rest } = response.data.owner
|
||||
playlist.owner = { display_name, uri, id }
|
||||
playlist.followers = response.data.followers.total
|
||||
playlist.tracks = response.data.tracks.items.map((playlist_track) => {
|
||||
return {
|
||||
added_at: playlist_track.added_at,
|
||||
track: {
|
||||
uri: playlist_track.track.uri,
|
||||
name: playlist_track.track.name,
|
||||
artists: playlist_track.track.artists.map((artist) => { return { name: artist.name } }),
|
||||
album: { name: playlist_track.track.album.name },
|
||||
is_local: playlist_track.track.is_local,
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return res.status(200).send(playlist);
|
||||
} catch (error) {
|
||||
logger.error('getUserPlaylist', { error });
|
||||
return res.status(500).send({ message: "Server Error. Try again." });
|
||||
}
|
||||
}
|
||||
module.exports = {
|
||||
getUserPlaylists
|
||||
getUserPlaylists,
|
||||
getUserPlaylist,
|
||||
};
|
||||
@ -1,6 +1,6 @@
|
||||
const router = require('express').Router();
|
||||
|
||||
const { getUserPlaylists } = require('../controllers/playlists');
|
||||
const { getUserPlaylists, getUserPlaylist } = require('../controllers/playlists');
|
||||
const { isAuthenticated } = require('../middleware/authCheck');
|
||||
const validator = require("../validators");
|
||||
|
||||
@ -10,5 +10,11 @@ router.get(
|
||||
validator.validate,
|
||||
getUserPlaylists
|
||||
);
|
||||
router.get(
|
||||
"/details",
|
||||
isAuthenticated,
|
||||
validator.validate,
|
||||
getUserPlaylist
|
||||
);
|
||||
|
||||
module.exports = router;
|
||||
|
||||
45
typedefs.js
45
typedefs.js
@ -6,6 +6,51 @@
|
||||
* @typedef {import('express').NextFunction} Next
|
||||
*
|
||||
* @typedef {import('winston').Logger} Logger
|
||||
*
|
||||
* @typedef {{
|
||||
* display_name: string,
|
||||
* uri: string,
|
||||
* id: string
|
||||
* }} PlaylistOwner
|
||||
*
|
||||
* @typedef {{
|
||||
* name: string,
|
||||
* description: string,
|
||||
* owner: PlaylistOwner,
|
||||
* id: string,
|
||||
* }} SimplifiedPlaylist
|
||||
*
|
||||
* @typedef {{
|
||||
* name: string
|
||||
* }} Album
|
||||
*
|
||||
* @typedef {{
|
||||
* name: string
|
||||
* }} Artist
|
||||
*
|
||||
* @typedef {{
|
||||
* uri: string,
|
||||
* name: string,
|
||||
* artists: Artist[]
|
||||
* album: Album,
|
||||
* is_local: boolean,
|
||||
* }} Track
|
||||
*
|
||||
* @typedef {{
|
||||
* added_at: string,
|
||||
* track: Track,
|
||||
* }} PlaylistTrack
|
||||
*
|
||||
* @typedef {{
|
||||
* uri: string,
|
||||
* name: string,
|
||||
* description: string,
|
||||
* owner: PlaylistOwner,
|
||||
* followers: {
|
||||
* total: number
|
||||
* },
|
||||
* tracks: PlaylistTrack[],
|
||||
* }} Playlist
|
||||
*/
|
||||
|
||||
exports.unused = {};
|
||||
@ -1,7 +1,7 @@
|
||||
const axios = require('axios');
|
||||
|
||||
const { baseAPIURL, accountsAPIURL } = require("./constants");
|
||||
const logger = require('./utils/logger')(module);
|
||||
const { baseAPIURL, accountsAPIURL } = require("../constants");
|
||||
const logger = require('./logger')(module);
|
||||
|
||||
const authInstance = axios.default.create({
|
||||
baseURL: accountsAPIURL,
|
||||
Loading…
x
Reference in New Issue
Block a user