feat(videos): add Video module in NestJS, build Video CRUD in Admin Panel, sync dynamic video fetching on frontend, and update catalog links to /catalog

This commit is contained in:
parsa aghaei committed 2026-07-27 11:54:16 +03:30
1 parent 85d5a11a5e
commit 52f0caccfc
17 files changed
+798 -53

No files matched your search

+2
View File
@@ -18,6 +18,7 @@ import { WikiModule } from './wiki/wiki.module';
import { SeoModule } from './seo/seo.module';
import { CmsModule } from './cms/cms.module';
import { WholesaleModule } from './wholesale/wholesale.module';
import { VideosModule } from './videos/videos.module';
@Module({
imports: [
@@ -40,6 +41,7 @@ import { WholesaleModule } from './wholesale/wholesale.module';
SeoModule,
CmsModule,
WholesaleModule,
VideosModule,
],
controllers: [MetricsController],
providers: [
@@ -0,0 +1,41 @@
import { IsString, IsNotEmpty, IsOptional, IsBoolean } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
export class CreateVideoDto {
@ApiProperty({ description: 'عنوان ویدئوی آموزشی', example: 'نحوه آماده‌سازی کانی‌هیدروکس GAG' })
@IsString()
@IsNotEmpty()
title: string;
@ApiProperty({ description: 'نام دکتر/ارائه‌دهنده', example: 'دکتر کلاوس هنینگ' })
@IsString()
@IsNotEmpty()
doctor: string;
@ApiPropertyOptional({ description: 'مدت زمان ویدئو', example: '۰۲:۳۰' })
@IsString()
@IsOptional()
duration?: string;
@ApiPropertyOptional({ description: 'آدرس تصویر کاور', example: 'https://example.com/thumb.jpg' })
@IsString()
@IsOptional()
thumbnail?: string;
@ApiProperty({ description: 'آدرس فایل ویدئو', example: 'https://example.com/video.mp4' })
@IsString()
@IsNotEmpty()
videoUrl: string;
@ApiPropertyOptional({ description: 'توضیحات تکمیلی ویدئو' })
@IsString()
@IsOptional()
description?: string;
@ApiPropertyOptional({ description: 'آیا ویدئوی ویژه/صفحه اصلی است؟', example: true })
@IsBoolean()
@IsOptional()
isFeatured?: boolean;
}
export class UpdateVideoDto extends CreateVideoDto {}
+51
View File
@@ -0,0 +1,51 @@
import { Controller, Get, Post, Put, Delete, Body, Param, Query, UseGuards } from '@nestjs/common';
import { VideosService } from './videos.service';
import { CreateVideoDto } from './dto/create-video.dto';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { ApiTags, ApiOperation, ApiQuery, ApiBearerAuth } from '@nestjs/swagger';
@ApiTags('Videos - مشاوره ویدئویی و آکادمی')
@Controller('videos')
export class VideosController {
constructor(private readonly videosService: VideosService) {}
@Get()
@ApiOperation({ summary: 'دریافت لیست ویدئوها (عمومی)' })
@ApiQuery({ name: 'page', required: false })
@ApiQuery({ name: 'limit', required: false })
@ApiQuery({ name: 'search', required: false })
@ApiQuery({ name: 'featured', required: false })
findAll(@Query() query: any) {
return this.videosService.findAll(query);
}
@Get(':id')
@ApiOperation({ summary: 'دریافت جزئیات یک ویدئو' })
findOne(@Param('id') id: string) {
return this.videosService.findOne(id);
}
@Post()
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'افزودن ویدئوی جدید (ادمین)' })
create(@Body() createVideoDto: CreateVideoDto) {
return this.videosService.create(createVideoDto);
}
@Put(':id')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'ویرایش ویدئو (ادمین)' })
update(@Param('id') id: string, @Body() updateVideoDto: Partial<CreateVideoDto>) {
return this.videosService.update(id, updateVideoDto);
}
@Delete(':id')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'حذف ویدئو (ادمین)' })
remove(@Param('id') id: string) {
return this.videosService.remove(id);
}
}
+10
View File
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { VideosController } from './videos.controller';
import { VideosService } from './videos.service';
@Module({
controllers: [VideosController],
providers: [VideosService],
exports: [VideosService],
})
export class VideosModule {}
+98
View File
@@ -0,0 +1,98 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { CreateVideoDto } from './dto/create-video.dto';
@Injectable()
export class VideosService {
constructor(private readonly prisma: PrismaService) {}
async findAll(query: any) {
const page = Number(query.page) || 1;
const limit = Number(query.limit) || 20;
const skip = (page - 1) * limit;
const where: any = {};
if (query.search) {
where.OR = [
{ title: { contains: query.search, mode: 'insensitive' } },
{ doctor: { contains: query.search, mode: 'insensitive' } },
{ description: { contains: query.search, mode: 'insensitive' } },
];
}
if (query.featured !== undefined) {
where.isFeatured = query.featured === 'true' || query.featured === true;
}
const [data, total] = await Promise.all([
this.prisma.video.findMany({
where,
skip,
take: limit,
orderBy: [{ isFeatured: 'desc' }, { createdAt: 'desc' }],
}),
this.prisma.video.count({ where }),
]);
return {
data,
meta: {
total,
page,
limit,
lastPage: Math.ceil(total / limit),
},
};
}
async findOne(id: string) {
const video = await this.prisma.video.findUnique({
where: { id },
});
if (!video) {
throw new NotFoundException('ویدئوی مورد نظر یافت نشد');
}
// Increment view count
await this.prisma.video.update({
where: { id },
data: { viewsCount: { increment: 1 } },
});
return video;
}
async create(dto: CreateVideoDto) {
return this.prisma.video.create({
data: {
title: dto.title,
doctor: dto.doctor,
duration: dto.duration || '۰۲:۰۰',
thumbnail: dto.thumbnail,
videoUrl: dto.videoUrl,
description: dto.description,
isFeatured: dto.isFeatured ?? false,
},
});
}
async update(id: string, dto: Partial<CreateVideoDto>) {
await this.findOne(id);
return this.prisma.video.update({
where: { id },
data: {
title: dto.title,
doctor: dto.doctor,
duration: dto.duration,
thumbnail: dto.thumbnail,
videoUrl: dto.videoUrl,
description: dto.description,
isFeatured: dto.isFeatured,
},
});
}
async remove(id: string) {
await this.findOne(id);
return this.prisma.video.delete({
where: { id },
});
}
}