Implemented PagedResponse
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { orm } from '../orm/orm';
|
import { orm } from '../orm/orm';
|
||||||
import { UnwrappedRequest } from '../utilities/guard';
|
import { UnwrappedRequest } from '../utilities/guard';
|
||||||
import { CreatedResponse, ErrorResponse, OkResponse } from '../utilities/responseHelper';
|
import { CreatedResponse, ErrorResponse, OkResponse, PagedResponse } from '../utilities/responseHelper';
|
||||||
import {
|
import {
|
||||||
GameToCollectionRequest,
|
GameToCollectionRequest,
|
||||||
CreateCollectionRequest,
|
CreateCollectionRequest,
|
||||||
@@ -27,7 +27,7 @@ async function get(request: UnwrappedRequest): Promise<Response> {
|
|||||||
|
|
||||||
async function list(request: UnwrappedRequest): Promise<Response> {
|
async function list(request: UnwrappedRequest): Promise<Response> {
|
||||||
try {
|
try {
|
||||||
return new OkResponse(await orm.collections.list(request.claims));
|
return new PagedResponse(request, await orm.collections.list(request.claims));
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return new ErrorResponse(error as Error);
|
return new ErrorResponse(error as Error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { orm } from '../orm/orm';
|
import { orm } from '../orm/orm';
|
||||||
import { UnwrappedRequest } from '../utilities/guard';
|
import { UnwrappedRequest } from '../utilities/guard';
|
||||||
import { CreatedResponse, ErrorResponse, OkResponse } from '../utilities/responseHelper';
|
import { CreatedResponse, ErrorResponse, OkResponse, PagedResponse } from '../utilities/responseHelper';
|
||||||
import {
|
import {
|
||||||
CreateGameRequest,
|
CreateGameRequest,
|
||||||
UpdateGameRequest,
|
UpdateGameRequest,
|
||||||
@@ -55,7 +55,7 @@ async function drop(request: UnwrappedRequest): Promise<Response> {
|
|||||||
|
|
||||||
async function query(request: UnwrappedRequest): Promise<Response> {
|
async function query(request: UnwrappedRequest): Promise<Response> {
|
||||||
try {
|
try {
|
||||||
return new OkResponse(await orm.games.query(request.params.query));
|
return new PagedResponse(request, await orm.games.query(request.params.query));
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return new ErrorResponse(error as Error);
|
return new ErrorResponse(error as Error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { orm } from '../orm/orm';
|
import { orm } from '../orm/orm';
|
||||||
import { UnwrappedRequest } from '../utilities/guard';
|
import { UnwrappedRequest } from '../utilities/guard';
|
||||||
import { CreatedResponse, ErrorResponse, OkResponse } from '../utilities/responseHelper';
|
import { CreatedResponse, ErrorResponse, OkResponse, PagedResponse } from '../utilities/responseHelper';
|
||||||
import { CreatePlayerRequest, UpdatePlayerRequest } from '../utilities/requestModels';
|
import { CreatePlayerRequest, UpdatePlayerRequest } from '../utilities/requestModels';
|
||||||
import { PlayerId } from '../utilities/secureIds';
|
import { PlayerId } from '../utilities/secureIds';
|
||||||
|
|
||||||
@@ -23,7 +23,7 @@ async function get(request: UnwrappedRequest): Promise<Response> {
|
|||||||
|
|
||||||
async function list(request: UnwrappedRequest): Promise<Response> {
|
async function list(request: UnwrappedRequest): Promise<Response> {
|
||||||
try {
|
try {
|
||||||
return new OkResponse(await orm.players.list(request.claims));
|
return new PagedResponse(request, await orm.players.list(request.claims));
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return new ErrorResponse(error as Error);
|
return new ErrorResponse(error as Error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export default {
|
|||||||
Claims.COLLECTIONS.OWNED.GAME.REMOVE,
|
Claims.COLLECTIONS.OWNED.GAME.REMOVE,
|
||||||
]),
|
]),
|
||||||
},
|
},
|
||||||
'/api/collection/list': {
|
'/api/collection/list/:pageSize/:page': {
|
||||||
GET: guard(collections.list, [Claims.ADMIN, Claims.COLLECTIONS.OWNED.LIST]),
|
GET: guard(collections.list, [Claims.ADMIN, Claims.COLLECTIONS.OWNED.LIST]),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ export default {
|
|||||||
PATCH: guard(games.update, [Claims.ADMIN, Claims.GAMES.UPDATE]),
|
PATCH: guard(games.update, [Claims.ADMIN, Claims.GAMES.UPDATE]),
|
||||||
DELETE: guard(games.drop, [Claims.ADMIN, Claims.GAMES.DELETE]),
|
DELETE: guard(games.drop, [Claims.ADMIN, Claims.GAMES.DELETE]),
|
||||||
},
|
},
|
||||||
'/api/game/search/:query': {
|
'/api/game/search/:query/:pageSize/:page': {
|
||||||
GET: guard(games.query, [Claims.ADMIN, Claims.GAMES.READ]),
|
GET: guard(games.query, [Claims.ADMIN, Claims.GAMES.READ]),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ export default {
|
|||||||
PATCH: guard(player.update, [Claims.ADMIN, Claims.PLAYERS.OTHER.UPDATE, Claims.PLAYERS.SELF.UPDATE]),
|
PATCH: guard(player.update, [Claims.ADMIN, Claims.PLAYERS.OTHER.UPDATE, Claims.PLAYERS.SELF.UPDATE]),
|
||||||
DELETE: guard(player.drop, [Claims.ADMIN, Claims.PLAYERS.OTHER.DELETE, Claims.PLAYERS.SELF.DELETE]),
|
DELETE: guard(player.drop, [Claims.ADMIN, Claims.PLAYERS.OTHER.DELETE, Claims.PLAYERS.SELF.DELETE]),
|
||||||
},
|
},
|
||||||
'/api/player/list': {
|
'/api/player/list/:pageSize/:page': {
|
||||||
GET: guard(player.list, [Claims.ADMIN, Claims.PLAYERS.OTHER.READ]),
|
GET: guard(player.list, [Claims.ADMIN, Claims.PLAYERS.OTHER.READ]),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { BadRequestError, NotFoundError, UnauthorizedError } from './errors';
|
import { BadRequestError, NotFoundError, UnauthorizedError } from './errors';
|
||||||
import { isArray } from 'lodash';
|
import { clamp, isArray } from 'lodash';
|
||||||
|
import { UnwrappedRequest } from './guard';
|
||||||
|
|
||||||
export class ErrorResponse extends Response {
|
export class ErrorResponse extends Response {
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
@@ -37,9 +38,18 @@ export class NotFoundResponse extends Response {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class PagedResponse extends Response {
|
||||||
|
//@ts-ignore
|
||||||
|
constructor(request: UnwrappedRequest, body: any[]) {
|
||||||
|
const pageSize = clamp(parseInt(request.params.pageSize ?? 100), 1, 100);
|
||||||
|
const page = Math.max(0, parseInt(request.params.page ?? 1) - 1);
|
||||||
|
return new OkResponse(body.slice(page * pageSize, page * pageSize + pageSize));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class OkResponse extends Response {
|
export class OkResponse extends Response {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
constructor(body?: Model | null) {
|
constructor(body?: any) {
|
||||||
if (body) {
|
if (body) {
|
||||||
return Response.json(
|
return Response.json(
|
||||||
isArray(body)
|
isArray(body)
|
||||||
|
|||||||
Reference in New Issue
Block a user