chore: add is pinned to the blog entity and also add route for it
This commit is contained in:
@@ -664,4 +664,5 @@ export const enum BlogMessage {
|
|||||||
INVALID_DATE = "تاریخ نامعتبر است",
|
INVALID_DATE = "تاریخ نامعتبر است",
|
||||||
IS_ACTIVE_SHOULD_BE_1_0 = "وضعیت فعال بودن باید یکی از ۱ یا ۰ باشد",
|
IS_ACTIVE_SHOULD_BE_1_0 = "وضعیت فعال بودن باید یکی از ۱ یا ۰ باشد",
|
||||||
SEARCH_QUERY_STRING = "رشته جستجو باید یک رشته باشد",
|
SEARCH_QUERY_STRING = "رشته جستجو باید یک رشته باشد",
|
||||||
|
IS_PINNED_SHOULD_BE_A_BOOLEAN = "وضعیت سنجاق بودن باید یک بولین باشد",
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { ApiProperty } from "@nestjs/swagger";
|
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
|
||||||
import { ArrayMinSize, IsArray, IsBoolean, IsNotEmpty, IsString, IsUUID, MaxLength } from "class-validator";
|
import { ArrayMinSize, IsArray, IsBoolean, IsNotEmpty, IsOptional, IsString, IsUUID, MaxLength } from "class-validator";
|
||||||
|
|
||||||
import { BlogMessage } from "../../../common/enums/message.enum";
|
import { BlogMessage } from "../../../common/enums/message.enum";
|
||||||
|
|
||||||
@@ -55,4 +55,12 @@ export class CreateBlogDto {
|
|||||||
@IsBoolean({ message: BlogMessage.IS_ACTIVE_SHOULD_BE_A_BOOLEAN })
|
@IsBoolean({ message: BlogMessage.IS_ACTIVE_SHOULD_BE_A_BOOLEAN })
|
||||||
@ApiProperty({ description: "Is active of the blog", example: true })
|
@ApiProperty({ description: "Is active of the blog", example: true })
|
||||||
isActive: boolean;
|
isActive: boolean;
|
||||||
|
|
||||||
|
@ApiPropertyOptional({
|
||||||
|
description: "Whether the blog is pinned or not",
|
||||||
|
default: false,
|
||||||
|
})
|
||||||
|
@IsOptional()
|
||||||
|
@IsBoolean({ message: BlogMessage.IS_PINNED_SHOULD_BE_A_BOOLEAN })
|
||||||
|
isPinned?: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,6 +61,12 @@ export class BlogsController {
|
|||||||
return this.blogsService.getMostVisitedBlogs();
|
return this.blogsService.getMostVisitedBlogs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation({ summary: "Get most pinned blogs" })
|
||||||
|
@SkipAuth()
|
||||||
|
@Get("pinned")
|
||||||
|
getPinnedBlogs() {
|
||||||
|
return this.blogsService.getPinnedBlogs();
|
||||||
|
}
|
||||||
@ApiOperation({ summary: "Create a new blog (admin route)" })
|
@ApiOperation({ summary: "Create a new blog (admin route)" })
|
||||||
@Post()
|
@Post()
|
||||||
@AdminRoute()
|
@AdminRoute()
|
||||||
|
|||||||
@@ -37,6 +37,9 @@ export class Blog extends BaseEntity {
|
|||||||
@Column({ type: "int", default: 0 })
|
@Column({ type: "int", default: 0 })
|
||||||
viewCount: number;
|
viewCount: number;
|
||||||
|
|
||||||
|
@Column({ type: "boolean", default: false })
|
||||||
|
isPinned: boolean;
|
||||||
|
|
||||||
@ManyToOne(() => User, (user) => user.blogs)
|
@ManyToOne(() => User, (user) => user.blogs)
|
||||||
author: User;
|
author: User;
|
||||||
|
|
||||||
|
|||||||
@@ -262,4 +262,16 @@ export class BlogsService {
|
|||||||
const blogs = await this.blogsRepository.getMostVisitedBlogs();
|
const blogs = await this.blogsRepository.getMostVisitedBlogs();
|
||||||
return { blogs };
|
return { blogs };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getPinnedBlogs() {
|
||||||
|
const blogs = await this.blogsRepository.getPinnedBlogs();
|
||||||
|
return { blogs };
|
||||||
|
}
|
||||||
|
|
||||||
|
async togglePinnedStatus(id: string) {
|
||||||
|
const blog = await this.findBlogById(id);
|
||||||
|
blog.isPinned = !blog.isPinned;
|
||||||
|
await this.blogsRepository.save(blog);
|
||||||
|
return { message: CommonMessage.UPDATE_SUCCESS, blog };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -191,4 +191,25 @@ export class BlogsRepository extends Repository<Blog> {
|
|||||||
.take(limit)
|
.take(limit)
|
||||||
.getMany();
|
.getMany();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getPinnedBlogs(limit: number = 4) {
|
||||||
|
return this.createQueryBuilder("blog")
|
||||||
|
.where("blog.deletedAt IS NULL")
|
||||||
|
.andWhere("blog.isActive = :isActive", { isActive: true })
|
||||||
|
.andWhere("blog.isPinned = :isPinned", { isPinned: true })
|
||||||
|
.select([
|
||||||
|
"blog.id",
|
||||||
|
"blog.title",
|
||||||
|
"blog.previewContent",
|
||||||
|
"blog.metaTitle",
|
||||||
|
"blog.metaDescription",
|
||||||
|
"blog.imageUrl",
|
||||||
|
"blog.slug",
|
||||||
|
"blog.createdAt",
|
||||||
|
"blog.viewCount",
|
||||||
|
])
|
||||||
|
.orderBy("blog.createdAt", "DESC")
|
||||||
|
.take(limit)
|
||||||
|
.getMany();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user