Skip to content

Commit

Permalink
Debugging Upstash #5
Browse files Browse the repository at this point in the history
  • Loading branch information
david emioma committed Apr 2, 2024
1 parent 80300bb commit c29b053
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
8 changes: 6 additions & 2 deletions app/api/cart/[cartItemId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ export async function PATCH(
},
});

await redis.set(`${user.id}-cart`, newCart);
if (process.env.VERCEL_ENV !== "preview") {
await redis.set(`${user.id}-cart`, newCart);
}

return NextResponse.json({ message: "Cart item updated!" });
} catch (err) {
Expand Down Expand Up @@ -214,7 +216,9 @@ export async function DELETE(
},
});

await redis.set(`${user.id}-cart`, newCart);
if (process.env.VERCEL_ENV !== "preview") {
await redis.set(`${user.id}-cart`, newCart);
}

return NextResponse.json({ message: "Cart item deleted!" });
} catch (err) {
Expand Down
12 changes: 8 additions & 4 deletions app/api/cart/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,12 @@ export async function GET(request: Request) {
return NextResponse.json({ cart: null });
}

const cachedCart = await redis.get(`${user.id}-cart`);
if (process.env.VERCEL_ENV !== "preview") {
const cachedCart = await redis.get(`${user.id}-cart`);

if (cachedCart) {
return NextResponse.json(cachedCart);
if (cachedCart) {
return NextResponse.json(cachedCart);
}
}

const cart = await prismadb.cart.findUnique({
Expand Down Expand Up @@ -225,7 +227,9 @@ export async function GET(request: Request) {
},
});

await redis.set(`${user.id}-cart`, cart);
if (process.env.VERCEL_ENV !== "preview") {
await redis.set(`${user.id}-cart`, cart);
}

return NextResponse.json(cart);
} catch (err) {
Expand Down

0 comments on commit c29b053

Please sign in to comment.