From 7240cb07929fc452e2d2d2a0d598229413b41f21 Mon Sep 17 00:00:00 2001 From: Aaron William Po Date: Mon, 13 Feb 2023 13:08:05 -0500 Subject: [PATCH] Fix beer likes and db client --- hooks/useUser.ts | 4 ---- pages/account/index.tsx | 16 ++++++++++++++++ pages/api/beers/[id]/like/index.ts | 8 ++------ pages/logout/index.tsx | 2 +- prisma/DBClient.ts | 14 ++++++++++---- services/BeerPostLike/findBeerPostLikeById.ts | 9 +++++++-- 6 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 pages/account/index.tsx diff --git a/hooks/useUser.ts b/hooks/useUser.ts index e9736e2..4c6b825 100644 --- a/hooks/useUser.ts +++ b/hooks/useUser.ts @@ -9,10 +9,6 @@ const useUser = () => { error, isLoading, } = useSWR('/api/users/current', async (url) => { - if (!document.cookie) { - throw new Error('Not logged in.'); - } - const response = await fetch(url); if (!response.ok) { diff --git a/pages/account/index.tsx b/pages/account/index.tsx new file mode 100644 index 0000000..2a6db58 --- /dev/null +++ b/pages/account/index.tsx @@ -0,0 +1,16 @@ +import Layout from '@/components/ui/Layout'; +import { NextPage } from 'next'; + +interface AccountPageProps {} + +const AccountPage: NextPage = () => { + return ( + +
+

Account Page

+
+
+ ); +}; + +export default AccountPage; diff --git a/pages/api/beers/[id]/like/index.ts b/pages/api/beers/[id]/like/index.ts index fcd85e1..6fe71ba 100644 --- a/pages/api/beers/[id]/like/index.ts +++ b/pages/api/beers/[id]/like/index.ts @@ -24,7 +24,7 @@ const sendLikeRequest = async ( throw new ServerError('Could not find a beer post with that id', 404); } - const alreadyLiked = await findBeerPostLikeById(id); + const alreadyLiked = await findBeerPostLikeById(beer.id, user.id); const jsonResponse = { success: true as const, @@ -50,11 +50,7 @@ const router = createRouter< router.post( getCurrentUser, - validateRequest({ - querySchema: z.object({ - id: z.string().uuid(), - }), - }), + validateRequest({ querySchema: z.object({ id: z.string().uuid() }) }), sendLikeRequest, ); diff --git a/pages/logout/index.tsx b/pages/logout/index.tsx index 0cf2d44..f30bee1 100644 --- a/pages/logout/index.tsx +++ b/pages/logout/index.tsx @@ -10,7 +10,7 @@ const LogoutPage: NextPage = () => { router.reload(); router.push('/'); }, [router]); - return
; + return null; }; export default LogoutPage; diff --git a/prisma/DBClient.ts b/prisma/DBClient.ts index 8542d44..4192a37 100644 --- a/prisma/DBClient.ts +++ b/prisma/DBClient.ts @@ -1,11 +1,17 @@ import { PrismaClient } from '@prisma/client'; +const globalForPrisma = global as unknown as { prisma: PrismaClient }; + const DBClient = { - instance: new PrismaClient(), + instance: + globalForPrisma.prisma || + new PrismaClient({ + log: ['info', 'warn'], + }), }; -export type IDBClient = typeof DBClient; - -Object.freeze(DBClient); +if (process.env.NODE_ENV !== 'production') { + globalForPrisma.prisma = DBClient.instance; +} export default DBClient; diff --git a/services/BeerPostLike/findBeerPostLikeById.ts b/services/BeerPostLike/findBeerPostLikeById.ts index 92bcd45..8cbfe27 100644 --- a/services/BeerPostLike/findBeerPostLikeById.ts +++ b/services/BeerPostLike/findBeerPostLikeById.ts @@ -1,6 +1,11 @@ import DBClient from '@/prisma/DBClient'; -const findBeerPostLikeById = async (id: string) => - DBClient.instance.beerPostLike.findUnique({ where: { id } }); +const findBeerPostLikeById = async (beerPostId: string, userId: string) => + DBClient.instance.beerPostLike.findFirst({ + where: { + beerPostId, + userId, + }, + }); export default findBeerPostLikeById;