events.controller.ts93 lines, 2.1 KB
| 1 | import { |
| 2 | Controller, |
| 3 | Get, |
| 4 | Post, |
| 5 | Put, |
| 6 | Body, |
| 7 | Param, |
| 8 | Request, |
| 9 | UseGuards, |
| 10 | ParseUUIDPipe, |
| 11 | } from '@nestjs/common'; |
| 12 | import { AuthGuard } from '@nestjs/passport'; |
| 13 | import { EventsService } from './events.service'; |
| 14 | import { CreateEventDto, UpdateEventDto } from '@app/common'; |
| 15 | |
| 16 | @Controller('events') |
| 17 | export class EventsController { |
| 18 | constructor(private readonly eventsService: EventsService) {} |
| 19 | |
| 20 | @Get() |
| 21 | findAll() { |
| 22 | return this.eventsService.findAll(); |
| 23 | } |
| 24 | |
| 25 | // Protected - get my events |
| 26 | @UseGuards(AuthGuard('jwt')) |
| 27 | @Get('my-events') |
| 28 | findMyEvents(@Request() req: { user: { userId: string } }) { |
| 29 | return this.eventsService.findMyEvents(req.user.userId); |
| 30 | } |
| 31 | |
| 32 | //Public - get a single event |
| 33 | @Get(':id') |
| 34 | findOne(@Param('id', ParseUUIDPipe) id: string) { |
| 35 | return this.eventsService.findOne(id); |
| 36 | } |
| 37 | |
| 38 | @UseGuards(AuthGuard('jwt')) |
| 39 | @Post() |
| 40 | create( |
| 41 | @Body() createEventDto: CreateEventDto, |
| 42 | @Request() req: { user: { userId: string; role?: string } }, |
| 43 | ) { |
| 44 | return this.eventsService.create( |
| 45 | createEventDto, |
| 46 | req.user.userId, |
| 47 | req.user.role || 'USER', |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | @UseGuards(AuthGuard('jwt')) |
| 52 | @Put(':id') |
| 53 | update( |
| 54 | @Param('id', ParseUUIDPipe) id: string, |
| 55 | @Body() updateEventDto: UpdateEventDto, |
| 56 | @Request() req: { user: { userId: string; role?: string } }, |
| 57 | ) { |
| 58 | return this.eventsService.update( |
| 59 | id, |
| 60 | updateEventDto, |
| 61 | req.user.userId, |
| 62 | req.user.role || 'USER', |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | // Protected - publish event |
| 67 | @UseGuards(AuthGuard('jwt')) |
| 68 | @Post(':id/publish') |
| 69 | publish( |
| 70 | @Param('id', ParseUUIDPipe) id: string, |
| 71 | @Request() req: { user: { userId: string; role?: string } }, |
| 72 | ) { |
| 73 | return this.eventsService.publish( |
| 74 | id, |
| 75 | req.user.userId, |
| 76 | req.user.role || 'USER', |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | // Protected - cancel event |
| 81 | @UseGuards(AuthGuard('jwt')) |
| 82 | @Post(':id/cancel') |
| 83 | cancel( |
| 84 | @Param('id', ParseUUIDPipe) id: string, |
| 85 | @Request() req: { user: { userId: string; role?: string } }, |
| 86 | ) { |
| 87 | return this.eventsService.cancel( |
| 88 | id, |
| 89 | req.user.userId, |
| 90 | req.user.role || 'USER', |
| 91 | ); |
| 92 | } |
| 93 | } |