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' ;
2
12
import { Response } from 'express' ;
3
13
import { events } from './constants' ;
4
14
import { EventsService } from './events.service' ;
@@ -7,14 +17,14 @@ import { EventsService } from './events.service';
7
17
export class EventsController {
8
18
constructor ( private readonly eventsService : EventsService ) { }
9
19
10
- @Get ( '/ :id/subscribe' )
20
+ @Get ( ':id/subscribe' )
11
21
poll (
12
22
@Res ( ) res : Response ,
13
23
@Param ( 'id' ) id : string ,
14
24
@Query ( 'isInitialRequest' ) isInitialRequestParam : string ,
15
25
) {
16
26
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' ) ;
18
28
19
29
const isInitialRequest = isInitialRequestParam === 'true' ;
20
30
if ( isInitialRequest ) {
@@ -24,4 +34,15 @@ export class EventsController {
24
34
const cb = ( data : any ) => res . json ( data ) ;
25
35
this . eventsService . addClient ( { subscribedEvent : event . name , callback : cb } ) ;
26
36
}
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
+ }
27
48
}
0 commit comments