From cba2191fdce9a420b19c60eed3ffa1db9f3d3f89 Mon Sep 17 00:00:00 2001 From: Bakhtiyor Ganijon Date: Tue, 31 Oct 2023 07:03:16 +0900 Subject: [PATCH] fix issue --- app/(Main)/[username]/[url]/page.tsx | 3 +- app/api/posts/[username]/views/route.ts | 40 ++++++++++++++++++------- components/blog/actions.ts | 18 +++++++---- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/app/(Main)/[username]/[url]/page.tsx b/app/(Main)/[username]/[url]/page.tsx index 25784480..31ea1fc8 100644 --- a/app/(Main)/[username]/[url]/page.tsx +++ b/app/(Main)/[username]/[url]/page.tsx @@ -85,9 +85,8 @@ export default async function PostView({ params }: { params: { username: string, - await fetch(`${process.env.DOMAIN}/api/posts/${author?.username}/views/`, { + await fetch(`${process.env.DOMAIN}/api/posts/${author?.username}/views/?url=${post.url}`, { method: "POST", - body: JSON.stringify({ post: post, author: author }), }); return ( diff --git a/app/api/posts/[username]/views/route.ts b/app/api/posts/[username]/views/route.ts index c04ce782..c86c17a2 100644 --- a/app/api/posts/[username]/views/route.ts +++ b/app/api/posts/[username]/views/route.ts @@ -1,15 +1,33 @@ -// pages/api/some-route.ts -import { NextRequest, NextResponse } from 'next/server'; -import { incrementPostViews } from '@/components/blog/actions'; +import { incrementPostViews } from "@/components/blog/actions"; +import postgres from "@/lib/postgres"; +import { NextRequest, NextResponse } from "next/server"; -export default async function POST(req: NextRequest, res: NextResponse) { - const { post } = await req.json(); - const { author } = await req.json(); +export async function POST( + req: NextRequest, + { params }: { params: { username: string } } +) { + try { + // Get the 'slug' route parameter from the request object + const username = params.username; + const postUrl = req.nextUrl.searchParams.get("url"); - const cookie = await incrementPostViews({ post, author }); + if (username === undefined || username === null) { + return NextResponse.json({ error: "User not found" }, { status: 404 }); + } + if (postUrl === undefined || postUrl === null) { + return NextResponse.json({ error: "Post not found" }, { status: 404 }); + } - // Set the cookie - res.cookies.set(cookie) + const cookie = await incrementPostViews({ author: username, post: postUrl }); - return NextResponse.json({ message: 'Post view incremented' }, { status: 200 }); -} \ No newline at end of file + req.cookies.set(cookie) + + return NextResponse.json({ message: "View added" }, { status: 200 }); + } catch (error) { + console.log(error); + return NextResponse.json( + { error: "Something went wrong" }, + { status: 500 } + ); + } +} diff --git a/components/blog/actions.ts b/components/blog/actions.ts index ccff1c95..bcc083a7 100644 --- a/components/blog/actions.ts +++ b/components/blog/actions.ts @@ -1,14 +1,22 @@ import postgres from "@/lib/postgres"; // actions.ts -export async function incrementPostViews({ post, author} : { post: any, author: any }) { - const cookieName = `post_views_${author?.username}_${post?.url}`; +export async function incrementPostViews({ post, author} : { post: string, author: string }) { + const cookieName = `post_views_${author}_${post}`; + const authorId = await postgres.user.findUnique({ + where: { + username: author, + }, + select: { + id: true, + }, + }); // Make an API request to increment the view count await postgres.post.update({ where: { - url: post?.url, - authorId: author?.id, + url: post, + authorId: authorId?.id, }, data: { views: { @@ -22,7 +30,7 @@ export async function incrementPostViews({ post, author} : { post: any, author: return { name: cookieName, value: "true", - path: `/${author?.username}/${post?.url}`, + path: `/${author}/${post}`, expires: expirationDate, secure: true, };