Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

feat: add pagination to endpoint #289

Merged
merged 3 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions src/github/github-event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export class GithubEventService {
public readonly eventObservable = this.eventSubject.asObservable();

public emitEvent(event): void {
console.log(event); // @TODO remove
this.eventSubject.next(event);
}

Expand All @@ -41,8 +40,23 @@ export class GithubEventService {
return await newEvent.save();
}

public async getAll() {
return await this.eventModel.find();
public async getAll(offset = 0, limit = 10) {
const totalCount = await this.eventModel.countDocuments();
const pagination = {
offset: offset,
limit: limit,
previousOffset: offset - limit < 0 ? 0 : offset - limit,
nextOffset: offset + limit,
pageCount: Math.floor(totalCount / limit) + 1,
currentPage: Math.floor(offset / limit) + 1,
totalCount: totalCount,
};
const query = this.eventModel.find().skip(offset);
if (limit) {
query.limit(limit);
}
const result = await query;
return { items: result, meta: { pagination } };
}

public async getOne(id: string) {
Expand Down
20 changes: 17 additions & 3 deletions src/github/github.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import {
Headers,
Put,
UseGuards,
Query,
} from '@nestjs/common';
import { ApiSecurity, ApiTags } from '@nestjs/swagger';
import { ApiQuery, ApiSecurity, ApiTags } from '@nestjs/swagger';
import { TokenGuard } from '../auth/token.strategy';
import { CreateGithubProfileDTO } from './dto/create-github.dto';
import { CreateEventDTO } from './dto/create-events.dto';
import { GithubEventService } from './github-event.service';
import { GithubProfileService } from './github-profile.service';
import { mapEvent } from './data/event-map';
import { GithubWebhookGuard } from './guards/webhook.guard';
import { PaginationParams } from './interfaces/pagination-params';

@ApiTags('Github')
@Controller('github')
Expand All @@ -35,8 +37,20 @@ export class GithubController {
}

@Get('events')
async getAllEvents() {
return await this.eventService.getAll();
@ApiQuery({
name: 'limit',
type: Number,
required: false,
description: 'Limits the page size',
})
@ApiQuery({
name: 'offset',
type: Number,
required: false,
description: 'Amount of skipped Documents',
})
async getAllEvents(@Query() { offset, limit }: PaginationParams) {
return await this.eventService.getAll(offset, limit);
}

@Post('events')
Expand Down
16 changes: 16 additions & 0 deletions src/github/interfaces/pagination-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IsNumber, Min, IsOptional } from 'class-validator';
import { Type } from 'class-transformer';

export class PaginationParams {
@IsOptional()
@Type(() => Number)
@IsNumber()
@Min(0)
offset?: number;

@IsOptional()
@Type(() => Number)
@IsNumber()
@Min(1)
limit?: number;
}
2 changes: 1 addition & 1 deletion test/features/github.feature
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Feature: Github module
| event | "workflow_dispatch" |
Then make a GET request to "/github/events"
Then the response status code should be 200
And the response at index "0" should contain:
And the reponse in property "items" at index "0" should contain:
| event | "workflowDispatch" |

Scenario: get all events without authorization
Expand Down
34 changes: 34 additions & 0 deletions test/step-definitions/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,38 @@ export class responses {
}
});
}

@then(/the reponse in property "([^"]*)" at index "([^"]*)" should contain:/)
public validateResponseProperty(
property: string,
index: string,
table: { rawTable: [] },
) {
const data = this.context.tableToObject(table);
Object.keys(data).forEach((key) => {
if (/TYPE:/.test(data[key])) {
switch (data[key]) {
case 'TYPE:ID':
const isValidID = Types.ObjectId.isValid(
JSON.parse(this.context.response.text)[property][index][key],
);
this.context.documentId = JSON.parse(this.context.response.text)[
property
][index][key];
expect(isValidID).to.be.true;
break;
default:
const regex = getRegex(data[key]);
expect(
JSON.parse(this.context.response.text)[property][index][key],
).to.match(regex);
break;
}
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it easier to read if the if ends with a return and then the else is not required

Copy link
Member Author

@Cahllagerfeld Cahllagerfeld Jul 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You got me there :D this thing was generated by Copilot xD I'll refactor it 👍

expect(
JSON.parse(this.context.response.text)[property][index][key],
).to.eql(data[key]);
}
});
}
}