diff --git a/backend/src/routes/post/controller.ts b/backend/src/routes/post/controller.ts index fd8ef30e..c8ce7285 100644 --- a/backend/src/routes/post/controller.ts +++ b/backend/src/routes/post/controller.ts @@ -83,28 +83,28 @@ export const createPostController = async ( } }; -export const getPostsController = async (req: Request, res: Response) => { - const posts = await prisma.post.findMany({ - select: { - id: true, - title: true, - codeSnippet: true, - description: true, - tags: true, - author: { - select: { - id: true, - username: true, - email: true, - }, - }, - }, - }); +// export const getPostsController = async (req: Request, res: Response) => { +// const posts = await prisma.post.findMany({ +// select: { +// id: true, +// title: true, +// codeSnippet: true, +// description: true, +// tags: true, +// author: { +// select: { +// id: true, +// username: true, +// email: true, +// }, +// }, +// }, +// }); - res.status(200).json({ - posts, - }); -}; +// res.status(200).json({ +// posts, +// }); +// }; export const getPostController = async (req: Request, res: Response) => { try { @@ -145,3 +145,39 @@ export const getPostController = async (req: Request, res: Response) => { }); } }; + +export const getPostsWithPagination = async (req: Request, res: Response) => { + try { + const page = parseInt(req.query.page as string); + const pageSize = parseInt(req.query.pageSize as string); + + const totalPosts = await prisma.post.count(); + const totalPages = Math.ceil(totalPosts / pageSize); + + const posts = await prisma.post.findMany({ + skip: (page - 1) * pageSize, + take: pageSize, + select: { + id: true, + title: true, + codeSnippet: true, + description: true, + tags: true, + author: { + select: { + id: true, + username: true, + email: true, + }, + }, + }, + }); + + res.status(200).json({ + posts, + totalPages, + }); + } catch (error) { + res.status(500).json({ error: 'Failed to fetch posts' }); + } +}; \ No newline at end of file diff --git a/backend/src/routes/post/route.ts b/backend/src/routes/post/route.ts index 42e8747f..ef21d172 100644 --- a/backend/src/routes/post/route.ts +++ b/backend/src/routes/post/route.ts @@ -1,10 +1,10 @@ import { Router } from "express"; import authMiddleware from "../../middleware/auth" -import { createPostController, getPostController, getPostsController } from "./controller"; +import { createPostController, getPostController, getPostsWithPagination } from "./controller"; const postRouter = Router(); -postRouter.get('/', getPostsController) +postRouter.get('/', getPostsWithPagination); postRouter.post('/', authMiddleware, createPostController) diff --git a/frontend/src/pages/Posts.tsx b/frontend/src/pages/Posts.tsx index 84b4d11d..c949a3bc 100644 --- a/frontend/src/pages/Posts.tsx +++ b/frontend/src/pages/Posts.tsx @@ -13,12 +13,16 @@ const Posts = () => { const [filterTags, setFilterTags] = useState([]); const filterRef = useRef(null); const [searchQuery, setSearchQuery] = useState(''); + const [page, setPage] = useState(1); + const [totalPages, setTotalPages] = useState(1); useEffect(() => { const fetchPosts = async () => { try { - const response = await axios.get('/api/v1/posts'); + setLoading(true); + const response = await axios.get(`/api/v1/posts?page=${page}&pageSize=12`); setPosts(response.data.posts); + setTotalPages(response.data.totalPages); setLoading(false); } catch (error) { setError('Failed to fetch posts'); @@ -27,7 +31,7 @@ const Posts = () => { }; fetchPosts(); - }, []); + }, [page]); useEffect(() => { const handleClickOutside = (event: MouseEvent) => { @@ -71,6 +75,22 @@ const Posts = () => { post.author.username.toLowerCase().includes(searchQuery.toLowerCase())) ); + const handlePreviousPage = () => { + if (page > 1) { + setPage(page - 1); + } + }; + + const handleNextPage = () => { + if (page < totalPages) { + setPage(page + 1); + } + }; + + const handlePageClick = (pageNumber: number) => { + setPage(pageNumber); + }; + if (loading) { return ; } @@ -143,6 +163,28 @@ const Posts = () => { ))} +
+ + {Array.from({ length: totalPages }, (_, i) => ( + + ))} + +
); };