51 lines
1.4 KiB
JavaScript
51 lines
1.4 KiB
JavaScript
function slugify(value) {
|
|
return String(value || '')
|
|
.toLowerCase()
|
|
.trim()
|
|
.replace(/[^a-z0-9]+/g, '-')
|
|
.replace(/^-+|-+$/g, '')
|
|
.replace(/-{2,}/g, '-');
|
|
}
|
|
|
|
async function uniqueScreenSlug(pool, baseSlug, excludeId) {
|
|
const start = baseSlug || `screen-${Date.now()}`;
|
|
let candidate = start;
|
|
let counter = 2;
|
|
while (true) {
|
|
const params = [candidate];
|
|
let sql = 'SELECT id FROM screens WHERE slug = ?';
|
|
if (excludeId !== undefined && excludeId !== null) {
|
|
sql += ' AND id <> ?';
|
|
params.push(excludeId);
|
|
}
|
|
const [rows] = await pool.query(sql, params);
|
|
if (!rows.length) {
|
|
return candidate;
|
|
}
|
|
candidate = `${start}-${counter}`;
|
|
counter += 1;
|
|
}
|
|
}
|
|
|
|
async function fetchScreenById(pool, id) {
|
|
const [rows] = await pool.query(`
|
|
SELECT s.id, s.name, s.slug, s.playlist_id, s.created_at, s.modified_at, s.created_by, s.modified_by, p.name AS playlist_name
|
|
FROM screens s
|
|
LEFT JOIN playlists p ON p.id = s.playlist_id
|
|
WHERE s.id = ?
|
|
`, [id]);
|
|
return rows[0] || null;
|
|
}
|
|
|
|
async function fetchScreenEditData(pool) {
|
|
const [playlists] = await pool.query('SELECT id, name, fade_between_slides, skip_unavailable_rtmp, created_at, modified_at, created_by, modified_by FROM playlists ORDER BY id DESC');
|
|
return { playlists };
|
|
}
|
|
|
|
module.exports = {
|
|
slugify,
|
|
uniqueScreenSlug,
|
|
fetchScreenById,
|
|
fetchScreenEditData
|
|
};
|