84 lines
2.0 KiB
TypeScript
84 lines
2.0 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Param,
|
|
Query,
|
|
Patch,
|
|
Body,
|
|
Delete,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
import { Prisma, videos, video_situation } from 'generated/prisma';
|
|
|
|
import { VideosService } from './videos.service';
|
|
import { VideoResponseDto } from './dto/video-response.dto';
|
|
import { KeycloakAuthGuard } from '../auth/keycloak-auth.guard';
|
|
import { Roles } from '../auth/decorator/roles.decorator';
|
|
import { ListVideosQueryDto } from './dto/list-videos-query.dto';
|
|
import { VideoMetadataDto } from 'src/shared/dto/video-metadata';
|
|
|
|
type PaginatedResponse<T> = {
|
|
content: T[];
|
|
pagination: {
|
|
page: number;
|
|
perPage: number;
|
|
total: number;
|
|
totalPages: number;
|
|
hasNext: boolean;
|
|
hasPrev: boolean;
|
|
};
|
|
};
|
|
|
|
@Controller('videos')
|
|
@UseGuards(KeycloakAuthGuard)
|
|
export class VideosController {
|
|
constructor(private readonly videosService: VideosService) {}
|
|
|
|
@Get('situacoes')
|
|
@Roles('user', 'admin')
|
|
getSituacao(): video_situation[] {
|
|
return Object.values(video_situation) as video_situation[];
|
|
}
|
|
|
|
@Get('search')
|
|
@Roles('user', 'admin')
|
|
getVideoMetadata(
|
|
@Query() { url }: { url: string },
|
|
): Promise<VideoMetadataDto> {
|
|
return this.videosService.getVideoMetadata(url);
|
|
}
|
|
|
|
@Get()
|
|
@Roles('user', 'admin')
|
|
async list(
|
|
@Query() query: ListVideosQueryDto,
|
|
): Promise<PaginatedResponse<VideoResponseDto> | VideoResponseDto[]> {
|
|
if (query.pageable || query.page || query.perPage) {
|
|
return this.videosService.listPaginated(query);
|
|
}
|
|
|
|
return this.videosService.list({
|
|
situation: query.situation,
|
|
title: query.title,
|
|
});
|
|
}
|
|
|
|
@Get(':id')
|
|
async get(@Param('id') id: string): Promise<videos | null> {
|
|
return this.videosService.get(Number(id));
|
|
}
|
|
|
|
@Patch(':id')
|
|
async update(
|
|
@Param('id') id: string,
|
|
@Body() body: Prisma.videosUpdateInput,
|
|
): Promise<videos> {
|
|
return this.videosService.update(Number(id), body);
|
|
}
|
|
|
|
@Delete(':id')
|
|
async delete(@Param('id') id: string): Promise<videos> {
|
|
return this.videosService.delete(Number(id));
|
|
}
|
|
}
|