Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Result Pie Charts #15

Merged
merged 12 commits into from
Jun 27, 2022
2 changes: 2 additions & 0 deletions backend/src/answer/answer.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Repository } from 'typeorm';
import { AnswerController } from './answer.controller';
import { AnswerService } from './answer.service';
import { PollService } from '../poll/poll.service';
import { VoteGateway } from '../vote/vote.gateway';

describe('AnswerController', () => {
let controller: AnswerController;
Expand All @@ -15,6 +16,7 @@ describe('AnswerController', () => {
AnswerService,
QuestionService,
PollService,
VoteGateway,
{
provide: 'AnswerRepository',
useClass: Repository,
Expand Down
23 changes: 23 additions & 0 deletions backend/src/answer/answer.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@nestjs/common';
import {
ApiCreatedResponse,
ApiNoContentResponse,
ApiNotFoundResponse,
ApiOkResponse,
ApiTags,
Expand Down Expand Up @@ -110,6 +111,28 @@ export class AnswerController {
return this.sanitized(answer);
}

@Post(':answerId/vote')
@ApiNoContentResponse({
description: 'Vote recorded successfully',
})
@ApiNotFoundResponse({
type: null,
description: 'No answer with the given id has been found',
})
async vote(
@Param('pollId') pollId: number,
@Param('questionId') questionId: number,
@Param('answerId') answerId: number,
@Res({ passthrough: true }) res: Response,
): Promise<null> {
const result = await this.answerService.increaseCount(answerId);
if (!result) {
res.status(404);
return;
}
res.status(204);
}

// @Patch(':answerId')
// @ApiOkResponse({
// type: [Answer],
Expand Down
3 changes: 2 additions & 1 deletion backend/src/answer/answer.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { AnswerController } from './answer.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import Answer from '../models/answer';
import { QuestionModule } from '../question/question.module';
import { VoteModule } from '../vote/vote.module';

@Module({
imports: [TypeOrmModule.forFeature([Answer]), QuestionModule],
imports: [TypeOrmModule.forFeature([Answer]), QuestionModule, VoteModule],
controllers: [AnswerController],
providers: [AnswerService],
exports: [AnswerService],
Expand Down
2 changes: 2 additions & 0 deletions backend/src/answer/answer.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { QuestionService } from '../question/question.service';
import { Repository } from 'typeorm';
import { AnswerService } from './answer.service';
import { PollService } from '../poll/poll.service';
import { VoteGateway } from '../vote/vote.gateway';

describe('AnswerService', () => {
let service: AnswerService;
Expand All @@ -12,6 +13,7 @@ describe('AnswerService', () => {
providers: [
AnswerService,
QuestionService,
VoteGateway,
PollService,
{
provide: 'AnswerRepository',
Expand Down
14 changes: 14 additions & 0 deletions backend/src/answer/answer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { QuestionService } from '../question/question.service';
import { Repository } from 'typeorm';
import { CreateAnswerDto } from './dto/create-answer.dto';
import { UpdateAnswerDto } from './dto/update-answer.dto';
import { VoteGateway } from '../vote/vote.gateway';

@Injectable()
export class AnswerService {
Expand All @@ -13,6 +14,8 @@ export class AnswerService {
private answerRepository: Repository<Answer>,
@Inject(QuestionService)
private questionService: QuestionService,
@Inject(VoteGateway)
private voteGateway: VoteGateway,
) {}

async create(
Expand Down Expand Up @@ -50,6 +53,17 @@ export class AnswerService {
});
}

async increaseCount(answerId: number): Promise<boolean> {
const result = await this.answerRepository.increment(
{ id: answerId },
'count',
1,
);
this.voteGateway.notifyListeners();

return result.affected == 1;
}

// async update(
// pollId: number,
// questionId: number,
Expand Down
5 changes: 3 additions & 2 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import options from './config/ormconfig';
import { PollModule } from './poll/poll.module';
import { VoteGateway } from './vote/vote.gateway';
import { QuestionModule } from './question/question.module';
import { AnswerModule } from './answer/answer.module';
import { VoteModule } from './vote/vote.module';

@Module({
imports: [
TypeOrmModule.forRoot(options),
PollModule,
QuestionModule,
AnswerModule,
VoteModule,
],
controllers: [],
providers: [VoteGateway],
providers: [],
})
export class AppModule {}
2 changes: 0 additions & 2 deletions backend/src/types/voting.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ interface ServerToClientEvents {
}

interface VotePayload {
pollCode: string;
questionID: number;
answerID: number;
}

Expand Down
31 changes: 5 additions & 26 deletions backend/src/vote/vote.gateway.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,17 @@
import {
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
} from '@nestjs/websockets';
import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets';
import { Server } from 'socket.io';

interface UpdatePayload {
votes: number;
}

interface ServerToClientEvents {
update: (payload: UpdatePayload) => void;
}

interface VotePayload {
pollCode: string;
questionID: number;
answerID: number;
}

interface ClientToServerEvents {
vote: (payload: VotePayload) => void;
update: () => void;
}

export type VoteServer = Server<ClientToServerEvents, ServerToClientEvents>;
export type VoteServer = Server<Record<string, never>, ServerToClientEvents>;

@WebSocketGateway()
export class VoteGateway {
@WebSocketServer() server: VoteServer;
private votes = 0;

@SubscribeMessage('vote')
handleMessage(client: any, payload: VotePayload): void {
this.votes++;
this.server.emit('update', { votes: this.votes });
notifyListeners() {
this.server.emit('update');
}
}
8 changes: 8 additions & 0 deletions backend/src/vote/vote.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { VoteGateway } from './vote.gateway';

@Module({
providers: [VoteGateway],
exports: [VoteGateway],
})
export class VoteModule {}
2 changes: 1 addition & 1 deletion frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
rel="stylesheet"
href="https://fonts.googleapis.com/icon?family=Material+Icons"
/>
<title>Vite App</title>
<title>SolidPolls</title>
</head>
<body>
<div id="root"></div>
Expand Down
Loading