feat(blog): complete full-stack blog module with rich text editor, SEO analyzer, JSON-LD schema, and RSS feed
Deploy Canina / deploy (push) Successful in 1m43s
Deploy Canina / deploy (push) Successful in 1m43s
This commit is contained in:
1 parent
eb63d22ac4
commit
5705354429
23 files changed
+124946
-120969
No files matched your search
@@ -34,14 +34,84 @@ export class BlogsController {
|
||||
@ApiQuery({ name: 'page', required: false })
|
||||
@ApiQuery({ name: 'limit', required: false })
|
||||
@ApiQuery({ name: 'search', required: false })
|
||||
async getBlogs(@Query() query: PaginationDto) {
|
||||
@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: Prisma.BlogCreateWithoutAuthorInput,
|
||||
@Body() body: any,
|
||||
@Request() req: { user: { id: string } },
|
||||
) {
|
||||
const data = await this.blogsService.createBlog(body, req.user.id);
|
||||
@@ -52,7 +122,7 @@ export class BlogsController {
|
||||
@ApiOperation({ summary: 'ویرایش مقاله' })
|
||||
async updateBlog(
|
||||
@Param('id') id: string,
|
||||
@Body() body: Prisma.BlogUpdateInput,
|
||||
@Body() body: any,
|
||||
) {
|
||||
const data = await this.blogsService.updateBlog(id, body);
|
||||
return { success: true, data };
|
||||
|
||||
@@ -7,14 +7,28 @@ import { PaginationDto } from '../common/dto/pagination.dto';
|
||||
export class BlogsService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async getBlogs(query: PaginationDto) {
|
||||
async getBlogs(query: PaginationDto & { status?: string; categoryId?: string; tagId?: string }) {
|
||||
const page = Number(query.page) || 1;
|
||||
const limit = Number(query.limit) || 10;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const where: Prisma.BlogWhereInput = query.search
|
||||
? { title: { contains: query.search, mode: 'insensitive' } }
|
||||
: {};
|
||||
const where: Prisma.BlogWhereInput = {};
|
||||
|
||||
if (query.search) {
|
||||
where.OR = [
|
||||
{ title: { contains: query.search, mode: 'insensitive' } },
|
||||
{ excerpt: { contains: query.search, mode: 'insensitive' } },
|
||||
{ slug: { contains: query.search, mode: 'insensitive' } },
|
||||
];
|
||||
}
|
||||
|
||||
if (query.status) {
|
||||
where.status = query.status;
|
||||
}
|
||||
|
||||
if (query.categoryId) {
|
||||
where.categoryId = query.categoryId;
|
||||
}
|
||||
|
||||
const [data, total] = await Promise.all([
|
||||
this.prisma.blog.findMany({
|
||||
@@ -23,7 +37,9 @@ export class BlogsService {
|
||||
take: limit,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
include: {
|
||||
author: { select: { firstName: true, lastName: true } },
|
||||
author: { select: { id: true, firstName: true, lastName: true, email: true } },
|
||||
category: { select: { id: true, name: true, slug: true } },
|
||||
tags: { select: { id: true, name: true, slug: true } },
|
||||
_count: { select: { comments: true } },
|
||||
},
|
||||
}),
|
||||
@@ -36,24 +52,124 @@ export class BlogsService {
|
||||
total,
|
||||
page,
|
||||
limit,
|
||||
lastPage: Math.ceil(total / limit),
|
||||
lastPage: Math.ceil(total / limit) || 1,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async createBlog(
|
||||
data: Prisma.BlogCreateWithoutAuthorInput,
|
||||
authorId: string,
|
||||
) {
|
||||
async getBlogById(id: string) {
|
||||
const blog = await this.prisma.blog.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
author: { select: { id: true, firstName: true, lastName: true } },
|
||||
category: true,
|
||||
tags: true,
|
||||
},
|
||||
});
|
||||
if (!blog) throw new NotFoundException('Blog not found');
|
||||
return blog;
|
||||
}
|
||||
|
||||
async createBlog(data: any, currentUserId: string) {
|
||||
const {
|
||||
tags,
|
||||
tagNames,
|
||||
categoryId,
|
||||
authorId,
|
||||
publishedAt,
|
||||
readingTime,
|
||||
...rest
|
||||
} = data;
|
||||
|
||||
// Connect or create tags
|
||||
const tagConnectOrCreate = [];
|
||||
if (Array.isArray(tagNames) && tagNames.length > 0) {
|
||||
for (const tName of tagNames) {
|
||||
const trimmed = String(tName).trim();
|
||||
if (trimmed) {
|
||||
const slug = trimmed.toLowerCase().replace(/\s+/g, '-').replace(/[^\w\u0600-\u06FF-]+/g, '');
|
||||
tagConnectOrCreate.push({
|
||||
where: { name: trimmed },
|
||||
create: { name: trimmed, slug: slug || `tag-${Date.now()}` },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const targetAuthorId = authorId || currentUserId;
|
||||
|
||||
const createPayload: Prisma.BlogCreateInput = {
|
||||
...rest,
|
||||
readingTime: readingTime ? Number(readingTime) : 3,
|
||||
publishedAt: publishedAt ? new Date(publishedAt) : (rest.status === 'PUBLISHED' ? new Date() : null),
|
||||
author: { connect: { id: targetAuthorId } },
|
||||
...(categoryId ? { category: { connect: { id: categoryId } } } : {}),
|
||||
...(tagConnectOrCreate.length > 0 ? { tags: { connectOrCreate: tagConnectOrCreate } } : {}),
|
||||
};
|
||||
|
||||
return this.prisma.blog.create({
|
||||
data: { ...data, author: { connect: { id: authorId } } },
|
||||
data: createPayload,
|
||||
include: {
|
||||
author: { select: { firstName: true, lastName: true } },
|
||||
category: true,
|
||||
tags: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async updateBlog(id: string, data: Prisma.BlogUpdateInput) {
|
||||
async updateBlog(id: string, data: any) {
|
||||
const blog = await this.prisma.blog.findUnique({ where: { id } });
|
||||
if (!blog) throw new NotFoundException('Blog not found');
|
||||
return this.prisma.blog.update({ where: { id }, data });
|
||||
|
||||
const {
|
||||
tags,
|
||||
tagNames,
|
||||
categoryId,
|
||||
authorId,
|
||||
publishedAt,
|
||||
readingTime,
|
||||
...rest
|
||||
} = data;
|
||||
|
||||
const updatePayload: Prisma.BlogUpdateInput = {
|
||||
...rest,
|
||||
...(readingTime !== undefined ? { readingTime: Number(readingTime) } : {}),
|
||||
...(publishedAt !== undefined ? { publishedAt: publishedAt ? new Date(publishedAt) : null } : {}),
|
||||
...(authorId ? { author: { connect: { id: authorId } } } : {}),
|
||||
...(categoryId !== undefined
|
||||
? categoryId
|
||||
? { category: { connect: { id: categoryId } } }
|
||||
: { category: { disconnect: true } }
|
||||
: {}),
|
||||
};
|
||||
|
||||
if (Array.isArray(tagNames)) {
|
||||
const tagConnectOrCreate = [];
|
||||
for (const tName of tagNames) {
|
||||
const trimmed = String(tName).trim();
|
||||
if (trimmed) {
|
||||
const slug = trimmed.toLowerCase().replace(/\s+/g, '-').replace(/[^\w\u0600-\u06FF-]+/g, '');
|
||||
tagConnectOrCreate.push({
|
||||
where: { name: trimmed },
|
||||
create: { name: trimmed, slug: slug || `tag-${Date.now()}` },
|
||||
});
|
||||
}
|
||||
}
|
||||
updatePayload.tags = {
|
||||
set: [], // clear previous relations
|
||||
connectOrCreate: tagConnectOrCreate,
|
||||
};
|
||||
}
|
||||
|
||||
return this.prisma.blog.update({
|
||||
where: { id },
|
||||
data: updatePayload,
|
||||
include: {
|
||||
author: { select: { firstName: true, lastName: true } },
|
||||
category: true,
|
||||
tags: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async deleteBlog(id: string) {
|
||||
@@ -62,6 +178,59 @@ export class BlogsService {
|
||||
return this.prisma.blog.delete({ where: { id } });
|
||||
}
|
||||
|
||||
// Authors (Users who can write)
|
||||
async getAuthors() {
|
||||
return this.prisma.user.findMany({
|
||||
select: { id: true, firstName: true, lastName: true, email: true, role: true },
|
||||
orderBy: { firstName: 'asc' },
|
||||
});
|
||||
}
|
||||
|
||||
// Blog Categories Management
|
||||
async getBlogCategories() {
|
||||
return this.prisma.blogCategory.findMany({
|
||||
orderBy: { order: 'asc' },
|
||||
include: {
|
||||
parent: true,
|
||||
children: true,
|
||||
_count: { select: { blogs: true } },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async createBlogCategory(data: Prisma.BlogCategoryCreateInput) {
|
||||
return this.prisma.blogCategory.create({ data });
|
||||
}
|
||||
|
||||
async updateBlogCategory(id: string, data: Prisma.BlogCategoryUpdateInput) {
|
||||
return this.prisma.blogCategory.update({ where: { id }, data });
|
||||
}
|
||||
|
||||
async deleteBlogCategory(id: string) {
|
||||
return this.prisma.blogCategory.delete({ where: { id } });
|
||||
}
|
||||
|
||||
// Blog Tags Management
|
||||
async getBlogTags() {
|
||||
return this.prisma.blogTag.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
include: {
|
||||
_count: { select: { blogs: true } },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async createBlogTag(data: { name: string; slug?: string }) {
|
||||
const slug = data.slug || data.name.toLowerCase().replace(/\s+/g, '-').replace(/[^\w\u0600-\u06FF-]+/g, '');
|
||||
return this.prisma.blogTag.create({
|
||||
data: { name: data.name, slug: slug || `tag-${Date.now()}` },
|
||||
});
|
||||
}
|
||||
|
||||
async deleteBlogTag(id: string) {
|
||||
return this.prisma.blogTag.delete({ where: { id } });
|
||||
}
|
||||
|
||||
// Blog Comments Management
|
||||
async getComments(query: {
|
||||
page?: number;
|
||||
@@ -93,7 +262,7 @@ export class BlogsService {
|
||||
|
||||
return {
|
||||
data,
|
||||
meta: { total, page, limit, lastPage: Math.ceil(total / limit) },
|
||||
meta: { total, page, limit, lastPage: Math.ceil(total / limit) || 1 },
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user