From 3e6decb08b4ec83797fa2f58c4a220025f5dba2f Mon Sep 17 00:00:00 2001 From: Shobhit-Nagpal Date: Tue, 21 Jan 2025 17:44:05 +0530 Subject: [PATCH] refactor: collection api routes --- src/app/api/collection/all/route.ts | 46 ------------------- .../{collection => collections}/[id]/route.ts | 0 .../api/{collection => collections}/route.ts | 43 +++++++++++++++++ src/hooks/collections/useDeleteCollection.tsx | 2 +- src/hooks/collections/useFetchCollection.tsx | 2 +- .../useFetchCollectionWithMemes.tsx | 2 +- src/hooks/collections/useFetchCollections.tsx | 2 +- src/hooks/collections/usePostCollection.tsx | 2 +- src/hooks/collections/useUpdateCollection.tsx | 2 +- 9 files changed, 49 insertions(+), 52 deletions(-) delete mode 100644 src/app/api/collection/all/route.ts rename src/app/api/{collection => collections}/[id]/route.ts (100%) rename src/app/api/{collection => collections}/route.ts (72%) diff --git a/src/app/api/collection/all/route.ts b/src/app/api/collection/all/route.ts deleted file mode 100644 index 99cb0b1..0000000 --- a/src/app/api/collection/all/route.ts +++ /dev/null @@ -1,46 +0,0 @@ -import prisma from "@/lib/prisma"; -import { currentUser } from "@clerk/nextjs/server"; -import { NextResponse } from "next/server"; - -export const dynamic = "force-dynamic"; - -export async function GET() { - try { - const user = await currentUser(); - - if (!user) { - return NextResponse.json( - { - message: "User not authenticated", - }, - { status: 401 }, - ); - } - - const collections = await prisma.collection.findMany({ - where: { - madeById: user.id, - }, - }); - - return NextResponse.json( - { - message: "Fetched collections successfully", - data: collections, - }, - { - status: 200, - }, - ); - } catch (err: any) { - console.error(err); - return NextResponse.json( - { - message: "Internal Server error", - }, - { - status: 500, - }, - ); - } -} diff --git a/src/app/api/collection/[id]/route.ts b/src/app/api/collections/[id]/route.ts similarity index 100% rename from src/app/api/collection/[id]/route.ts rename to src/app/api/collections/[id]/route.ts diff --git a/src/app/api/collection/route.ts b/src/app/api/collections/route.ts similarity index 72% rename from src/app/api/collection/route.ts rename to src/app/api/collections/route.ts index 2d79766..4f51141 100644 --- a/src/app/api/collection/route.ts +++ b/src/app/api/collections/route.ts @@ -2,6 +2,49 @@ import prisma from "@/lib/prisma"; import { currentUser } from "@clerk/nextjs/server"; import { NextRequest, NextResponse } from "next/server"; +export const dynamic = "force-dynamic"; + +export async function GET() { + try { + const user = await currentUser(); + + if (!user) { + return NextResponse.json( + { + message: "User not authenticated", + }, + { status: 401 }, + ); + } + + const collections = await prisma.collection.findMany({ + where: { + madeById: user.id, + }, + }); + + return NextResponse.json( + { + message: "Fetched collections successfully", + data: collections, + }, + { + status: 200, + }, + ); + } catch (err: any) { + console.error(err); + return NextResponse.json( + { + message: "Internal Server error", + }, + { + status: 500, + }, + ); + } +} + export async function POST(req: NextRequest) { try { const user = await currentUser(); diff --git a/src/hooks/collections/useDeleteCollection.tsx b/src/hooks/collections/useDeleteCollection.tsx index e7aa367..29bd1bd 100644 --- a/src/hooks/collections/useDeleteCollection.tsx +++ b/src/hooks/collections/useDeleteCollection.tsx @@ -12,7 +12,7 @@ export function useDeleteCollection() { async (id: string) => { try { setLoading(true); - const res = await fetch("/api/collection", { + const res = await fetch("/api/collections", { method: "DELETE", body: JSON.stringify({ collectionId: id, diff --git a/src/hooks/collections/useFetchCollection.tsx b/src/hooks/collections/useFetchCollection.tsx index 6e3a5de..4f89cc9 100644 --- a/src/hooks/collections/useFetchCollection.tsx +++ b/src/hooks/collections/useFetchCollection.tsx @@ -15,7 +15,7 @@ export function useFetchCollection(id: string) { async (id: string) => { try { setLoading(true); - const req = await fetch(`/api/collection/${id}`); + const req = await fetch(`/api/collections/${id}`); const res = await req.json(); if (req.status === 200) { setCollectionWithMemes(res.data); diff --git a/src/hooks/collections/useFetchCollectionWithMemes.tsx b/src/hooks/collections/useFetchCollectionWithMemes.tsx index bec3860..2b5d0ec 100644 --- a/src/hooks/collections/useFetchCollectionWithMemes.tsx +++ b/src/hooks/collections/useFetchCollectionWithMemes.tsx @@ -15,7 +15,7 @@ export function useFetchCollectionWithMemes() { async (collectionId: string) => { try { setLoading(true); - const req = await fetch(`/api/collection/${collectionId}`); + const req = await fetch(`/api/collections/${collectionId}`); const res = await req.json(); if (req.status === 200) { setCollectionWithMemes(res.data); diff --git a/src/hooks/collections/useFetchCollections.tsx b/src/hooks/collections/useFetchCollections.tsx index 3d5761d..8c3362b 100644 --- a/src/hooks/collections/useFetchCollections.tsx +++ b/src/hooks/collections/useFetchCollections.tsx @@ -12,7 +12,7 @@ export function useFetchCollections() { const fetchCollections = useCallback(async () => { setLoading(true); try { - const req = await fetch("/api/collection/all"); + const req = await fetch("/api/collections"); const res = await req.json(); if (req.status === 200) { setCollections(res.data); diff --git a/src/hooks/collections/usePostCollection.tsx b/src/hooks/collections/usePostCollection.tsx index f20777b..18e4fa8 100644 --- a/src/hooks/collections/usePostCollection.tsx +++ b/src/hooks/collections/usePostCollection.tsx @@ -10,7 +10,7 @@ export function usePostCollection() { const postCollection = useCallback(async (name: string) => { setLoading(true); try { - const req = await fetch("/api/collection/", { + const req = await fetch("/api/collections", { method: "POST", body: JSON.stringify({ collectionName: name, diff --git a/src/hooks/collections/useUpdateCollection.tsx b/src/hooks/collections/useUpdateCollection.tsx index e3da1fc..7fe678a 100644 --- a/src/hooks/collections/useUpdateCollection.tsx +++ b/src/hooks/collections/useUpdateCollection.tsx @@ -11,7 +11,7 @@ export function useUpdateCollection() { async (collectionId: string, payload: { collectionName: string }) => { setLoading(true); try { - const req = await fetch(`/api/collection/${collectionId}`, { + const req = await fetch(`/api/collections/${collectionId}`, { method: "PATCH", body: JSON.stringify(payload), });