Skip to content

Commit

Permalink
feat: store votes, add endpoint to retrieve results
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Frost committed May 25, 2024
1 parent 1c76fb0 commit 3ce63da
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/events/events.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,15 @@ export class EventsController {
if (!answer || !['a', 'b'].includes(answer)) {
throw new BadRequestException('Invalid answer');
}

this.eventsService.addVote(id, answer);
}

@Get(':id/vote/results')
getVotes(@Param('id') id: string) {
const event = events.find((e) => e.name === id);
if (!event) throw new NotFoundException('Event not found');

return this.eventsService.getVotes(id);
}
}
15 changes: 15 additions & 0 deletions src/events/events.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ interface Client {
callback: (data: any) => void;
}

export interface Vote {
event: string;
answer: 'a' | 'b';
createdAt: Date;
}

@Injectable()
export class EventsService {
private clients: Client[] = [];
private currentActions: Record<string, any> = events.reduce(
(acc, event) => ({ ...acc, [event.name]: { action: null } }),
{},
);
private votes: Vote[] = [];

constructor() {
events.forEach((event) => this.scheduleNewAction(event.name));
Expand Down Expand Up @@ -67,4 +74,12 @@ export class EventsService {
getCurrentAction(event: string) {
return this.currentActions[event];
}

addVote(event: string, answer: 'a' | 'b') {
this.votes.push({ event, answer, createdAt: new Date() });
}

getVotes(event: string) {
return this.votes.filter((vote) => vote.event === event);
}
}

0 comments on commit 3ce63da

Please sign in to comment.