Skip to content

Commit

Permalink
fix issue
Browse files Browse the repository at this point in the history
  • Loading branch information
thebkht committed Oct 30, 2023
1 parent 3db7338 commit cba2191
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
3 changes: 1 addition & 2 deletions app/(Main)/[username]/[url]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
40 changes: 29 additions & 11 deletions app/api/posts/[username]/views/route.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
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 }
);
}
}
18 changes: 13 additions & 5 deletions components/blog/actions.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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,
};
Expand Down

0 comments on commit cba2191

Please sign in to comment.