-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
43 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters