mirror of
https://github.com/fallenbagel/jellyseerr.git
synced 2025-12-24 02:39:18 -05:00
74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
import {
|
|
DuplicateWatchlistRequestError,
|
|
NotFoundError,
|
|
Watchlist,
|
|
} from '@server/entity/Watchlist';
|
|
import logger from '@server/logger';
|
|
import { Router } from 'express';
|
|
import { QueryFailedError } from 'typeorm';
|
|
|
|
import { watchlistCreate } from '@server/interfaces/api/watchlistCreate';
|
|
|
|
const watchlistRoutes = Router();
|
|
|
|
watchlistRoutes.post<never, Watchlist, Watchlist>(
|
|
'/',
|
|
async (req, res, next) => {
|
|
try {
|
|
if (!req.user) {
|
|
return next({
|
|
status: 401,
|
|
message: 'You must be logged in to add watchlist.',
|
|
});
|
|
}
|
|
const values = watchlistCreate.parse(req.body);
|
|
|
|
const request = await Watchlist.createWatchlist({
|
|
watchlistRequest: values,
|
|
user: req.user,
|
|
});
|
|
return res.status(201).json(request);
|
|
} catch (error) {
|
|
if (!(error instanceof Error)) {
|
|
return;
|
|
}
|
|
switch (error.constructor) {
|
|
case QueryFailedError:
|
|
logger.warn('Something wrong with data watchlist', {
|
|
tmdbId: req.body.tmdbId,
|
|
mediaType: req.body.mediaType,
|
|
label: 'Watchlist',
|
|
});
|
|
return next({ status: 409, message: 'Something wrong' });
|
|
case DuplicateWatchlistRequestError:
|
|
return next({ status: 409, message: error.message });
|
|
default:
|
|
return next({ status: 500, message: error.message });
|
|
}
|
|
}
|
|
}
|
|
);
|
|
|
|
watchlistRoutes.delete('/:tmdbId', async (req, res, next) => {
|
|
if (!req.user) {
|
|
return next({
|
|
status: 401,
|
|
message: 'You must be logged in to delete watchlist data.',
|
|
});
|
|
}
|
|
try {
|
|
await Watchlist.deleteWatchlist(Number(req.params.tmdbId), req.user);
|
|
return res.status(204).send();
|
|
} catch (e) {
|
|
if (e instanceof NotFoundError) {
|
|
return next({
|
|
status: 401,
|
|
message: e.message,
|
|
});
|
|
}
|
|
return next({ status: 500, message: e.message });
|
|
}
|
|
});
|
|
|
|
export default watchlistRoutes;
|