update: add toggle status of blog
This commit is contained in:
@@ -61,12 +61,29 @@ export class BlogsController {
|
||||
return this.blogsService.getMostVisitedBlogs();
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "toggle status of blogs (admin route)" })
|
||||
@PermissionsDec(PermissionEnum.BLOGS)
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@Post(":id/toggle-status")
|
||||
toggleBlogStatus(@Param() paramDto: ParamDto) {
|
||||
return this.blogsService.toggleBlogStatus(paramDto.id);
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "Get most pinned blogs" })
|
||||
@SkipAuth()
|
||||
@Get("pinned")
|
||||
getPinnedBlogs() {
|
||||
return this.blogsService.getPinnedBlogs();
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "Toggle pinned status of a blog (admin route)" })
|
||||
@AdminRoute()
|
||||
@PermissionsDec(PermissionEnum.BLOGS)
|
||||
@Post(":id/toggle-pinned")
|
||||
togglePinnedStatus(@Param() paramDto: ParamDto) {
|
||||
return this.blogsService.togglePinnedStatus(paramDto.id);
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "Create a new blog (admin route)" })
|
||||
@Post()
|
||||
@AdminRoute()
|
||||
|
||||
@@ -244,13 +244,6 @@ export class BlogsService {
|
||||
return category;
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
private async findBlogById(id: string) {
|
||||
const blog = await this.blogsRepository.findOneById(id);
|
||||
if (!blog) throw new BadRequestException(BlogMessage.BLOG_NOT_FOUND);
|
||||
return blog;
|
||||
}
|
||||
|
||||
async incrementViewCount(id: string) {
|
||||
const blog = await this.findBlogById(id);
|
||||
blog.viewCount += 1;
|
||||
@@ -267,6 +260,7 @@ export class BlogsService {
|
||||
const blogs = await this.blogsRepository.getPinnedBlogs();
|
||||
return { blogs };
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async togglePinnedStatus(id: string) {
|
||||
const blog = await this.findBlogById(id);
|
||||
@@ -274,4 +268,21 @@ export class BlogsService {
|
||||
await this.blogsRepository.save(blog);
|
||||
return { message: CommonMessage.UPDATE_SUCCESS, blog };
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async toggleBlogStatus(id: string) {
|
||||
const blog = await this.findBlogById(id);
|
||||
blog.isActive = !blog.isActive;
|
||||
|
||||
//
|
||||
await this.blogsRepository.save(blog);
|
||||
return { message: CommonMessage.UPDATE_SUCCESS, blog };
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
private async findBlogById(id: string) {
|
||||
const blog = await this.blogsRepository.findOneById(id);
|
||||
if (!blog) throw new BadRequestException(BlogMessage.BLOG_NOT_FOUND);
|
||||
return blog;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user