-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
post.controller.ts
137 lines (125 loc) · 4.13 KB
/
post.controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { Body, Delete, Get, Param, Patch, Post, Query } from "@nestjs/common";
import { Observable } from "rxjs";
import type { PaginationResponse } from "@common/@types";
import { Action } from "@common/@types";
import {
ApiPaginatedResponse,
GenericController,
LoggedInUser,
SwaggerResponse,
UUIDParam,
} from "@common/decorators";
import { CursorPaginationDto } from "@common/dtos";
import { Comment, Post as PostEntity, User } from "@entities";
import { CheckPolicies, GenericPolicyHandler } from "@lib/casl";
import { PostService } from "./post.service";
import { CreateCommentDto, CreatePostDto, EditPostDto } from "./dtos";
@GenericController("posts")
export class PostController {
constructor(private readonly postService: PostService) {}
@Get()
@ApiPaginatedResponse(PostEntity)
findAll(@Query() dto: CursorPaginationDto): Observable<PaginationResponse<PostEntity>> {
return this.postService.findAll(dto);
}
@Get(":slug")
@SwaggerResponse({
operation: "Post fetch",
notFound: "Post doesn't exist.",
params: ["slug"],
})
getById(@Param("slug") slug: string): Observable<PostEntity> {
return this.postService.findOne(slug);
}
@Get(":slug/comments")
@SwaggerResponse({
operation: "Post comment fetch",
notFound: "Post doesn't exist.",
params: ["slug"],
})
findComments(@Param("slug") slug: string): Observable<Comment[]> {
return this.postService.findComments(slug);
}
@Post()
@SwaggerResponse({ operation: "create post" })
@CheckPolicies(new GenericPolicyHandler(PostEntity, Action.Create))
create(@Body() dto: CreatePostDto, @LoggedInUser() author: User) {
return this.postService.create(dto, author);
}
@Patch(":slug")
@SwaggerResponse({
operation: "Post update",
notFound: "Post doesn't exist.",
params: ["slug"],
})
@CheckPolicies(new GenericPolicyHandler(PostEntity, Action.Update))
update(@Param("slug") slug: string, @Body() dto: EditPostDto): Observable<PostEntity> {
return this.postService.update(slug, dto);
}
@Delete(":slug")
@SwaggerResponse({
operation: "Post delete",
notFound: "Post doesn't exist.",
params: ["slug"],
})
@CheckPolicies(new GenericPolicyHandler(PostEntity, Action.Delete))
remove(@Param("slug") slug: string): Observable<PostEntity> {
return this.postService.remove(slug);
}
@Post(":slug/comments")
@SwaggerResponse({
operation: "Post comment create",
notFound: "Post doesn't exist.",
params: ["slug"],
})
@CheckPolicies(new GenericPolicyHandler(Comment, Action.Create))
createComment(
@LoggedInUser("id") user: number, @Param("slug")
slug: string, @Body()
commentData: CreateCommentDto,
) {
return this.postService.addComment(user, slug, commentData);
}
@Patch(":slug/comments/:commentIdx")
@SwaggerResponse({
operation: "Post comment edit",
notFound: "Post doesn't exist.",
params: ["slug", "commentIdx"],
})
@CheckPolicies(new GenericPolicyHandler(Comment, Action.Delete))
editComment(
@Param("slug") slug: string, @UUIDParam("commentIdx")
commentIndex: string, @Body()
commentData: CreateCommentDto,
) {
return this.postService.editComment(slug, commentIndex, commentData);
}
@Delete(":slug/comments/:commentIdx")
@SwaggerResponse({
operation: "Post comment delete",
notFound: "Post doesn't exist.",
params: ["slug", "commentIdx"],
})
@CheckPolicies(new GenericPolicyHandler(Comment, Action.Delete))
deleteComment(@Param("slug") slug: string, @UUIDParam("commentIdx") commentIndex: string) {
return this.postService.deleteComment(slug, commentIndex);
}
@Post(":slug/favorite")
@SwaggerResponse({
operation: "Post favorite",
notFound: "Post doesn't exist.",
params: ["slug"],
})
favorite(@LoggedInUser("id") userId: number, @UUIDParam("slug") slug: string) {
return this.postService.favorite(userId, slug);
}
@Delete(":slug/favorite")
@SwaggerResponse({
operation: "Post unfavorite",
notFound: "Post doesn't exist.",
params: ["slug"],
})
unFavorite(@LoggedInUser("id") userId: number, @Param("slug") slug: string) {
return this.postService.unFavorite(userId, slug);
}
}