Skip to content

Commit 613eb6f

Browse files
committed
✨ post & comment likeing
1 parent cf38c4c commit 613eb6f

File tree

10 files changed

+97
-39
lines changed

10 files changed

+97
-39
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"sidebase",
2020
"tailwindcss",
2121
"vueuc",
22-
"wght"
22+
"wght",
23+
"dtos"
2324
],
2425
"files.eol": "\n",
2526
"[typescript]": {
File renamed without changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { commentService } from '~/server/services/comment.service';
2+
import { AuthenticatedUser } from '~/types/auth';
3+
4+
export default defineEventHandler(async (event) => {
5+
const { jwt } = await readBody<AuthenticatedUser>(event);
6+
const commentId = getRouterParam(event, 'id');
7+
if (Number.isNaN(commentId)) {
8+
return createError({
9+
message: 'Invalid comment id',
10+
});
11+
}
12+
13+
const { getLikes } = commentService(jwt);
14+
return await getLikes(Number(commentId));
15+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { commentService } from '~/server/services/comment.service';
2+
import { AuthenticatedUser } from '~/types/auth';
3+
4+
export default defineEventHandler(async (event) => {
5+
const { jwt } = await readBody<AuthenticatedUser>(event);
6+
const commentId = getRouterParam(event, 'id');
7+
if (Number.isNaN(commentId)) {
8+
return createError({
9+
message: 'Invalid comment id',
10+
});
11+
}
12+
13+
const { likeComment } = commentService(jwt);
14+
return await likeComment(Number(commentId));
15+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { postService } from '~/server/services/post.service';
2+
import { AuthenticatedUser } from '~/types/auth';
3+
4+
export default defineEventHandler(async (event) => {
5+
const { jwt } = await readBody<AuthenticatedUser>(event);
6+
const nftAddress = getRouterParam(event, 'id');
7+
if (!nftAddress) {
8+
return createError({
9+
message: 'Invalid NFT address',
10+
});
11+
}
12+
13+
const { getLikes } = postService(jwt);
14+
return await getLikes(nftAddress);
15+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { postService } from '~/server/services/post.service';
2+
import { AuthenticatedUser } from '~/types/auth';
3+
4+
export default defineEventHandler(async (event) => {
5+
const { jwt } = await readBody<AuthenticatedUser>(event);
6+
const nftAddress = getRouterParam(event, 'id');
7+
if (!nftAddress) {
8+
return createError({
9+
message: 'Invalid NFT address',
10+
});
11+
}
12+
13+
const { likePost } = postService(jwt);
14+
return await likePost(nftAddress);
15+
});

server/services/comment.service.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useApi } from '../utils/api';
2-
import { AddCommentDTO, CommentDTO, UpdateCommentDTO } from '~/types/dtos';
2+
import { AddCommentDTO, CommentDTO, CommentLikeDTO, UpdateCommentDTO } from '~/types/dtos';
33

44
export function commentService(token: string) {
55
const getAll = async (nftAddress: string, pageNumber: number, pageSize: number): Promise<CommentDTO[]> => {
@@ -39,8 +39,20 @@ export function commentService(token: string) {
3939
return resp;
4040
};
4141

42+
const getLikes = async (commentId: number): Promise<CommentLikeDTO[]> => {
43+
return await useApi<CommentLikeDTO[]>(`post/comments/${commentId}/likes`, token);
44+
};
45+
46+
const likeComment = async (commentId: number): Promise<CommentLikeDTO> => {
47+
return await useApi<CommentLikeDTO>(`post/comments/${commentId}/ likes`, token, {
48+
method: 'POST',
49+
});
50+
};
51+
4252
return {
4353
getAll,
54+
getLikes,
55+
likeComment,
4456
add,
4557
update,
4658
remove,

server/services/post.service.ts

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useApi } from '../utils/api';
2-
import { AddCommentDTO, CommentDTO, LikePostResponseDTO, NFTPost, UpdateCommentDTO } from '~/types/dtos';
2+
import { LikePostResponseDTO, NFTPost, PostLikeDTO } from '~/types/dtos';
33

44
export function postService(token: string) {
55
const getMyPosts = async (pageNumber: number, pageSize: number): Promise<NFTPost[]> => {
@@ -11,41 +11,8 @@ export function postService(token: string) {
1111
return await useApi<NFTPost[]>(`post/my?${params.toString()}`, token);
1212
};
1313

14-
const getNftComments = async (nftAddress: string, pageNumber: number, pageSize: number): Promise<CommentDTO[]> => {
15-
const params: URLSearchParams = new URLSearchParams({
16-
pageNumber: String(pageNumber),
17-
pageSize: String(pageSize),
18-
});
19-
20-
return await useApi<CommentDTO[]>(`post/${nftAddress}/comments?${params.toString()}`, token);
21-
};
22-
23-
const addComment = async (nftAddress: string, content: string, parentCommentId: number): Promise<CommentDTO> => {
24-
const resp = await useApi<CommentDTO, AddCommentDTO>(`post/${nftAddress}/comments`, token, {
25-
method: 'POST',
26-
body: {
27-
parentCommentId,
28-
content,
29-
},
30-
});
31-
return resp;
32-
};
33-
34-
const updateComment = async (commentId: number, content: string): Promise<CommentDTO> => {
35-
const resp = await useApi<CommentDTO, UpdateCommentDTO>(`post/comments/${commentId}`, token, {
36-
method: 'PUT',
37-
body: {
38-
content,
39-
},
40-
});
41-
return resp;
42-
};
43-
44-
const deleteComment = async (commentId: number) => {
45-
const resp = await useApi(`post/comments/${commentId}`, token, {
46-
method: 'DELETE',
47-
});
48-
return resp;
14+
const getLikes = async (nftAddress: string): Promise<PostLikeDTO[]> => {
15+
return await useApi<PostLikeDTO[]>(`post/${nftAddress}/likes}`, token);
4916
};
5017

5118
const likePost = async (nftAddress: string): Promise<LikePostResponseDTO> => {
@@ -55,5 +22,5 @@ export function postService(token: string) {
5522
return resp;
5623
};
5724

58-
return { getMyPosts, addComment, getNftComments, updateComment, deleteComment, likePost };
25+
return { getMyPosts, likePost, getLikes };
5926
}

types/dtos/nft.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,21 @@ export interface LikePostResponseDTO {
6767
likerAddress: string;
6868
postNFTAddress: string;
6969
}
70+
71+
export interface CommentLikeDTO {
72+
id: number;
73+
liker: {
74+
address: string;
75+
username: string;
76+
};
77+
commentId: number;
78+
}
79+
80+
export interface PostLikeDTO {
81+
id: number;
82+
liker: {
83+
address: string;
84+
username: string;
85+
};
86+
postNFTAddress: string;
87+
}

0 commit comments

Comments
 (0)