92 lines
4.1 KiB
JavaScript
92 lines
4.1 KiB
JavaScript
// Playlist route registration and pagination wiring.
|
|
|
|
module.exports = function registerPlaylistsRoutes(app, deps) {
|
|
const pool = deps.pool;
|
|
const common = deps.common;
|
|
const pages = deps.pages;
|
|
const fetchScreensByPlaylistId = deps.fetchScreensByPlaylistId;
|
|
const { buildPagination } = require('../../../lib/pagination');
|
|
const requirePermission = deps.requirePermission;
|
|
const { buildDuplicatePlaylistName, buildDuplicatePlaylist } = require('./duplicate');
|
|
|
|
const LIST_PAGE_SIZE = 25;
|
|
|
|
function requireQueryPermission(readPermissionKey, editPermissionKey) {
|
|
return function (req, res, next) {
|
|
const permissionKey = req.query && req.query.edit ? editPermissionKey : readPermissionKey;
|
|
return requirePermission(permissionKey)(req, res, next);
|
|
};
|
|
}
|
|
|
|
app.get('/playlists', requireQueryPermission('playlists.read', 'playlists.update'), async function (req, res, next) {
|
|
try {
|
|
const page = Math.max(1, Math.floor(Number(req.query.page) || 1));
|
|
const search = common.getSearchQuery(req);
|
|
const sort = common.getSortQuery(req);
|
|
const direction = common.getSortDirectionQuery(req);
|
|
if (req.query.edit) {
|
|
const data = await common.fetchAdminData(pool);
|
|
const playlist = await common.fetchPlaylistById(pool, Number(req.query.edit));
|
|
if (!playlist) {
|
|
return res.status(404).send('Playlist not found');
|
|
}
|
|
playlist.inUse = (await fetchScreensByPlaylistId(pool, playlist.id)).length > 0;
|
|
return res.send(pages.renderPlaylistEditPage(playlist, data, req.query.message ? String(req.query.message) : '', req.currentUser));
|
|
}
|
|
const data = await common.fetchPlaylistsPage(pool, page, LIST_PAGE_SIZE, search, sort, direction);
|
|
res.send(pages.renderPlaylistsPage({
|
|
playlists: data.playlists || [],
|
|
pagination: buildPagination(data.totalItems, data.currentPage, 'page', { search: search, sort: sort, direction: direction }, LIST_PAGE_SIZE, 'playlists', 'Playlist pages')
|
|
}, req.query.message ? String(req.query.message) : '', req.currentUser));
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
app.get('/playlists/new', requirePermission('playlists.create'), function (req, res) {
|
|
(async function () {
|
|
const data = await common.fetchAdminData(pool);
|
|
res.send(await pages.renderPlaylistFormPage(req.query.message ? String(req.query.message) : '', req.currentUser, data, pool));
|
|
})().catch(function (error) {
|
|
res.status(500).send(error.message || 'Unable to load page.');
|
|
});
|
|
});
|
|
|
|
app.get('/playlists/:id/duplicate', requirePermission('playlists.read'), requirePermission('playlists.create'), async function (req, res, next) {
|
|
try {
|
|
const playlist = await common.fetchPlaylistById(pool, Number(req.params.id));
|
|
if (!playlist) {
|
|
return res.status(404).send('Playlist not found');
|
|
}
|
|
|
|
const data = await common.fetchAdminData(pool);
|
|
let duplicateName = buildDuplicatePlaylistName(playlist.name);
|
|
let duplicateIndex = 2;
|
|
while (await common.fetchDuplicateName(pool, 'c_playlists', duplicateName)) {
|
|
duplicateName = buildDuplicatePlaylistName(playlist.name) + ' (' + duplicateIndex + ')';
|
|
duplicateIndex += 1;
|
|
}
|
|
|
|
const duplicatePlaylist = buildDuplicatePlaylist(playlist, duplicateName);
|
|
res.send(await pages.renderPlaylistFormPage(duplicatePlaylist, req.query.message ? String(req.query.message) : 'Review the copied values and save when ready.', req.currentUser, data, pool));
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
app.get('/playlists/:id/edit', requirePermission('playlists.update'), async function (req, res, next) {
|
|
try {
|
|
const playlist = await common.fetchPlaylistById(pool, Number(req.params.id));
|
|
if (!playlist) {
|
|
return res.status(404).send('Playlist not found');
|
|
}
|
|
const data = await common.fetchAdminData(pool);
|
|
playlist.inUse = (await fetchScreensByPlaylistId(pool, playlist.id)).length > 0;
|
|
res.send(pages.renderPlaylistEditPage(playlist, data, req.query.message ? String(req.query.message) : '', req.currentUser));
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
};
|
|
|