Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: endpoint for paginated list of productions #102

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,20 +30,25 @@
},

/** 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[];

Check warning on line 42 in src/db_manager.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
},

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

Check warning on line 50 in src/db_manager.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
| any

Check warning on line 51 in src/db_manager.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
| undefined;
},

Expand All @@ -52,21 +57,21 @@
): Promise<Production | undefined> {
const result = await db
.collection('productions')
.updateOne({ _id: production._id as any }, { $set: production });

Check warning on line 60 in src/db_manager.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
return result.modifiedCount === 1 ? production : undefined;
},

async addProduction(name: string, lines: Line[]): Promise<Production> {
const _id = await getNextSequence('productions');
const production = { name, lines, _id };
await db.collection('productions').insertOne(production as any);

Check warning on line 67 in src/db_manager.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
return production;
},

async deleteProduction(productionId: number): Promise<boolean> {
const result = await db
.collection('productions')
.deleteOne({ _id: productionId as any });

Check warning on line 74 in src/db_manager.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
return result.deletedCount === 1;
},

Expand All @@ -86,7 +91,7 @@
await db
.collection('productions')
.updateOne(
{ _id: productionId as any },

Check warning on line 94 in src/db_manager.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
{ $set: { lines: production.lines } }
);
}
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 @@ -112,8 +112,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
Loading