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

#312: Add pagination to the github/{id}/events endpoint #320

Merged
merged 2 commits into from
Jun 10, 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
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.

2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { UserModule } from './user/user.module';
import { GithubModule } from './github/github.module';
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
import { APP_GUARD } from '@nestjs/core';
import { UtilsModule } from './utils/utils.module';

@Module({
imports: [
Expand All @@ -23,6 +24,7 @@ import { APP_GUARD } from '@nestjs/core';
}),
UserModule,
GithubModule,
UtilsModule,
],
controllers: [AppController],
providers: [
Expand Down
43 changes: 31 additions & 12 deletions src/github/github-event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Subject } from 'rxjs';
import { GithubEventModel } from './schema/github-event.schema';
import { PaginationService } from '../utils/pagination/pagination.service';

const DEFAULT_LIMIT = 10;
const DEFAULT_OFFSET = 0;

@Injectable()
export class GithubEventService {
constructor(
@InjectModel(GithubEventModel.name)
private readonly eventModel: Model<GithubEventModel>,
private readonly paginationService: PaginationService,
) {}

/**
Expand Down Expand Up @@ -40,17 +45,14 @@ export class GithubEventService {
return await newEvent.save();
}

public async getAll(offset = 0, limit = 10) {
public async getAll(offset = DEFAULT_OFFSET, limit = DEFAULT_LIMIT) {
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 pagination = this.paginationService.getPagination(
offset,
limit,
totalCount,
);

const query = this.eventModel.find().skip(offset);
if (limit) {
query.limit(limit);
Expand All @@ -63,8 +65,25 @@ export class GithubEventService {
return await this.eventModel.findById(id);
}

public async getByUsername(githubUsername: string) {
return await this.eventModel.find({ githubUsername });
public async getByUsername(
githubUsername: string,
offset = DEFAULT_OFFSET,
limit = DEFAULT_LIMIT,
) {
const totalCount = await this.eventModel.countDocuments({ githubUsername });
const pagination = this.paginationService.getPagination(
offset,
limit,
totalCount,
);

const query = this.eventModel.find({ githubUsername }).skip(offset);
if (limit) {
query.limit(limit);
}

const result = await query;
return { items: result, meta: { pagination } };
}

public async deleteByUsername(githubUsername: string) {
Expand Down
7 changes: 5 additions & 2 deletions src/github/github.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ export class GithubController {
}

@Get(':id/events')
async findEvents(@Param('id') id: string) {
return await this.eventService.getByUsername(id);
async findEvents(
@Param('id') id: string,
@Query() { offset, limit }: PaginationParams,
) {
return await this.eventService.getByUsername(id, offset, limit);
}
}
2 changes: 2 additions & 0 deletions src/github/github.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { GithubEventService } from './github-event.service';
import { APP_GUARD } from '@nestjs/core';
import { ThrottlerGuard } from '@nestjs/throttler';
import { EventGateway } from './event.gateway';
import { UtilsModule } from 'src/utils/utils.module';

@Module({
imports: [
Expand All @@ -27,6 +28,7 @@ import { EventGateway } from './event.gateway';
{ name: GithubProfileModel.name, schema: GithubProfileSchema },
{ name: GithubEventModel.name, schema: GithubEventSchema },
]),
UtilsModule,
],
controllers: [GithubController],
providers: [
Expand Down
18 changes: 18 additions & 0 deletions src/utils/pagination/pagination.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PaginationService } from './pagination.service';

describe('PaginationService', () => {
let service: PaginationService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PaginationService],
}).compile();

service = module.get<PaginationService>(PaginationService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
16 changes: 16 additions & 0 deletions src/utils/pagination/pagination.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class PaginationService {
getPagination(offset: number, limit: number, totalCount: number) {
return {
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,
};
}
}
8 changes: 8 additions & 0 deletions src/utils/utils.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { PaginationService } from './pagination/pagination.service';

@Module({
providers: [PaginationService],
exports: [PaginationService],
})
export class UtilsModule {}
Loading