Skip to content

Commit

Permalink
Merge pull request #21 from fiit-tp7-2023/AB#308-prepare-nuxt-endpoin…
Browse files Browse the repository at this point in the history
…t-for-fetching-post-data

✨ hot posts api preparation
  • Loading branch information
bran0h authored Apr 20, 2024
2 parents aa5dbe5 + 9616a77 commit a961256
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
24 changes: 24 additions & 0 deletions server/api/post/hot-posts.get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { usePostService } from '~/server/services/post.service';
import type { PaginationDTO } from '~/types/dtos';
import { useLogger } from '~/server/utils/logger';

export default defineEventHandler(async (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<PaginationDTO>(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);
});
19 changes: 17 additions & 2 deletions server/services/post.service.ts
Original file line number Diff line number Diff line change
@@ -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<NFTPost[]> => {
Expand Down Expand Up @@ -29,5 +29,20 @@ export function usePostService(token: string) {
return resp;
};

return { getMyPosts, like, unlike, getLikes };
const getHotPosts = async (pageNumber: number, pageSize: number): Promise<UserPostResponseDTO[]> => {
const params: URLSearchParams = new URLSearchParams({
pageNumber: String(pageNumber),
pageSize: String(pageSize),
});

return await useApi<UserPostResponseDTO[]>(`posts/hot-posts?${params.toString()}`, token);
};

return {
getMyPosts,
getLikes,
like,
unlike,
getHotPosts,
};
}
12 changes: 12 additions & 0 deletions types/dtos/nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit a961256

Please sign in to comment.