Skip to content

Commit

Permalink
Debugging Upstash #8
Browse files Browse the repository at this point in the history
  • Loading branch information
david emioma committed Apr 3, 2024
1 parent 2a3c00c commit 41aa93c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 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 (redis && user.id) {
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 (redis && user.id) {
await redis.set(`${user.id}-cart`, newCart);
}

return NextResponse.json({ message: "Cart item deleted!" });
} catch (err) {
Expand Down
16 changes: 11 additions & 5 deletions app/api/cart/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ export async function POST(request: Request) {
},
});

await redis.set(`${user.id}-cart`, newCart);
if (redis && user.id) {
await redis.set(`${user.id}-cart`, newCart);
}

return NextResponse.json({ message: "Item added to cart!" });
} catch (err) {
Expand All @@ -193,10 +195,12 @@ export async function GET(request: Request) {
return NextResponse.json({ cart: null });
}

const cachedCart = await redis.get(`${user.id}-cart`);
if (redis && user.id) {
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 +229,9 @@ export async function GET(request: Request) {
},
});

await redis.set(`${user.id}-cart`, cart);
if (redis && user.id) {
await redis.set(`${user.id}-cart`, cart);
}

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

0 comments on commit 41aa93c

Please sign in to comment.