Skip to content

Commit

Permalink
feat: add voting endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Frost committed May 19, 2024
1 parent 1260c9c commit 535ee63
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/events/events.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { Controller, Get, Param, Query, Res } from '@nestjs/common';
import {
BadRequestException,
Body,
Controller,
Get,
NotFoundException,
Param,
Post,
Query,
Res,
} from '@nestjs/common';
import { Response } from 'express';
import { events } from './constants';
import { EventsService } from './events.service';
Expand All @@ -7,14 +17,14 @@ import { EventsService } from './events.service';
export class EventsController {
constructor(private readonly eventsService: EventsService) {}

@Get('/:id/subscribe')
@Get(':id/subscribe')
poll(
@Res() res: Response,
@Param('id') id: string,
@Query('isInitialRequest') isInitialRequestParam: string,
) {
const event = events.find((e) => e.name === id);
if (!event) return res.status(404).json({ message: 'Event not found' });
if (!event) throw new NotFoundException('Event not found');

const isInitialRequest = isInitialRequestParam === 'true';
if (isInitialRequest) {
Expand All @@ -24,4 +34,15 @@ export class EventsController {
const cb = (data: any) => res.json(data);
this.eventsService.addClient({ subscribedEvent: event.name, callback: cb });
}

@Post(':id/vote')
vote(@Param('id') id: string, @Body() body: any) {
const event = events.find((e) => e.name === id);
if (!event) throw new NotFoundException('Event not found');

const answer = body.answer;
if (!answer || !['a', 'b'].includes(answer)) {
throw new BadRequestException('Invalid answer');
}
}
}

0 comments on commit 535ee63

Please sign in to comment.