feat: add filtering for requests api

pull/179/head
sct 4 years ago
parent f5f2545520
commit cb9ae25d94

@ -1581,6 +1581,31 @@ paths:
Returns all requests if the user has the `ADMIN` or `MANAGE_REQUESTS` permissions. Otherwise, only the logged in users requests are returned. Returns all requests if the user has the `ADMIN` or `MANAGE_REQUESTS` permissions. Otherwise, only the logged in users requests are returned.
tags: tags:
- request - request
parameters:
- in: query
name: take
schema:
type: number
nullable: true
example: 20
- in: query
name: skip
schema:
type: number
nullable: true
example: 0
- in: query
name: filter
schema:
type: string
nullable: true
enum: [all, available, approved, pending, unavailable]
- in: query
name: sort
schema:
type: string
enum: [added, modified]
default: added
responses: responses:
'200': '200':
description: Requests returned description: Requests returned

@ -1,7 +1,7 @@
import { Router } from 'express'; import { Router } from 'express';
import { isAuthenticated } from '../middleware/auth'; import { isAuthenticated } from '../middleware/auth';
import { Permission } from '../lib/permissions'; import { Permission } from '../lib/permissions';
import { getRepository } from 'typeorm'; import { getRepository, FindOperator, FindOneOptions, In } from 'typeorm';
import { MediaRequest } from '../entity/MediaRequest'; import { MediaRequest } from '../entity/MediaRequest';
import TheMovieDb from '../api/themoviedb'; import TheMovieDb from '../api/themoviedb';
import Media from '../entity/Media'; import Media from '../entity/Media';
@ -14,21 +14,55 @@ const requestRoutes = Router();
requestRoutes.get('/', async (req, res, next) => { requestRoutes.get('/', async (req, res, next) => {
const requestRepository = getRepository(MediaRequest); const requestRepository = getRepository(MediaRequest);
try { try {
let statusFilter:
| MediaRequestStatus
| FindOperator<MediaRequestStatus>
| undefined = undefined;
switch (req.query.filter) {
case 'available':
statusFilter = MediaRequestStatus.AVAILABLE;
break;
case 'approved':
statusFilter = MediaRequestStatus.APPROVED;
break;
case 'pending':
statusFilter = MediaRequestStatus.PENDING;
break;
case 'unavailable':
statusFilter = In([
MediaRequestStatus.PENDING,
MediaRequestStatus.APPROVED,
]);
break;
default:
statusFilter = In(Object.values(MediaRequestStatus));
}
let sortFilter: FindOneOptions<MediaRequest>['order'] = {
id: 'DESC',
};
switch (req.query.sort) {
case 'modified':
sortFilter = {
updatedAt: 'DESC',
};
break;
}
const requests = req.user?.hasPermission(Permission.MANAGE_REQUESTS) const requests = req.user?.hasPermission(Permission.MANAGE_REQUESTS)
? await requestRepository.find({ ? await requestRepository.find({
order: { order: sortFilter,
id: 'DESC',
},
relations: ['media'], relations: ['media'],
where: { status: statusFilter },
take: Number(req.query.take) ?? 20, take: Number(req.query.take) ?? 20,
skip: Number(req.query.skip) ?? 0, skip: Number(req.query.skip) ?? 0,
}) })
: await requestRepository.find({ : await requestRepository.find({
where: { requestedBy: { id: req.user?.id } }, where: { requestedBy: { id: req.user?.id }, status: statusFilter },
relations: ['media'], relations: ['media'],
order: { order: sortFilter,
id: 'DESC',
},
take: Number(req.query.limit) ?? 20, take: Number(req.query.limit) ?? 20,
skip: Number(req.query.skip) ?? 0, skip: Number(req.query.skip) ?? 0,
}); });

@ -70,7 +70,7 @@ const Discover: React.FC = () => {
); );
const { data: requests, error: requestError } = useSWR<MediaRequest[]>( const { data: requests, error: requestError } = useSWR<MediaRequest[]>(
'/api/v1/request' '/api/v1/request?filter=unavailable&take=20&sort=modified&skip=0'
); );
return ( return (

Loading…
Cancel
Save