Ajusta endpoints
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import dayjs from 'dayjs';
|
||||
import { plainToInstance } from 'class-transformer';
|
||||
|
||||
import { Prisma, videos, EVideoSituation } from 'generated/prisma';
|
||||
import { Prisma, videos, video_situation } from 'generated/prisma';
|
||||
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { VideoResponseDto } from './dto/video-response.dto';
|
||||
@@ -11,37 +11,42 @@ import { PaginatedResponse } from './dto/paginated.dto';
|
||||
export class VideosService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async list(): Promise<VideoResponseDto[]> {
|
||||
const data = await this.prisma.videos.findMany({ orderBy: { id: 'desc' } });
|
||||
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 data.map((video) => ({
|
||||
id: video.id,
|
||||
title: video.title,
|
||||
url: video.url,
|
||||
situation: video.situation,
|
||||
clips_quantity: video.clips_quantity ?? 0,
|
||||
videoid: video.videoid!,
|
||||
filename: video.filename || '',
|
||||
datetime_download: video.datetime_download
|
||||
? dayjs(video.datetime_download).format('DD/MM/YYYY HH:mm:ss')
|
||||
: '',
|
||||
}));
|
||||
return plainToInstance(VideoResponseDto, data, {
|
||||
excludeExtraneousValues: true,
|
||||
});
|
||||
}
|
||||
|
||||
async listPaginated(
|
||||
page: number,
|
||||
perPage: number,
|
||||
situation?: EVideoSituation,
|
||||
direction: 'asc' | 'desc' = 'desc',
|
||||
situation?: video_situation,
|
||||
): Promise<PaginatedResponse<VideoResponseDto>> {
|
||||
const skip = (page - 1) * perPage;
|
||||
const where = situation ? { situation } : undefined;
|
||||
const skip = page >= 1 ? page * perPage : 0;
|
||||
const where = situation ? { situation } : {};
|
||||
|
||||
const [rows, total] = await Promise.all([
|
||||
this.prisma.videos.findMany({
|
||||
where,
|
||||
orderBy: { id: 'asc' },
|
||||
orderBy: { id: direction },
|
||||
skip,
|
||||
take: Number(perPage),
|
||||
take: perPage ?? 1,
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
@@ -56,16 +61,11 @@ export class VideosService {
|
||||
this.prisma.videos.count({ where }),
|
||||
]);
|
||||
|
||||
const content: VideoResponseDto[] = rows.map((row) => ({
|
||||
id: row.id,
|
||||
title: row.title ?? null,
|
||||
url: row.url,
|
||||
situation: row.situation,
|
||||
clips_quantity: row.clips_quantity ?? 0,
|
||||
videoid: row.videoid ?? '',
|
||||
filename: row.filename ?? '',
|
||||
datetime_download: row.datetime_download ?? '',
|
||||
}));
|
||||
const content: VideoResponseDto[] = plainToInstance(
|
||||
VideoResponseDto,
|
||||
rows,
|
||||
{ excludeExtraneousValues: true },
|
||||
);
|
||||
|
||||
const totalPages = Math.max(1, Math.ceil(total / perPage));
|
||||
|
||||
@@ -73,6 +73,7 @@ export class VideosService {
|
||||
content,
|
||||
pagination: {
|
||||
page,
|
||||
direction,
|
||||
perPage,
|
||||
total,
|
||||
totalPages,
|
||||
@@ -100,11 +101,4 @@ export class VideosService {
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
|
||||
async listBySituation(situation: EVideoSituation): Promise<videos[]> {
|
||||
return this.prisma.videos.findMany({
|
||||
where: { situation },
|
||||
orderBy: { id: 'desc' },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user