Skip to content

Commit

Permalink
feat: endpoint for paginated list of productions (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
birme authored Jul 9, 2024
1 parent 17866aa commit 25a1ecb
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
56 changes: 53 additions & 3 deletions src/api_productions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
NewProductionLine,
ErrorResponse,
PatchLineResponse,
PatchLine
PatchLine,
ProductionListResponse
} from './models';
import { SmbProtocol } from './smb';
import { ProductionManager } from './production_manager';
Expand Down Expand Up @@ -90,13 +91,62 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
}
);

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()
Expand All @@ -105,7 +155,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
},
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,
Expand Down
7 changes: 6 additions & 1 deletion src/db_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Production[]> {
async getProductions(limit: number, offset: number): Promise<Production[]> {
const productions = await db
.collection('productions')
.find()
.sort({ $natural: -1 })
.skip(offset)
.limit(limit)
.toArray();

return productions as any as Production[];
},

async getProductionsLength(): Promise<number> {
return await db.collection('productions').countDocuments();
},

async getProduction(id: number): Promise<Production | undefined> {
return db.collection('productions').findOne({ _id: id as any }) as
| any
Expand Down
8 changes: 8 additions & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type NewProduction = Static<typeof NewProduction>;
export type NewProductionLine = Static<typeof NewProductionLine>;
export type Production = Static<typeof Production>;
export type ProductionResponse = Static<typeof ProductionResponse>;
export type ProductionListResponse = Static<typeof ProductionListResponse>;
export type DetailedProductionResponse = Static<
typeof DetailedProductionResponse
>;
Expand Down Expand Up @@ -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(),
Expand Down
8 changes: 6 additions & 2 deletions src/production_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,12 @@ export class ProductionManager extends EventEmitter {
return undefined;
}

async getProductions(limit = 0): Promise<Production[]> {
return dbManager.getProductions(limit);
async getProductions(limit = 0, offset = 0): Promise<Production[]> {
return dbManager.getProductions(limit, offset);
}

async getNumberOfProductions(): Promise<number> {
return dbManager.getProductionsLength();
}

async getProduction(id: number): Promise<Production | undefined> {
Expand Down

0 comments on commit 25a1ecb

Please sign in to comment.