also improves modal requests by not waiting for result from radarr/sonarrpull/42/head
parent
7287a6a957
commit
06dc606bcf
@ -0,0 +1,71 @@
|
||||
import { Router } from 'express';
|
||||
import { getRepository, FindOperator } from 'typeorm';
|
||||
import Media from '../entity/Media';
|
||||
import { MediaStatus } from '../constants/media';
|
||||
|
||||
export interface MediaResultsResponse {
|
||||
pageInfo: {
|
||||
pages: number;
|
||||
page: number;
|
||||
results: number;
|
||||
pageSize: number;
|
||||
};
|
||||
results: Media[];
|
||||
}
|
||||
|
||||
const mediaRoutes = Router();
|
||||
|
||||
mediaRoutes.get('/', async (req, res, next) => {
|
||||
const mediaRepository = getRepository(Media);
|
||||
|
||||
const pageSize = Number(req.query.take) ?? 20;
|
||||
const skip = Number(req.query.skip) ?? 0;
|
||||
|
||||
let statusFilter:
|
||||
| MediaStatus
|
||||
| FindOperator<MediaStatus>
|
||||
| undefined = undefined;
|
||||
|
||||
switch (req.query.filter) {
|
||||
case 'available':
|
||||
statusFilter = MediaStatus.AVAILABLE;
|
||||
break;
|
||||
case 'partial':
|
||||
statusFilter = MediaStatus.PARTIALLY_AVAILABLE;
|
||||
break;
|
||||
case 'processing':
|
||||
statusFilter = MediaStatus.PROCESSING;
|
||||
break;
|
||||
case 'pending':
|
||||
statusFilter = MediaStatus.PENDING;
|
||||
break;
|
||||
default:
|
||||
statusFilter = undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
const [media, mediaCount] = await mediaRepository.findAndCount({
|
||||
order: {
|
||||
id: 'DESC',
|
||||
},
|
||||
where: {
|
||||
status: statusFilter,
|
||||
},
|
||||
take: pageSize,
|
||||
skip,
|
||||
});
|
||||
return res.status(200).json({
|
||||
pageInfo: {
|
||||
pages: Math.ceil(mediaCount / pageSize),
|
||||
pageSize,
|
||||
results: mediaCount,
|
||||
page: Math.ceil(skip / pageSize) + 1,
|
||||
},
|
||||
results: media,
|
||||
} as MediaResultsResponse);
|
||||
} catch (e) {
|
||||
next({ status: 500, message: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
export default mediaRoutes;
|
Loading…
Reference in new issue