133 lines
4.2 KiB
TypeScript
133 lines
4.2 KiB
TypeScript
import {add, clear, get, history, insertAt, list, move, processing, remove, retry} from "../models/queue.models";
|
|
import {ProcessingQueue, Queue, QueueItem} from "../types/queue";
|
|
import {Request, Response} from "express";
|
|
|
|
export function listQueue(_req: Request, res: Response): void {
|
|
try {
|
|
const resp: Queue = list();
|
|
res.status(200).json(resp);
|
|
} catch (err) {
|
|
res.status(500).send(err instanceof Error ? err.message : 'Unknown error');
|
|
}
|
|
}
|
|
|
|
export function getQueueItem(req: Request, res: Response): void {
|
|
try {
|
|
const index = parseInt(req.params.index, 10);
|
|
if (isNaN(index)) {
|
|
res.status(400).send('Invalid index');
|
|
return;
|
|
}
|
|
const resp: QueueItem = get(index);
|
|
res.status(200).json(resp);
|
|
} catch (err) {
|
|
res.status(500).send(err instanceof Error ? err.message : 'Unknown error');
|
|
}
|
|
}
|
|
|
|
export async function addQueueItem(req: Request, res: Response): Promise<void> {
|
|
try {
|
|
const song: string = req.body.song;
|
|
if (!song) {
|
|
res.status(400).send('No song provided');
|
|
return;
|
|
}
|
|
const resp: QueueItem = await add(song);
|
|
res.status(201).json(resp); // Created
|
|
} catch (err) {
|
|
res.status(500).send(err instanceof Error ? err.message : 'Unknown error');
|
|
}
|
|
}
|
|
|
|
export async function insertQueueItem(req: Request, res: Response): Promise<void> {
|
|
try {
|
|
const song: string = req.body.song;
|
|
if (!song) {
|
|
res.status(400).send('No song provided');
|
|
return;
|
|
}
|
|
const index: number = parseInt(req.params.index, 10);
|
|
if (isNaN(index)) {
|
|
res.status(400).send('Invalid index');
|
|
return;
|
|
}
|
|
const resp: QueueItem = await insertAt(song, index);
|
|
res.status(201).json(resp); // Created
|
|
} catch (err) {
|
|
res.status(500).send(err instanceof Error ? err.message : 'Unknown error');
|
|
}
|
|
}
|
|
|
|
export async function removeQueueItem(req: Request, res: Response): Promise<void> {
|
|
try {
|
|
const song: string = req.params.song;
|
|
if (!song) {
|
|
res.status(400).send('No song provided');
|
|
return;
|
|
}
|
|
const resp: QueueItem = await remove(song);
|
|
res.status(205).json(resp); // Client should reset content.
|
|
} catch (err) {
|
|
res.status(500).send(err instanceof Error ? err.message : 'Unknown error');
|
|
}
|
|
}
|
|
|
|
export function clearQueue(_req: Request, res: Response): void {
|
|
try {
|
|
clear();
|
|
res.status(205).send(); // Client should reset content.
|
|
} catch (err) {
|
|
res.status(500).send(err instanceof Error ? err.message : 'Unknown error');
|
|
}
|
|
}
|
|
|
|
export function moveQueueItem(req: Request, res: Response): void {
|
|
try {
|
|
const song: string = req.body.song;
|
|
if (!song) {
|
|
res.status(400).send('No song provided');
|
|
return;
|
|
}
|
|
const to: number = parseInt(req.body.to, 10);
|
|
if (isNaN(to)) {
|
|
res.status(400).send('Invalid index');
|
|
return;
|
|
}
|
|
move(song, to);
|
|
res.status(205).send(); // Client should reset content.
|
|
} catch (err) {
|
|
res.status(500).send(err instanceof Error ? err.message : 'Unknown error');
|
|
}
|
|
}
|
|
|
|
export function retryQueueItem(req: Request, res: Response): void {
|
|
try {
|
|
const song: string = req.params.song;
|
|
if (!song) {
|
|
res.status(400).send('No song provided');
|
|
return;
|
|
}
|
|
retry(song);
|
|
res.status(205).send(); // Client should reset content.
|
|
} catch (err) {
|
|
res.status(500).send(err instanceof Error ? err.message : 'Unknown error');
|
|
}
|
|
}
|
|
|
|
export function getProcessing(_req: Request, res: Response): void {
|
|
try {
|
|
const resp: ProcessingQueue = processing();
|
|
res.status(200).json(resp);
|
|
} catch (err) {
|
|
res.status(500).send(err instanceof Error ? err.message : 'Unknown error');
|
|
}
|
|
}
|
|
|
|
export function getHistory(_req: Request, res: Response): void {
|
|
try {
|
|
const resp: Queue = history();
|
|
res.status(200).json(resp);
|
|
} catch (err) {
|
|
res.status(500).send(err instanceof Error ? err.message : 'Unknown error');
|
|
}
|
|
}
|