Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 2x 5x 5x 5x 5x 5x 5x 4x 7x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 7x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x | import { NextRequest, NextResponse } from 'next/server';
import {
withAuth,
withErrorHandling,
successResponse,
createdResponse,
validationErrorResponse,
ApiError,
ApiSuccessResponse,
ApiErrorResponse } from "@/lib/api";
import { RouteContext } from "@/lib/api/middleware";
import { prisma } from "@/lib/prisma";
import { addToWishlistSchema, validateInput } from "@/lib/validation-schemas";
import { Session } from "next-auth";
// Type for wishlist item with product
interface WishlistItemWithProduct {
id: number;
createdAt: Date;
product: {
id: number;
title: string;
price: number;
discountedPrice: number | null;
images: Array<{ thumbnailUrl: string | null; url: string }>;
reviews: Array<{ rating: number }>;
};
}
interface TransformedWishlistItem {
id: number;
title: string;
price: number;
discountedPrice: number | null;
reviews: number;
averageRating: number;
imgs: {
thumbnails: string[];
previews: string[];
};
addedAt: Date;
}
// GET /api/wishlist - Get user's wishlist items
async function handleGet(
_request: NextRequest,
_context: RouteContext | undefined,
session: Session
): Promise<NextResponse<ApiSuccessResponse<TransformedWishlistItem[]>>> {
const userId = session.user.id;
const wishlistItems = await prisma.wishlist.findMany({
where: { userId },
include: {
product: {
include: {
images: {
orderBy: { order: "asc" }},
reviews: {
select: {
rating: true}}}}},
orderBy: {
createdAt: "desc"}});
// Transform to match frontend Product type
const transformedWishlist = wishlistItems.map(
(item: WishlistItemWithProduct) => {
const totalRating = item.product.reviews.reduce(
(sum: number, review: { rating: number }) => sum + review.rating,
0
);
const averageRating =
item.product.reviews.length > 0
? totalRating / item.product.reviews.length
: 0;
return {
id: item.product.id,
title: item.product.title,
price: item.product.price,
discountedPrice: item.product.discountedPrice,
reviews: item.product.reviews.length,
averageRating,
imgs: {
thumbnails: item.product.images.map(
(img) => img.thumbnailUrl || img.url
),
previews: item.product.images.map((img) => img.url)},
addedAt: item.createdAt};
}
);
return successResponse(transformedWishlist);
}
// DELETE /api/wishlist - Clear all wishlist items
async function handleDelete(
_request: NextRequest,
_context: RouteContext | undefined,
session: Session
): Promise<NextResponse<ApiSuccessResponse<{ success: boolean }>>> {
const userId = session.user.id;
await prisma.wishlist.deleteMany({
where: { userId }});
return successResponse({ success: true });
}
// POST /api/wishlist - Add item to wishlist
async function handlePost(
request: NextRequest,
_context: RouteContext | undefined,
session: Session
): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
const body = await request.json();
const userId = session.user.id;
// Validate input
const validation = validateInput(addToWishlistSchema, body);
if (!validation.success) {
return validationErrorResponse("Validation failed", validation.errors);
}
const { productId } = validation.data!;
// Check if product exists
const product = await prisma.product.findUnique({
where: { id: productId }});
if (!product) {
throw ApiError.notFound("Product");
}
// Check if already in wishlist
const existingItem = await prisma.wishlist.findUnique({
where: {
userId_productId: {
userId,
productId}}});
if (existingItem) {
throw ApiError.conflict("Product already in wishlist");
}
// Add to wishlist
const wishlistItem = await prisma.wishlist.create({
data: {
userId,
productId},
include: {
product: {
include: {
images: true}}}});
return createdResponse(wishlistItem);
}
export const GET = withErrorHandling(withAuth(handleGet));
export const DELETE = withErrorHandling(withAuth(handleDelete));
export const POST = withErrorHandling(withAuth(handlePost));
|