From 4d5ef9b40b62b131a0adf8aa30d89869af4d615e Mon Sep 17 00:00:00 2001 From: Adam Prisenznak Date: Sat, 20 Apr 2024 23:46:20 +0200 Subject: [PATCH 1/3] :sparkles: hot posts api preparation --- server/api/post/hot-posts.get.ts | 10 ++++++++++ server/services/post.service.ts | 19 +++++++++++++++++-- server/services/user.service.ts | 2 +- types/dtos/nft.ts | 12 ++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 server/api/post/hot-posts.get.ts diff --git a/server/api/post/hot-posts.get.ts b/server/api/post/hot-posts.get.ts new file mode 100644 index 0000000..964c8fd --- /dev/null +++ b/server/api/post/hot-posts.get.ts @@ -0,0 +1,10 @@ +import { usePostService } from '~/server/services/post.service'; +import type { AuthenticatedUser, PaginationDTO } from '~/types/dtos'; + +export default defineEventHandler(async (event) => { + const { jwt } = await readBody(event); + const { pageNumber, pageSize } = getQuery(event); + + const service = usePostService(jwt); + return await service.getHotPosts(pageNumber, pageSize); +}); diff --git a/server/services/post.service.ts b/server/services/post.service.ts index 3adc03f..782793d 100644 --- a/server/services/post.service.ts +++ b/server/services/post.service.ts @@ -1,5 +1,5 @@ import { useApi } from '../utils/api'; -import type { LikePostResponseDTO, NFTPost, PostLikeDTO } from '~/types/dtos'; +import type { LikePostResponseDTO, NFTPost, PostLikeDTO, UserPostResponseDTO } from '~/types/dtos'; export function usePostService(token: string) { const getMyPosts = async (pageNumber: number, pageSize: number): Promise => { @@ -29,5 +29,20 @@ export function usePostService(token: string) { return resp; }; - return { getMyPosts, like, unlike, getLikes }; + const getHotPosts = async (pageNumber: number, pageSize: number): Promise => { + const params: URLSearchParams = new URLSearchParams({ + pageNumber: String(pageNumber), + pageSize: String(pageSize), + }); + + return await useApi(`posts/hot-posts?${params.toString()}`, token); + }; + + return { + getMyPosts, + getLikes, + like, + unlike, + getHotPosts, + }; } diff --git a/server/services/user.service.ts b/server/services/user.service.ts index f4f949b..61adcf5 100644 --- a/server/services/user.service.ts +++ b/server/services/user.service.ts @@ -1,5 +1,5 @@ import { useApi } from '../utils/api'; -import { UserUpdateDTO, UserResponseDTO } from '~/types/dtos/user'; +import type { UserUpdateDTO, UserResponseDTO } from '~/types/dtos/user'; export function useUserService(token: string) { const updateUser = async (username: string, profilePicture: string): Promise => { diff --git a/types/dtos/nft.ts b/types/dtos/nft.ts index 3d3ab56..4f76fd5 100644 --- a/types/dtos/nft.ts +++ b/types/dtos/nft.ts @@ -85,3 +85,15 @@ export interface PostLikeDTO { }; postNFTAddress: string; } + +export interface UserPostResponseDTO { + id: string; + ownerAddress: string; + nft: NFTNode; + description: string; + commentCount: number; + likeCount: number; + isVisible: boolean; + isLiked: boolean; + createdAt: string; +} From 12adc3d12afc32d6b8884bc17110ed26675d2b84 Mon Sep 17 00:00:00 2001 From: Adam Prisenznak Date: Sat, 20 Apr 2024 23:53:15 +0200 Subject: [PATCH 2/3] :adhesive_bandage: fix get jwt for hot posts --- server/api/post/hot-posts.get.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/server/api/post/hot-posts.get.ts b/server/api/post/hot-posts.get.ts index 964c8fd..63855dc 100644 --- a/server/api/post/hot-posts.get.ts +++ b/server/api/post/hot-posts.get.ts @@ -1,8 +1,16 @@ import { usePostService } from '~/server/services/post.service'; -import type { AuthenticatedUser, PaginationDTO } from '~/types/dtos'; +import type { PaginationDTO } from '~/types/dtos'; +import { useLogger } from '~/server/utils/logger'; export default defineEventHandler(async (event) => { - const { jwt } = await readBody(event); + const jwt = getHeader(event, 'Authorization')?.split('Bearer ')[1]; + const logger = useLogger('POSTS::GET_HOT_POSTS'); + if (!jwt) { + logger.error('Unauthorized'); + throw createError({ + message: 'Unauthorized', + }); + } const { pageNumber, pageSize } = getQuery(event); const service = usePostService(jwt); From 5f9e734b4590ffea6a6cf605da1317cf5ab24de1 Mon Sep 17 00:00:00 2001 From: Adam Prisenznak Date: Sat, 20 Apr 2024 23:55:31 +0200 Subject: [PATCH 3/3] :adhesive_bandage: check pagination inputs for hot posts --- server/api/post/hot-posts.get.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/server/api/post/hot-posts.get.ts b/server/api/post/hot-posts.get.ts index 63855dc..aeae3cc 100644 --- a/server/api/post/hot-posts.get.ts +++ b/server/api/post/hot-posts.get.ts @@ -12,6 +12,12 @@ export default defineEventHandler(async (event) => { }); } const { pageNumber, pageSize } = getQuery(event); + if (Number.isNaN(Number(pageNumber)) || Number.isNaN(Number(pageSize))) { + logger.error('Invalid page number or page size', { pageNumber, pageSize }); + throw createError({ + message: 'Invalid page number or page size', + }); + } const service = usePostService(jwt); return await service.getHotPosts(pageNumber, pageSize);