215 lines
8.0 KiB
TypeScript
Executable File
215 lines
8.0 KiB
TypeScript
Executable File
import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Param, Patch, Post, Query } from "@nestjs/common";
|
|
import { ApiOperation } from "@nestjs/swagger";
|
|
|
|
import { BlogCommentQueryDto } from "./DTO/blog-comment-query.dto";
|
|
import { CreateBlogDto } from "./DTO/create-blog.dto";
|
|
import { CreateBlogCategoryDto } from "./DTO/create-category.dto";
|
|
import { CreateCommentReplyDto } from "./DTO/create-comment-reply.dto";
|
|
import { CreateCommentDto } from "./DTO/create-comment.dto";
|
|
import { BlogSlugDto } from "./DTO/get-blog-by-slug.dto";
|
|
import { BlogListSearchQueryDto, BlogSearchQueryDto } from "./DTO/search-blog-query.dto";
|
|
import { UpdateBlogDto } from "./DTO/update-blog.dto";
|
|
import { UpdateBlogCategoryDto } from "./DTO/update-category.dto";
|
|
import { UpdateCommentStatusDto } from "./DTO/update-comment-status.dto";
|
|
import { BlogCommentService } from "./providers/blog-comment.service";
|
|
import { BlogsService } from "./providers/blogs.service";
|
|
import { AdminRoute } from "../../common/decorators/admin.decorator";
|
|
import { AuthGuards } from "../../common/decorators/auth-guard.decorator";
|
|
import { Pagination } from "../../common/decorators/pagination.decorator";
|
|
import { PermissionsDec } from "../../common/decorators/permission.decorator";
|
|
import { SkipAuth } from "../../common/decorators/skip-auth.decorator";
|
|
import { UserDec } from "../../common/decorators/user.decorator";
|
|
import { ParamDto } from "../../common/DTO/param.dto";
|
|
import { CategoryListSearchQueryDto } from "../danak-services/DTO/category-search-query.dto";
|
|
import { PermissionEnum } from "../users/enums/permission.enum";
|
|
@Controller("blogs")
|
|
@AuthGuards()
|
|
export class BlogsController {
|
|
constructor(
|
|
private readonly blogsService: BlogsService,
|
|
private readonly blogCommentService: BlogCommentService,
|
|
) {}
|
|
|
|
@ApiOperation({ summary: "Get all public and active blogs with category id" })
|
|
@SkipAuth()
|
|
@Get()
|
|
getBlogsPublic(@Query() queryDto: BlogSearchQueryDto) {
|
|
return this.blogsService.getBlogsPublic(queryDto);
|
|
}
|
|
|
|
@ApiOperation({ summary: "Get most visited and pinned blogs" })
|
|
@SkipAuth()
|
|
@Get("combined")
|
|
getCombinedBlogs() {
|
|
return this.blogsService.getCombinedBlogs();
|
|
}
|
|
|
|
@ApiOperation({ summary: "Get all blogs (admin route)" })
|
|
@AdminRoute()
|
|
@PermissionsDec(PermissionEnum.BLOGS)
|
|
@Get("list")
|
|
getBlogList(@Query() queryDto: BlogListSearchQueryDto) {
|
|
return this.blogsService.getBlogList(queryDto);
|
|
}
|
|
|
|
@ApiOperation({ summary: "Get a blog by id" })
|
|
@SkipAuth()
|
|
@Get(":id")
|
|
getBlog(@Param() paramDto: ParamDto, @UserDec("isAdmin") isAdmin: boolean) {
|
|
return this.blogsService.getBlogById(paramDto.id, isAdmin);
|
|
}
|
|
|
|
@ApiOperation({ summary: "Get a blog by slug" })
|
|
@SkipAuth()
|
|
@Get("slug/:slug")
|
|
getBlogBySlug(@Param() paramDto: BlogSlugDto, @UserDec("isAdmin") isAdmin: boolean) {
|
|
return this.blogsService.getBlogBySlug(paramDto.slug, isAdmin);
|
|
}
|
|
|
|
@ApiOperation({ summary: "Get most visited blogs" })
|
|
@SkipAuth()
|
|
@Get("most-visited")
|
|
getMostVisitedBlogs() {
|
|
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()
|
|
@PermissionsDec(PermissionEnum.BLOGS)
|
|
createBlog(@Body() createBlogDto: CreateBlogDto, @UserDec("id") userId: string) {
|
|
return this.blogsService.createBlog(createBlogDto, userId);
|
|
}
|
|
|
|
@ApiOperation({ summary: "Update a blog (admin route)" })
|
|
@Patch(":id")
|
|
@AdminRoute()
|
|
@PermissionsDec(PermissionEnum.BLOGS)
|
|
updateBlog(@Param() paramDto: ParamDto, @Body() updateBlogDto: UpdateBlogDto, @UserDec("id") userId: string) {
|
|
return this.blogsService.updateBlog(paramDto.id, updateBlogDto, userId);
|
|
}
|
|
|
|
@ApiOperation({ summary: "Delete a blog (admin route) " })
|
|
@Delete(":id")
|
|
@AdminRoute()
|
|
@PermissionsDec(PermissionEnum.BLOGS)
|
|
deleteBlog(@Param() paramDto: ParamDto, @UserDec("id") userId: string) {
|
|
return this.blogsService.deleteBlog(paramDto.id, userId);
|
|
}
|
|
|
|
//-------------- comments --------------
|
|
|
|
@ApiOperation({ summary: "Create a new comment" })
|
|
@Post(":id/comments")
|
|
createComment(@Param() paramDto: ParamDto, @Body() createCommentDto: CreateCommentDto, @UserDec("id") userId: string) {
|
|
return this.blogCommentService.createComment(paramDto.id, createCommentDto, userId);
|
|
}
|
|
|
|
@ApiOperation({ summary: "Add a reply to a comment" })
|
|
@Post("comments/:id/replies")
|
|
addCommentReply(@Param() paramDto: ParamDto, @Body() createCommentReplyDto: CreateCommentReplyDto, @UserDec("id") userId: string) {
|
|
return this.blogCommentService.addCommentReply(paramDto.id, createCommentReplyDto, userId);
|
|
}
|
|
|
|
@ApiOperation({ summary: "Get all comments for a blog (admin route)" })
|
|
@AdminRoute()
|
|
@PermissionsDec(PermissionEnum.BLOGS)
|
|
@Get(":id/comments")
|
|
getBlogCommentsForAdmin(@Param() paramDto: ParamDto) {
|
|
return this.blogCommentService.getBlogCommentsForAdmin(paramDto.id);
|
|
}
|
|
|
|
@ApiOperation({ summary: "Get all comments for a blog (admin route)" })
|
|
@AdminRoute()
|
|
@PermissionsDec(PermissionEnum.BLOGS)
|
|
@Get("comments")
|
|
getAllCommentForAdmin(@Query() queryDto: BlogCommentQueryDto) {
|
|
return this.blogCommentService.getAllCommentForAdmin(queryDto);
|
|
}
|
|
|
|
@ApiOperation({ summary: "Update the status of a comment (admin route)" })
|
|
@AdminRoute()
|
|
@PermissionsDec(PermissionEnum.BLOGS)
|
|
@Patch("comments/:id/status")
|
|
updateCommentStatus(@Param() paramDto: ParamDto, @Body() updateCommentStatusDto: UpdateCommentStatusDto) {
|
|
return this.blogCommentService.updateCommentStatus(paramDto.id, updateCommentStatusDto);
|
|
}
|
|
|
|
//-------------- categories --------------
|
|
|
|
@ApiOperation({ summary: "Get all active categories (admin route)" })
|
|
@Pagination()
|
|
@Get("categories/list")
|
|
getAllActiveCategories(@Query() queryDto: CategoryListSearchQueryDto) {
|
|
return this.blogsService.getCategories(queryDto);
|
|
}
|
|
|
|
@ApiOperation({ summary: "Get all active categories (user side)" })
|
|
@SkipAuth()
|
|
@Get("categories/public")
|
|
getCategoriesUserSide() {
|
|
return this.blogsService.getCategoriesUserSide();
|
|
}
|
|
|
|
@ApiOperation({ summary: "Get a category by id" })
|
|
@Get("categories/:id")
|
|
getCategory(@Param() paramDto: ParamDto) {
|
|
return this.blogsService.getCategory(paramDto.id);
|
|
}
|
|
|
|
@ApiOperation({ summary: "Create a new category (admin route)" })
|
|
@Post("categories")
|
|
@AdminRoute()
|
|
@PermissionsDec(PermissionEnum.BLOGS)
|
|
createCategory(@Body() createCategoryDto: CreateBlogCategoryDto) {
|
|
return this.blogsService.createCategory(createCategoryDto);
|
|
}
|
|
|
|
@ApiOperation({ summary: "Update a category (admin route)" })
|
|
@Patch("categories/:id")
|
|
@AdminRoute()
|
|
@PermissionsDec(PermissionEnum.BLOGS)
|
|
updateCategory(@Param() paramDto: ParamDto, @Body() updateCategoryDto: UpdateBlogCategoryDto) {
|
|
return this.blogsService.updateCategory(paramDto.id, updateCategoryDto);
|
|
}
|
|
|
|
@ApiOperation({ summary: "Delete a category (admin route)" })
|
|
@Delete("categories/:id")
|
|
@AdminRoute()
|
|
@PermissionsDec(PermissionEnum.BLOGS)
|
|
deleteCategory(@Param() paramDto: ParamDto, @UserDec("id") userId: string) {
|
|
return this.blogsService.deleteCategory(paramDto.id, userId);
|
|
}
|
|
|
|
@ApiOperation({ summary: "toggle status of categories (admin route)" })
|
|
@PermissionsDec(PermissionEnum.BLOGS)
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post("categories/toggle-status/:id")
|
|
toggleCategoryStatus(@Param() paramDto: ParamDto) {
|
|
return this.blogsService.toggleCategoryStatus(paramDto.id);
|
|
}
|
|
}
|