feat(perf): implement comprehensive Core Web Vitals, ISR caching, and media optimizations
Deploy Canina / deploy (push) Successful in 1m48s
Deploy Canina / deploy (push) Successful in 1m48s
This commit is contained in:
1 parent
2a31a9f8e4
commit
fe6a900665
42 files changed
+12941
-10561
No files matched your search
@@ -2,12 +2,22 @@ import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { PaginationDto } from '../common/dto/pagination.dto';
|
||||
import { RevalidationService } from '../common/revalidation/revalidation.service';
|
||||
|
||||
@Injectable()
|
||||
export class BlogsService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
private revalidationService: RevalidationService,
|
||||
) {}
|
||||
|
||||
async getBlogs(query: PaginationDto & { status?: string; categoryId?: string; tagId?: string }) {
|
||||
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;
|
||||
@@ -37,7 +47,9 @@ export class BlogsService {
|
||||
take: limit,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
include: {
|
||||
author: { select: { id: true, firstName: true, lastName: true, email: 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 } },
|
||||
@@ -87,7 +99,10 @@ export class BlogsService {
|
||||
for (const tName of tagNames) {
|
||||
const trimmed = String(tName).trim();
|
||||
if (trimmed) {
|
||||
const slug = trimmed.toLowerCase().replace(/\s+/g, '-').replace(/[^\w\u0600-\u06FF-]+/g, '');
|
||||
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()}` },
|
||||
@@ -101,13 +116,19 @@ export class BlogsService {
|
||||
const createPayload: Prisma.BlogCreateInput = {
|
||||
...rest,
|
||||
readingTime: readingTime ? Number(readingTime) : 3,
|
||||
publishedAt: publishedAt ? new Date(publishedAt) : (rest.status === 'PUBLISHED' ? new Date() : null),
|
||||
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 } } : {}),
|
||||
...(tagConnectOrCreate.length > 0
|
||||
? { tags: { connectOrCreate: tagConnectOrCreate } }
|
||||
: {}),
|
||||
};
|
||||
|
||||
return this.prisma.blog.create({
|
||||
const created = await this.prisma.blog.create({
|
||||
data: createPayload,
|
||||
include: {
|
||||
author: { select: { firstName: true, lastName: true } },
|
||||
@@ -115,6 +136,9 @@ export class BlogsService {
|
||||
tags: true,
|
||||
},
|
||||
});
|
||||
|
||||
await this.revalidationService.revalidateBlog(created.slug);
|
||||
return created;
|
||||
}
|
||||
|
||||
async updateBlog(id: string, data: any) {
|
||||
@@ -133,8 +157,12 @@ export class BlogsService {
|
||||
|
||||
const updatePayload: Prisma.BlogUpdateInput = {
|
||||
...rest,
|
||||
...(readingTime !== undefined ? { readingTime: Number(readingTime) } : {}),
|
||||
...(publishedAt !== undefined ? { publishedAt: publishedAt ? new Date(publishedAt) : null } : {}),
|
||||
...(readingTime !== undefined
|
||||
? { readingTime: Number(readingTime) }
|
||||
: {}),
|
||||
...(publishedAt !== undefined
|
||||
? { publishedAt: publishedAt ? new Date(publishedAt) : null }
|
||||
: {}),
|
||||
...(authorId ? { author: { connect: { id: authorId } } } : {}),
|
||||
...(categoryId !== undefined
|
||||
? categoryId
|
||||
@@ -148,7 +176,10 @@ export class BlogsService {
|
||||
for (const tName of tagNames) {
|
||||
const trimmed = String(tName).trim();
|
||||
if (trimmed) {
|
||||
const slug = trimmed.toLowerCase().replace(/\s+/g, '-').replace(/[^\w\u0600-\u06FF-]+/g, '');
|
||||
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()}` },
|
||||
@@ -161,7 +192,7 @@ export class BlogsService {
|
||||
};
|
||||
}
|
||||
|
||||
return this.prisma.blog.update({
|
||||
const updated = await this.prisma.blog.update({
|
||||
where: { id },
|
||||
data: updatePayload,
|
||||
include: {
|
||||
@@ -170,18 +201,29 @@ export class BlogsService {
|
||||
tags: true,
|
||||
},
|
||||
});
|
||||
|
||||
await this.revalidationService.revalidateBlog(updated.slug);
|
||||
return updated;
|
||||
}
|
||||
|
||||
async deleteBlog(id: string) {
|
||||
const blog = await this.prisma.blog.findUnique({ where: { id } });
|
||||
if (!blog) throw new NotFoundException('Blog not found');
|
||||
return this.prisma.blog.delete({ where: { id } });
|
||||
const res = await this.prisma.blog.delete({ where: { id } });
|
||||
await this.revalidationService.revalidateBlog(blog.slug);
|
||||
return res;
|
||||
}
|
||||
|
||||
// Authors (Users who can write)
|
||||
async getAuthors() {
|
||||
return this.prisma.user.findMany({
|
||||
select: { id: true, firstName: true, lastName: true, email: true, role: true },
|
||||
select: {
|
||||
id: true,
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
email: true,
|
||||
role: true,
|
||||
},
|
||||
orderBy: { firstName: 'asc' },
|
||||
});
|
||||
}
|
||||
@@ -221,7 +263,12 @@ export class BlogsService {
|
||||
}
|
||||
|
||||
async createBlogTag(data: { name: string; slug?: string }) {
|
||||
const slug = data.slug || data.name.toLowerCase().replace(/\s+/g, '-').replace(/[^\w\u0600-\u06FF-]+/g, '');
|
||||
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()}` },
|
||||
});
|
||||
@@ -267,14 +314,31 @@ export class BlogsService {
|
||||
}
|
||||
|
||||
async updateCommentStatus(id: string, status: 'APPROVED' | 'REJECTED') {
|
||||
const comment = await this.prisma.blogComment.findUnique({ where: { id } });
|
||||
const comment = await this.prisma.blogComment.findUnique({
|
||||
where: { id },
|
||||
include: { blog: { select: { slug: true } } },
|
||||
});
|
||||
if (!comment) throw new NotFoundException('Comment not found');
|
||||
return this.prisma.blogComment.update({ where: { id }, data: { status } });
|
||||
const updated = await this.prisma.blogComment.update({
|
||||
where: { id },
|
||||
data: { status },
|
||||
});
|
||||
if (comment.blog?.slug) {
|
||||
await this.revalidationService.revalidateBlog(comment.blog.slug);
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
|
||||
async deleteComment(id: string) {
|
||||
const comment = await this.prisma.blogComment.findUnique({ where: { id } });
|
||||
const comment = await this.prisma.blogComment.findUnique({
|
||||
where: { id },
|
||||
include: { blog: { select: { slug: true } } },
|
||||
});
|
||||
if (!comment) throw new NotFoundException('Comment not found');
|
||||
return this.prisma.blogComment.delete({ where: { id } });
|
||||
const res = await this.prisma.blogComment.delete({ where: { id } });
|
||||
if (comment.blog?.slug) {
|
||||
await this.revalidationService.revalidateBlog(comment.blog.slug);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user