Files
clipperia-api/src/videos/videos.service.ts
2025-08-29 01:02:24 -03:00

105 lines
2.5 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { plainToInstance } from 'class-transformer';
import { Prisma, videos, video_situation } from 'generated/prisma';
import { PrismaService } from '../prisma/prisma.service';
import { VideoResponseDto } from './dto/video-response.dto';
import { PaginatedResponse } from '../shared/dto/paginated';
@Injectable()
export class VideosService {
constructor(private readonly prisma: PrismaService) {}
async list(situation?: video_situation): Promise<VideoResponseDto[]> {
const data = await this.prisma.videos.findMany({
where: situation ? { situation } : {},
orderBy: { id: 'desc' },
select: {
id: true,
title: true,
url: true,
situation: true,
clips_quantity: true,
videoid: true,
filename: true,
datetime_download: true,
},
});
return plainToInstance(VideoResponseDto, data, {
excludeExtraneousValues: true,
});
}
async listPaginated(
page: number,
perPage: number,
direction: 'asc' | 'desc' = 'desc',
situation?: video_situation,
): Promise<PaginatedResponse<VideoResponseDto>> {
const skip = page >= 1 ? page * perPage : 0;
const where = situation ? { situation } : {};
const [rows, total] = await Promise.all([
this.prisma.videos.findMany({
where,
orderBy: { id: direction },
skip,
take: perPage ?? 1,
select: {
id: true,
title: true,
url: true,
situation: true,
clips_quantity: true,
videoid: true,
filename: true,
datetime_download: true,
},
}),
this.prisma.videos.count({ where }),
]);
const content: VideoResponseDto[] = plainToInstance(
VideoResponseDto,
rows,
{ excludeExtraneousValues: true },
);
const totalPages = Math.max(1, Math.ceil(total / perPage));
return {
content,
pagination: {
page,
direction,
perPage,
total,
totalPages,
hasNext: page < totalPages,
hasPrev: page > 1,
},
};
}
async get(id: number): Promise<videos | null> {
return this.prisma.videos.findUnique({
where: { id },
});
}
async update(id: number, data: Prisma.videosUpdateInput): Promise<videos> {
return this.prisma.videos.update({
where: { id },
data,
});
}
async delete(id: number): Promise<videos> {
return this.prisma.videos.delete({
where: { id },
});
}
}