diff --git a/src/api_productions.ts b/src/api_productions.ts index e8ac385..6aabc51 100644 --- a/src/api_productions.ts +++ b/src/api_productions.ts @@ -14,7 +14,8 @@ import { NewProductionLine, ErrorResponse, PatchLineResponse, - PatchLine + PatchLine, + ProductionListResponse } from './models'; import { SmbProtocol } from './smb'; import { ProductionManager } from './production_manager'; @@ -90,13 +91,62 @@ const apiProductions: FastifyPluginCallback = ( } ); + fastify.get<{ + Reply: ProductionListResponse | string; + Querystring: { limit?: number; offset?: number }; + }>( + '/productionlist', + { + schema: { + description: 'Paginated list of all productions.', + querystring: Type.Object({ + limit: Type.Optional(Type.Number()), + offset: Type.Optional(Type.Number()) + }), + response: { + 200: ProductionListResponse, + 500: Type.String() + } + } + }, + async (request, reply) => { + try { + const limit = request.query.limit || 50; + const offset = request.query.offset || 0; + const productions = await productionManager.getProductions( + limit, + offset + ); + const totalItems = await productionManager.getNumberOfProductions(); + reply.code(200).send({ + productions: productions.map(({ _id, name }) => ({ + name, + productionId: _id.toString() + })), + offset, + limit, + totalItems + }); + } catch (err) { + Log().error(err); + reply + .code(500) + .send( + 'Exception thrown when trying to get paginated productions: ' + err + ); + } + } + ); + fastify.get<{ Reply: ProductionResponse[] | string; }>( '/production', { schema: { - description: 'Retrieves all Productions.', + description: + 'Retrieves 50 most recently created productions. Deprecated. Use /productionlist instead.', + deprecated: true, response: { 200: Type.Array(ProductionResponse), 500: Type.String() @@ -105,7 +155,7 @@ const apiProductions: FastifyPluginCallback = ( }, async (request, reply) => { try { - const productions = await productionManager.getProductions(50); + const productions = await productionManager.getProductions(50, 0); reply.code(200).send( productions.map(({ _id, name }) => ({ name, diff --git a/src/db_manager.ts b/src/db_manager.ts index 5e172b6..e3e3766 100644 --- a/src/db_manager.ts +++ b/src/db_manager.ts @@ -30,17 +30,22 @@ const dbManager = { }, /** Get all productions from the database in reverse natural order, limited by the limit parameter */ - async getProductions(limit: number): Promise { + async getProductions(limit: number, offset: number): Promise { const productions = await db .collection('productions') .find() .sort({ $natural: -1 }) + .skip(offset) .limit(limit) .toArray(); return productions as any as Production[]; }, + async getProductionsLength(): Promise { + return await db.collection('productions').countDocuments(); + }, + async getProduction(id: number): Promise { return db.collection('productions').findOne({ _id: id as any }) as | any diff --git a/src/models.ts b/src/models.ts index 3b5a1c4..ca5bf1c 100644 --- a/src/models.ts +++ b/src/models.ts @@ -4,6 +4,7 @@ export type NewProduction = Static; export type NewProductionLine = Static; export type Production = Static; export type ProductionResponse = Static; +export type ProductionListResponse = Static; export type DetailedProductionResponse = Static< typeof DetailedProductionResponse >; @@ -196,6 +197,13 @@ export const ProductionResponse = Type.Object({ productionId: Type.String() }); +export const ProductionListResponse = Type.Object({ + productions: Type.Array(ProductionResponse), + offset: Type.Number(), + limit: Type.Number(), + totalItems: Type.Number() +}); + export const DetailedProductionResponse = Type.Object({ name: Type.String(), productionId: Type.String(), diff --git a/src/production_manager.ts b/src/production_manager.ts index 2a949e7..24a7776 100644 --- a/src/production_manager.ts +++ b/src/production_manager.ts @@ -112,8 +112,12 @@ export class ProductionManager extends EventEmitter { return undefined; } - async getProductions(limit = 0): Promise { - return dbManager.getProductions(limit); + async getProductions(limit = 0, offset = 0): Promise { + return dbManager.getProductions(limit, offset); + } + + async getNumberOfProductions(): Promise { + return dbManager.getProductionsLength(); } async getProduction(id: number): Promise {