Skip to content

Commit 535ee63

Browse files
feat: add voting endpoint
1 parent 1260c9c commit 535ee63

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/events/events.controller.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
import { Controller, Get, Param, Query, Res } from '@nestjs/common';
1+
import {
2+
BadRequestException,
3+
Body,
4+
Controller,
5+
Get,
6+
NotFoundException,
7+
Param,
8+
Post,
9+
Query,
10+
Res,
11+
} from '@nestjs/common';
212
import { Response } from 'express';
313
import { events } from './constants';
414
import { EventsService } from './events.service';
@@ -7,14 +17,14 @@ import { EventsService } from './events.service';
717
export class EventsController {
818
constructor(private readonly eventsService: EventsService) {}
919

10-
@Get('/:id/subscribe')
20+
@Get(':id/subscribe')
1121
poll(
1222
@Res() res: Response,
1323
@Param('id') id: string,
1424
@Query('isInitialRequest') isInitialRequestParam: string,
1525
) {
1626
const event = events.find((e) => e.name === id);
17-
if (!event) return res.status(404).json({ message: 'Event not found' });
27+
if (!event) throw new NotFoundException('Event not found');
1828

1929
const isInitialRequest = isInitialRequestParam === 'true';
2030
if (isInitialRequest) {
@@ -24,4 +34,15 @@ export class EventsController {
2434
const cb = (data: any) => res.json(data);
2535
this.eventsService.addClient({ subscribedEvent: event.name, callback: cb });
2636
}
37+
38+
@Post(':id/vote')
39+
vote(@Param('id') id: string, @Body() body: any) {
40+
const event = events.find((e) => e.name === id);
41+
if (!event) throw new NotFoundException('Event not found');
42+
43+
const answer = body.answer;
44+
if (!answer || !['a', 'b'].includes(answer)) {
45+
throw new BadRequestException('Invalid answer');
46+
}
47+
}
2748
}

0 commit comments

Comments
 (0)