Skip to content

Commit

Permalink
Merge pull request #691 from bounswe/be-668/add-pagination-to-fetch-p…
Browse files Browse the repository at this point in the history
…oll-endpoint

Be 668/add pagination to fetch poll endpoint
  • Loading branch information
BatuhanIlhan authored Dec 23, 2023
2 parents d81088d + 0acd455 commit 77c741e
Show file tree
Hide file tree
Showing 3 changed files with 399 additions and 14 deletions.
3 changes: 0 additions & 3 deletions app/backend/src/poll/entities/poll.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ export class Poll {
@OneToMany(() => Vote, (vote) => vote.poll, { cascade: true })
votes: Relation<Vote[]>;

@Column({ default: 0 })
vote_count: number;

// @Todo Replace with report entity
//@Column({ nullable: true })
//report_list: Array<any>;
Expand Down
234 changes: 226 additions & 8 deletions app/backend/src/poll/poll.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ParseArrayPipe,
ConflictException,
ParseBoolPipe,
ParseIntPipe,
} from '@nestjs/common';
import { PollService } from './poll.service';
import { CreatePollDto } from './dto/create-poll.dto';
Expand Down Expand Up @@ -150,7 +151,6 @@ export class PollController {
}

@ApiQuery({ name: 'creatorId', required: false })
@ApiQuery({ name: 'approveStatus', required: false })
@ApiQuery({ name: 'likedById', required: false })
@ApiQuery({ name: 'votedById', required: false })
@ApiQuery({ name: 'followedById', required: false })
Expand All @@ -170,8 +170,6 @@ export class PollController {
@Req() req: any,
@Query('creatorId', new ParseUUIDPipe({ optional: true }))
creatorId?: string,
@Query('approveStatus', new ParseBoolPipe({ optional: true }))
approveStatus?: string,
@Query('likedById', new ParseUUIDPipe({ optional: true }))
likedById?: string,
@Query('followedById', new ParseUUIDPipe({ optional: true }))
Expand All @@ -184,7 +182,7 @@ export class PollController {
const userId = req.user?.sub; // Realize that it is not id instead sub. I do not know why but middleware gives this field.
return await this.pollService.findAll({
creatorId,
approveStatus,
approveStatus: true,
likedById,
votedById: null,
followedById,
Expand All @@ -194,6 +192,58 @@ export class PollController {
});
}

@ApiQuery({ name: 'pageSize', required: true })
@ApiQuery({ name: 'pageNum', required: true })
@ApiQuery({ name: 'creatorId', required: false })
@ApiQuery({ name: 'likedById', required: false })
@ApiQuery({ name: 'votedById', required: false })
@ApiQuery({ name: 'followedById', required: false })
@ApiQuery({ name: 'sort', required: false })
@ApiQuery({ name: 'tags', required: false })
@ApiResponse({
status: 200,
description: 'Polls are fetched successfully.',
type: [GetPollResponseDto],
})
@ApiResponse({
status: 500,
description: 'Internal server error, contact with backend team.',
})
@Get('/with-pagination')
public async findAllWithPagination(
@Req() req: any,
@Query('pageSize', ParseIntPipe)
pageSize: number,
@Query('pageNum', ParseIntPipe)
pageNum: number,
@Query('creatorId', new ParseUUIDPipe({ optional: true }))
creatorId?: string,
@Query('likedById', new ParseUUIDPipe({ optional: true }))
likedById?: string,
@Query('votedById', new ParseUUIDPipe({ optional: true }))
votedById?: string,
@Query('followedById', new ParseUUIDPipe({ optional: true }))
followedById?: string,
@Query('sort')
sortString?: string,
@Query('tags', new ParseArrayPipe({ optional: true }))
tags?: Array<string>,
): Promise<any> {
const userId = req.user?.sub; // Realize that it is not id instead sub. I do not know why but middleware gives this field.
return await this.pollService.findAllWithPagination({
creatorId,
approveStatus: true,
likedById,
votedById,
followedById,
sortString,
tags,
userId,
pageSize,
pageNum,
});
}

@UseGuards(AuthGuard, VerificationGuard)
@ApiQuery({ name: 'likedById', required: false })
@ApiResponse({
Expand All @@ -210,7 +260,7 @@ export class PollController {
const creatorId = req.user.id;
return await this.pollService.findAll({
creatorId,
approveStatus: null,
approveStatus: true,
likedById: null,
votedById: null,
followedById: null,
Expand All @@ -220,6 +270,41 @@ export class PollController {
});
}

@UseGuards(AuthGuard, VerificationGuard)
@ApiQuery({ name: 'pageSize', required: true })
@ApiQuery({ name: 'pageNum', required: true })
@ApiResponse({
status: 200,
description: 'Polls are fetched successfully.',
type: [GetPollResponseDto],
})
@ApiResponse({
status: 500,
description: 'Internal server error, contact with backend team.',
})
@Get('my-polls-with-pagination')
public async findMyPollsWithPagination(
@Req() req: any,
@Query('pageSize', ParseIntPipe)
pageSize: number,
@Query('pageNum', ParseIntPipe)
pageNum: number,
): Promise<any> {
const creatorId = req.user.id;
return await this.pollService.findAllWithPagination({
creatorId,
approveStatus: true,
likedById: null,
votedById: null,
followedById: null,
sortString: null,
tags: null,
userId: creatorId,
pageSize,
pageNum,
});
}

@UseGuards(AuthGuard, VerificationGuard)
@ApiQuery({ name: 'get poll with filtered status', required: false })
@ApiResponse({
Expand Down Expand Up @@ -261,13 +346,48 @@ export class PollController {
const userId = req.user.id;
return await this.pollService.findAll({
creatorId: null,
approveStatus: null,
approveStatus: true,
likedById: userId,
votedById: null,
followedById: null,
sortString: null,
tags: null,
userId,
});
}

@UseGuards(AuthGuard, VerificationGuard)
@ApiQuery({ name: 'pageSize', required: true })
@ApiQuery({ name: 'pageNum', required: true })
@ApiResponse({
status: 200,
description: 'Polls are fetched successfully.',
type: [GetPollResponseDto],
})
@ApiResponse({
status: 500,
description: 'Internal server error, contact with backend team.',
})
@Get('liked-by-me-with-pagination')
public async findPollsILikedWithPagination(
@Req() req: any,
@Query('pageSize', ParseIntPipe)
pageSize: number,
@Query('pageNum', ParseIntPipe)
pageNum: number,
): Promise<any> {
const userId = req.user.id;
return await this.pollService.findAllWithPagination({
creatorId: null,
approveStatus: true,
likedById: userId,
votedById: null,
followedById: null,
sortString: null,
tags: null,
userId,
pageSize,
pageNum,
});
}

Expand All @@ -286,7 +406,7 @@ export class PollController {
const userId = req.user.id;
return await this.pollService.findAll({
creatorId: null,
approveStatus: null,
approveStatus: true,
likedById: null,
votedById: userId,
followedById: null,
Expand All @@ -296,6 +416,41 @@ export class PollController {
});
}

@UseGuards(AuthGuard, VerificationGuard)
@ApiQuery({ name: 'pageSize', required: true })
@ApiQuery({ name: 'pageNum', required: true })
@ApiResponse({
status: 200,
description: 'Polls are fetched successfully.',
type: [GetPollResponseDto],
})
@ApiResponse({
status: 500,
description: 'Internal server error, contact with backend team.',
})
@Get('voted-by-me-with-pagination')
public async findPollsIVotedWithPagination(
@Req() req: any,
@Query('pageSize', ParseIntPipe)
pageSize: number,
@Query('pageNum', ParseIntPipe)
pageNum: number,
): Promise<any> {
const userId = req.user.id;
return await this.pollService.findAllWithPagination({
creatorId: null,
approveStatus: true,
likedById: null,
votedById: userId,
followedById: null,
sortString: null,
tags: null,
userId,
pageSize,
pageNum,
});
}

@UseGuards(AuthGuard, VerificationGuard)
@ApiResponse({
status: 200,
Expand All @@ -312,6 +467,34 @@ export class PollController {
return await this.pollService.findPollsUserdidNotVote(userId);
}

@UseGuards(AuthGuard, VerificationGuard)
@ApiQuery({ name: 'pageSize', required: true })
@ApiQuery({ name: 'pageNum', required: true })
@ApiResponse({
status: 200,
description: 'Polls are fetched successfully.',
type: [GetPollResponseDto],
})
@ApiResponse({
status: 500,
description: 'Internal server error, contact with backend team.',
})
@Get('not-voted-by-me-with-pagination')
public async findPollsIdidNoteVoteWithPagination(
@Req() req: any,
@Query('pageSize', ParseIntPipe)
pageSize: number,
@Query('pageNum', ParseIntPipe)
pageNum: number,
): Promise<any> {
const userId = req.user.id;
return await this.pollService.findPollsUserdidNotVoteWithPagination(
userId,
pageSize,
pageNum,
);
}

@UseGuards(AuthGuard, VerificationGuard)
@ApiResponse({
status: 200,
Expand All @@ -327,13 +510,48 @@ export class PollController {
const userId = req.user.id;
return await this.pollService.findAll({
creatorId: null,
approveStatus: null,
approveStatus: true,
likedById: null,
votedById: null,
followedById: userId,
sortString: null,
tags: null,
userId,
});
}

@UseGuards(AuthGuard, VerificationGuard)
@ApiQuery({ name: 'pageSize', required: true })
@ApiQuery({ name: 'pageNum', required: true })
@ApiResponse({
status: 200,
description: 'Polls are fetched successfully.',
type: [GetPollResponseDto],
})
@ApiResponse({
status: 500,
description: 'Internal server error, contact with backend team.',
})
@Get('my-followings-with-pagination')
public async findPollsOfUsersIFollowWithPagination(
@Req() req: any,
@Query('pageSize', ParseIntPipe)
pageSize: number,
@Query('pageNum', ParseIntPipe)
pageNum: number,
): Promise<any> {
const userId = req.user.id;
return await this.pollService.findAllWithPagination({
creatorId: null,
approveStatus: true,
likedById: null,
votedById: null,
followedById: userId,
sortString: null,
tags: null,
userId,
pageSize,
pageNum,
});
}

Expand Down
Loading

0 comments on commit 77c741e

Please sign in to comment.