|
| 1 | +import { Controller, Get, Post, Body, Patch, Param, Delete, Req, UseGuards } from '@nestjs/common'; |
| 2 | +import { RulesService } from './rules.service'; |
| 3 | +import { CreateRuleDto } from './dto/create-rule.dto'; |
| 4 | +import { UpdateRuleDto } from './dto/update-rule.dto'; |
| 5 | +import { ApiBearerAuth, ApiOkResponse, ApiSecurity } from '@nestjs/swagger'; |
| 6 | +import { JwtAuthGuard } from '../auth/guards/jwt.auth.guard'; |
| 7 | +import { RuleDto } from './dto/rule.dto'; |
| 8 | + |
| 9 | +@ApiBearerAuth('bearerAuth') |
| 10 | +@ApiSecurity('apiKey') |
| 11 | +@Controller('rules') |
| 12 | +export class RulesController { |
| 13 | + constructor(private readonly rulesService: RulesService) { } |
| 14 | + |
| 15 | + @UseGuards(JwtAuthGuard) |
| 16 | + @ApiOkResponse({ |
| 17 | + description: |
| 18 | + "Create a new rule configuration. Only the fields included in the request body will be used.", |
| 19 | + type: RuleDto, |
| 20 | + isArray: false, |
| 21 | + }) |
| 22 | + @Post() |
| 23 | + create(@Body() createRuleDto: CreateRuleDto, @Req() req) { |
| 24 | + const authHeader = req.headers?.authorization; |
| 25 | + if (!authHeader) { |
| 26 | + throw new Error('Authorization header is required'); |
| 27 | + } |
| 28 | + return this.rulesService.create(createRuleDto, req.user, authHeader); |
| 29 | + } |
| 30 | + |
| 31 | + @UseGuards(JwtAuthGuard) |
| 32 | + @ApiOkResponse({ |
| 33 | + description: |
| 34 | + "Current all of the user's rules configurations.", |
| 35 | + type: RuleDto, |
| 36 | + isArray: true, |
| 37 | + }) |
| 38 | + @Get() |
| 39 | + findAll(@Req() req) { |
| 40 | + const authHeader = req.headers?.authorization ?? ''; |
| 41 | + return this.rulesService.findAll(req.user, authHeader); |
| 42 | + } |
| 43 | + |
| 44 | + @UseGuards(JwtAuthGuard) |
| 45 | + @ApiOkResponse({ |
| 46 | + description: |
| 47 | + "Gets a user's rule configuration by ID.", |
| 48 | + type: RuleDto, |
| 49 | + isArray: false, |
| 50 | + }) |
| 51 | + @Get(':id') |
| 52 | + findOne(@Param('id') id: number, @Req() req) { |
| 53 | + const authHeader = req.headers?.authorization ?? ''; |
| 54 | + return this.rulesService.findOne(id, req.user, authHeader); |
| 55 | + } |
| 56 | + |
| 57 | + @UseGuards(JwtAuthGuard) |
| 58 | + @ApiOkResponse({ |
| 59 | + description: |
| 60 | + "Update a single rule configuration by ID. Only the fields included in the request body will be updated.", |
| 61 | + type: RuleDto, |
| 62 | + isArray: false, |
| 63 | + }) |
| 64 | + @Patch(':id') |
| 65 | + update(@Param('id') id: number, @Body() updateRuleDto: UpdateRuleDto, @Req() req) { |
| 66 | + const authHeader = req.headers?.authorization ?? ''; |
| 67 | + return this.rulesService.update(id, updateRuleDto, req.user, authHeader); |
| 68 | + } |
| 69 | + |
| 70 | + @UseGuards(JwtAuthGuard) |
| 71 | + @ApiOkResponse({ |
| 72 | + description: |
| 73 | + "Delete a user's rule configuration by ID.", |
| 74 | + type: Number, |
| 75 | + isArray: false, |
| 76 | + }) |
| 77 | + @Delete(':id') |
| 78 | + remove(@Param('id') id: number, @Req() req) { |
| 79 | + const authHeader = req.headers?.authorization ?? ''; |
| 80 | + return this.rulesService.remove(id, req.user, authHeader); |
| 81 | + } |
| 82 | +} |
0 commit comments