From 44aeeccb724d81306d8374c2ea86955d1ec30545 Mon Sep 17 00:00:00 2001 From: LeoMortari Date: Mon, 3 Nov 2025 16:31:58 -0300 Subject: [PATCH] Ajusta auth e filtros de videos --- src/modules/auth/auth.controller.ts | 10 ++++- .../videos/dto/list-videos-query.dto.ts | 18 +++++--- src/modules/videos/videos.controller.ts | 8 +++- src/modules/videos/videos.service.ts | 43 ++++++++++++++++--- 4 files changed, 66 insertions(+), 13 deletions(-) diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts index 9bc8628..f25b421 100644 --- a/src/modules/auth/auth.controller.ts +++ b/src/modules/auth/auth.controller.ts @@ -1,9 +1,10 @@ -import { Controller, Post, Body, HttpCode, HttpStatus } from '@nestjs/common'; +import { Controller, Post, Body, HttpCode, HttpStatus, Get, UseGuards } from '@nestjs/common'; import LoginResponseDto from './dto/loginResponse.dto'; import { AuthService } from './auth.service'; import { LoginDto } from './dto/login.dto'; +import { KeycloakAuthGuard } from './keycloak-auth.guard'; @Controller('auth') export class AuthController { @@ -26,4 +27,11 @@ export class AuthController { ): Promise { return this.authService.refreshToken(body.refreshToken); } + + @Get('check-token') + @UseGuards(KeycloakAuthGuard) + @HttpCode(HttpStatus.OK) + async checkToken(): Promise<{ valid: boolean }> { + return { valid: true }; + } } diff --git a/src/modules/videos/dto/list-videos-query.dto.ts b/src/modules/videos/dto/list-videos-query.dto.ts index 9eb498c..51a97f6 100644 --- a/src/modules/videos/dto/list-videos-query.dto.ts +++ b/src/modules/videos/dto/list-videos-query.dto.ts @@ -4,22 +4,30 @@ import { IsString, IsBoolean, IsNumber, + IsArray, } from 'class-validator'; import { video_situation } from 'generated/prisma'; import { Transform } from 'class-transformer'; export class ListVideosQueryDto { - @IsEnum(video_situation) @IsOptional() - @Transform( - ({ value }: { value: string }) => value?.toUpperCase() as video_situation, - ) - situation?: video_situation; + @Transform(({ value }) => { + if (!value) return undefined; + if (Array.isArray(value)) { + return value.map((v) => v?.toUpperCase() as video_situation); + } + return [value?.toUpperCase() as video_situation]; + }) + situation?: video_situation | video_situation[]; @IsString() @IsOptional() title?: string; + @IsString() + @IsOptional() + videoId?: string; + @IsNumber() @IsOptional() @Transform(({ value }) => (value ? Number(value) : 1)) diff --git a/src/modules/videos/videos.controller.ts b/src/modules/videos/videos.controller.ts index 156e96a..0f087b3 100644 --- a/src/modules/videos/videos.controller.ts +++ b/src/modules/videos/videos.controller.ts @@ -31,8 +31,11 @@ export class VideosController { @Get('situacoes') @Roles('user', 'admin') - getSituacao(): video_situation[] { - return Object.values(video_situation) as video_situation[]; + getSituacao(): { label: string; value: string }[] { + return Object.values(video_situation).map((situation) => ({ + label: situation, + value: situation, + })); } @Get('search') @@ -55,6 +58,7 @@ export class VideosController { return this.videosService.list({ situation: query.situation, title: query.title, + videoId: query.videoId, }); } diff --git a/src/modules/videos/videos.service.ts b/src/modules/videos/videos.service.ts index 8b55157..c35fe8c 100644 --- a/src/modules/videos/videos.service.ts +++ b/src/modules/videos/videos.service.ts @@ -38,17 +38,36 @@ export class VideosService { async list({ situation, title, + videoId, }: { - situation?: video_situation; + situation?: video_situation | video_situation[]; title?: string; + videoId?: string; }): Promise { - const where: Prisma.videosWhereInput = situation ? { situation } : {}; + const where: Prisma.videosWhereInput = {}; + + if (situation) { + if (Array.isArray(situation)) { + where.situation = { in: situation }; + } else { + where.situation = situation; + } + } if (title) { where.title = { contains: title, + mode: 'insensitive', }; } + + if (videoId) { + where.videoid = { + contains: videoId, + mode: 'insensitive', + }; + } + const data = await this.prisma.videos.findMany({ where, orderBy: { id: 'desc' }, @@ -77,13 +96,27 @@ export class VideosService { const direction = query.direction ?? 'desc'; const skip = page > 0 ? (page - 1) * perPage : 0; - const where: Prisma.videosWhereInput = query.situation - ? { situation: query.situation } - : {}; + const where: Prisma.videosWhereInput = {}; + + if (query.situation) { + if (Array.isArray(query.situation)) { + where.situation = { in: query.situation }; + } else { + where.situation = query.situation; + } + } if (query.title) { where.title = { contains: query.title, + mode: 'insensitive', + }; + } + + if (query.videoId) { + where.videoid = { + contains: query.videoId, + mode: 'insensitive', }; }