Skip to content

Commit b3c738c

Browse files
authored
Merge pull request #20 from GeneralMagicio/add-getPollDetails
Add get poll details
2 parents a89c687 + 967a66e commit b3c738c

File tree

3 files changed

+130
-14
lines changed

3 files changed

+130
-14
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: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
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+
Res,
11+
} from '@nestjs/common';
12+
import { Response } from 'express';
213
import { PollService } from './poll.service';
314
import { Prisma } from '@prisma/client';
4-
import { CreatePollDto } from './createPoll.dto';
15+
import { CreatePollDto, GetPollsDto } from './Poll.dto';
516

617
@Controller('poll')
718
export class PollController {
@@ -14,12 +25,25 @@ export class PollController {
1425
}
1526

1627
@Get()
17-
getPolls() {
18-
return this.pollService.getPolls();
28+
getPolls(@Req() req, @Query() query: GetPollsDto) {
29+
const userId = 1;
30+
return this.pollService.getPolls(userId, query);
1931
}
2032

2133
@Get(':id')
22-
getPollDetails(@Param('id') id: string) {
23-
return this.pollService.getPollDetails(Number(id));
34+
async getPollDetails(@Param('id') id: number, @Res() res: Response) {
35+
try {
36+
const poll = await this.pollService.getPollDetails(Number(id));
37+
38+
if (!poll) {
39+
return res.status(404).json({ message: 'No poll found' });
40+
}
41+
42+
return res.status(200).json(poll);
43+
} catch (error) {
44+
return res
45+
.status(500)
46+
.json({ message: 'Internal server error', error: error.message });
47+
}
2448
}
2549
}

src/poll/poll.service.ts

Lines changed: 65 additions & 8 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,15 +66,72 @@ 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) {
74-
return this.databaseService.poll.findUnique({
75-
where: {
76-
pollId: id,
77-
},
78-
});
124+
try {
125+
const poll = await this.databaseService.poll.findUnique({
126+
where: { pollId: id },
127+
});
128+
const user = await this.databaseService.user.findUnique({
129+
where: { id: poll?.authorUserId },
130+
});
131+
132+
return { user, poll };
133+
} catch (error) {
134+
throw new Error('Database query failed');
135+
}
79136
}
80137
}

0 commit comments

Comments
 (0)