| 1 | import { Controller, Get, Logger, Param, Res } from '@nestjs/common' |
| 2 | import { ApiTags } from '@nestjs/swagger' |
| 3 | import { Public } from '@yikart/aitoearn-auth' |
| 4 | import { ApiDoc } from '@yikart/common' |
| 5 | import { Response } from 'express' |
| 6 | import { ShortLinkService } from './short-link.service' |
| 7 | |
| 8 | @ApiTags('ShortLink') |
| 9 | @Controller('shortLink') |
| 10 | export class ShortLinkController { |
| 11 | private readonly logger = new Logger(ShortLinkController.name) |
| 12 | |
| 13 | constructor( |
| 14 | private readonly shortLinkService: ShortLinkService, |
| 15 | ) {} |
| 16 | |
| 17 | @ApiDoc({ |
| 18 | summary: 'Redirect short link to original URL', |
| 19 | }) |
| 20 | @Public() |
| 21 | @Get('/:code') |
| 22 | async redirect( |
| 23 | @Param('code') code: string, |
| 24 | @Res() res: Response, |
| 25 | ) { |
| 26 | this.logger.log(`Redirecting short link: ${code}`) |
| 27 | const originalUrl = await this.shortLinkService.getByCode(code) |
| 28 | res.redirect(originalUrl) |
| 29 | } |
| 30 | } |
| 31 |