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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 1x 1x 11x 11x 11x 11x 11x 11x 12x 1x 1x 10x 12x 1x 1x 9x 9x 9x 7x 12x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { NextRequest, NextResponse } from 'next/server';
import {
getComparisonProducts,
getComparisonAttributes,
canCompareProducts,
getRelatedComparableProducts } from "@/lib/ecommerce";
import {
withErrorHandling,
successResponse,
ApiError,
ApiSuccessResponse,
ApiErrorResponse } from "@/lib/api";
/**
* GET /api/comparison
* Get comparison data for products
*
* Query Parameters:
* - productIds: Comma-separated product IDs to compare
*/
async function handleGet(
request: NextRequest
): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
const searchParams = request.nextUrl.searchParams;
const productIdsStr = searchParams.get("productIds");
if (!productIdsStr) {
throw ApiError.badRequest("productIds query parameter is required");
}
const productIds = productIdsStr
.split(",")
.map((id) => parseInt(id))
.filter((id) => !isNaN(id));
if (productIds.length === 0) {
throw ApiError.badRequest("No valid product IDs provided");
}
if (productIds.length > 5) {
throw ApiError.badRequest("Maximum 5 products can be compared");
}
// Get comparison products
const products = await getComparisonProducts(productIds);
if (products.length === 0) {
throw ApiError.notFound("Products");
}
// Get attributes
const comparisonData = await getComparisonAttributes(productIds);
// Check if products can be compared (same category)
const sameCategory = await canCompareProducts(productIds);
return successResponse({
...comparisonData,
sameCategory });
}
export const GET = withErrorHandling(handleGet);
/**
* GET /api/comparison/related
* Get related products that can be compared
* Note: This is a named handler for related route, not exported as route handler
*/
export async function getRelatedHandler(
request: NextRequest
): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
const searchParams = request.nextUrl.searchParams;
const productId = searchParams.get("productId");
if (!productId) {
throw ApiError.badRequest("productId query parameter is required");
}
const id = parseInt(productId);
if (isNaN(id)) {
throw ApiError.badRequest("Invalid product ID");
}
const relatedProducts = await getRelatedComparableProducts(id);
return successResponse(relatedProducts);
}
|