Files
canina/backend/src/admin/blogs.controller.ts
T

177 lines
5.1 KiB
TypeScript

import {
Controller,
Get,
Post,
Put,
Patch,
Delete,
Body,
Param,
Query,
UseGuards,
Request,
} from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { BlogsService } from './blogs.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import {
ApiTags,
ApiBearerAuth,
ApiOperation,
ApiQuery,
} from '@nestjs/swagger';
import { PaginationDto } from '../common/dto/pagination.dto';
@ApiTags('Admin - مدیریت مقالات (بلاگ)')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('admin/blogs')
export class BlogsController {
constructor(private readonly blogsService: BlogsService) {}
@Get()
@ApiOperation({ summary: 'لیست مقالات' })
@ApiQuery({ name: 'page', required: false })
@ApiQuery({ name: 'limit', required: false })
@ApiQuery({ name: 'search', required: false })
@ApiQuery({ name: 'status', required: false })
@ApiQuery({ name: 'categoryId', required: false })
async getBlogs(
@Query() query: PaginationDto & { status?: string; categoryId?: string; tagId?: string },
) {
return this.blogsService.getBlogs(query);
}
@Get('authors')
@ApiOperation({ summary: 'لیست کاربران برای انتخاب به عنوان نویسنده' })
async getAuthors() {
const data = await this.blogsService.getAuthors();
return { success: true, data };
}
@Get('categories')
@ApiOperation({ summary: 'لیست دسته‌بندی‌های وبلاگ' })
async getBlogCategories() {
const data = await this.blogsService.getBlogCategories();
return { success: true, data };
}
@Post('categories')
@ApiOperation({ summary: 'ایجاد دسته‌بندی جدید برای وبلاگ' })
async createBlogCategory(@Body() body: Prisma.BlogCategoryCreateInput) {
const data = await this.blogsService.createBlogCategory(body);
return { success: true, data };
}
@Put('categories/:id')
@ApiOperation({ summary: 'ویرایش دسته‌بندی وبلاگ' })
async updateBlogCategory(
@Param('id') id: string,
@Body() body: Prisma.BlogCategoryUpdateInput,
) {
const data = await this.blogsService.updateBlogCategory(id, body);
return { success: true, data };
}
@Delete('categories/:id')
@ApiOperation({ summary: 'حذف دسته‌بندی وبلاگ' })
async deleteBlogCategory(@Param('id') id: string) {
await this.blogsService.deleteBlogCategory(id);
return { success: true };
}
@Get('tags')
@ApiOperation({ summary: 'لیست تگ‌های وبلاگ' })
async getBlogTags() {
const data = await this.blogsService.getBlogTags();
return { success: true, data };
}
@Post('tags')
@ApiOperation({ summary: 'ایجاد تگ جدید برای وبلاگ' })
async createBlogTag(@Body() body: { name: string; slug?: string }) {
const data = await this.blogsService.createBlogTag(body);
return { success: true, data };
}
@Delete('tags/:id')
@ApiOperation({ summary: 'حذف تگ وبلاگ' })
async deleteBlogTag(@Param('id') id: string) {
await this.blogsService.deleteBlogTag(id);
return { success: true };
}
@Get(':id')
@ApiOperation({ summary: 'دریافت مشخصات یک مقاله' })
async getBlogById(@Param('id') id: string) {
const data = await this.blogsService.getBlogById(id);
return { success: true, data };
}
@Post()
@ApiOperation({ summary: 'ایجاد مقاله جدید' })
async createBlog(
@Body() body: any,
@Request() req: { user: { id: string } },
) {
const data = await this.blogsService.createBlog(body, req.user.id);
return { success: true, data };
}
@Put(':id')
@ApiOperation({ summary: 'ویرایش مقاله' })
async updateBlog(
@Param('id') id: string,
@Body() body: any,
) {
const data = await this.blogsService.updateBlog(id, body);
return { success: true, data };
}
@Delete(':id')
@ApiOperation({ summary: 'حذف مقاله' })
async deleteBlog(@Param('id') id: string) {
await this.blogsService.deleteBlog(id);
return { success: true };
}
// Blog Comments endpoints
@Get('comments')
@ApiOperation({ summary: 'لیست نظرات مقالات' })
@ApiQuery({
name: 'status',
required: false,
description: 'PENDING | APPROVED | REJECTED',
})
@ApiQuery({ name: 'blogId', required: false })
@ApiQuery({ name: 'page', required: false })
@ApiQuery({ name: 'limit', required: false })
async getComments(
@Query()
query: {
page?: number;
limit?: number;
status?: string;
blogId?: string;
},
) {
return this.blogsService.getComments(query);
}
@Patch('comments/:id/status')
@ApiOperation({ summary: 'تغییر وضعیت نظر (تأیید/رد)' })
async updateCommentStatus(
@Param('id') id: string,
@Body() body: { status: 'APPROVED' | 'REJECTED' },
) {
return this.blogsService.updateCommentStatus(id, body.status);
}
@Delete('comments/:id')
@ApiOperation({ summary: 'حذف نظر' })
async deleteComment(@Param('id') id: string) {
await this.blogsService.deleteComment(id);
return { success: true };
}
}