Skip to content

Commit 817b907

Browse files
authored
Merge pull request #12 from GeneralMagicio/get-polls-api
added getPolls api
2 parents e1bbf1c + 8d9065d commit 817b907

File tree

3 files changed

+102
-7
lines changed

3 files changed

+102
-7
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import {
22
IsArray,
33
IsBoolean,
44
IsDateString,
5+
IsEnum,
6+
IsInt,
57
IsNotEmpty,
68
IsOptional,
79
IsString,
10+
Min,
811
} from 'class-validator';
912

1013
export class CreatePollDto {
@@ -38,3 +41,35 @@ export class CreatePollDto {
3841
@IsOptional()
3942
isAnonymous?: boolean;
4043
}
44+
45+
export class GetPollsDto {
46+
@IsOptional()
47+
@IsInt()
48+
@Min(1)
49+
page?: number = 1;
50+
51+
@IsOptional()
52+
@IsInt()
53+
@Min(1)
54+
limit?: number = 10;
55+
56+
@IsOptional()
57+
@IsBoolean()
58+
isActive?: boolean;
59+
60+
@IsOptional()
61+
@IsBoolean()
62+
userVoted?: boolean;
63+
64+
@IsOptional()
65+
@IsBoolean()
66+
userCreated?: boolean;
67+
68+
@IsOptional()
69+
@IsEnum(['endDate', 'participantCount', 'creationDate'])
70+
sortBy?: 'endDate' | 'participantCount' | 'creationDate';
71+
72+
@IsOptional()
73+
@IsEnum(['asc', 'desc'])
74+
sortOrder?: 'asc' | 'desc';
75+
}

src/poll/poll.controller.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
import { Controller, Get, Post, Body, Param, Delete } from '@nestjs/common';
1+
import {
2+
Controller,
3+
Get,
4+
Post,
5+
Body,
6+
Param,
7+
Delete,
8+
Req,
9+
Query,
10+
} from '@nestjs/common';
211
import { PollService } from './poll.service';
312
import { Prisma } from '@prisma/client';
4-
import { CreatePollDto } from './createPoll.dto';
13+
import { CreatePollDto, GetPollsDto } from './Poll.dto';
514

615
@Controller('poll')
716
export class PollController {
@@ -14,8 +23,9 @@ export class PollController {
1423
}
1524

1625
@Get()
17-
getPolls() {
18-
return this.pollService.getPolls();
26+
getPolls(@Req() req, @Query() query: GetPollsDto) {
27+
const userId = 1;
28+
return this.pollService.getPolls(userId, query);
1929
}
2030

2131
@Get(':id')

src/poll/poll.service.ts

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BadRequestException, Injectable } from '@nestjs/common';
22
import { ActionType, Prisma } from '@prisma/client';
33
import { DatabaseService } from 'src/database/database.service';
4-
import { CreatePollDto } from './createPoll.dto';
4+
import { CreatePollDto, GetPollsDto } from './Poll.dto';
55

66
@Injectable()
77
export class PollService {
@@ -66,8 +66,58 @@ export class PollService {
6666
});
6767
}
6868

69-
async getPolls() {
70-
return this.databaseService.poll.findMany({});
69+
async getPolls(userId: number, query: GetPollsDto) {
70+
const {
71+
page = 1,
72+
limit = 10,
73+
isActive,
74+
userVoted,
75+
userCreated,
76+
sortBy = 'endDate',
77+
sortOrder = 'asc',
78+
} = query;
79+
const skip = (page - 1) * limit;
80+
const now = new Date();
81+
const filters: any = {};
82+
if (isActive) {
83+
filters.startDate = { lte: now };
84+
filters.endDate = { gt: now };
85+
}
86+
if (userCreated) {
87+
filters.authorUserId = userId;
88+
}
89+
90+
// Get polls user voted in
91+
let votedPollIds: number[] = [];
92+
if (userVoted) {
93+
const userVotes = await this.databaseService.vote.findMany({
94+
where: { userId },
95+
select: { pollId: true },
96+
});
97+
votedPollIds = userVotes.map((v) => v.pollId);
98+
filters.pollId = { in: votedPollIds };
99+
}
100+
101+
// Sorting options
102+
const orderBy: any = {};
103+
if (sortBy) {
104+
orderBy[sortBy] = sortOrder;
105+
} else {
106+
orderBy.endDate = 'asc';
107+
}
108+
109+
// Fetch polls with pagination
110+
const [polls, total] = await this.databaseService.$transaction([
111+
this.databaseService.poll.findMany({
112+
where: filters,
113+
orderBy,
114+
skip,
115+
take: Number(limit),
116+
}),
117+
this.databaseService.poll.count({ where: filters }),
118+
]);
119+
120+
return { polls, total };
71121
}
72122

73123
async getPollDetails(id: number) {

0 commit comments

Comments
 (0)