diff --git a/app/api/views/[slug]/route.ts b/app/api/views/[slug]/route.ts deleted file mode 100644 index aa296ea..0000000 --- a/app/api/views/[slug]/route.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { NextResponse } from "next/server"; -import { SupabaseAdmin } from "@/lib/supabase"; - -export async function POST( - req: Request, - { params }: { params: { slug: string } } -) { - try { - await SupabaseAdmin.rpc("increment_post_view", { post_slug: params.slug }); - - return NextResponse.json({ - message: `Successfully incremented post: ${params.slug}`, - }); - } catch (error) { - return NextResponse.json({ error }); - } -} - -export async function GET( - req: Request, - { params }: { params: { slug: string } } -) { - const { data } = await SupabaseAdmin.from("posts") - .select("view_count") - .filter("slug", "eq", params.slug); - - if (data) - return NextResponse.json({ - total: data[0]?.view_count || null, - }); - return NextResponse.json({ - message: "Unsupported Request", - }); -} diff --git a/lib/utils.ts b/lib/utils.ts index ea5b80e..82aef33 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -1,5 +1,6 @@ import { FooterSocials } from "@/config/navigation"; import { Repo, SocialPlatform } from "@/types"; +import { SupabaseAdmin } from "./supabase"; export const fetchGithubRepos = async () => { const response = await fetch("https://api.github.com/users/choubari/repos"); @@ -98,15 +99,20 @@ export const validateCaptcha = (response_key) => { }; export async function getPostViews(slug: string) { - const response = await fetch( - `${process.env.NEXT_PUBLIC_APP_URL}/api/views/${slug}` - ); - const data = await response.json(); - return data; + const { data } = await SupabaseAdmin.from("posts") + .select("view_count") + .filter("slug", "eq", slug); + return { + total: data[0]?.view_count || null, + }; } export async function incrementPostViews(slug: string) { - await fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/views/${slug}`, { - method: "POST", - next: { revalidate: 10 }, - }); + try { + await SupabaseAdmin.rpc("increment_post_view", { post_slug: slug }); + return { + message: `Successfully incremented post: ${slug}`, + }; + } catch (error) { + return { error }; + } }