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 | 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 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 2x 2x 6x 6x 6x 6x 6x 6x 6x 8x 1x 1x 5x 5x 5x 8x 4x 4x 4x 4x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 4x 4x 4x 1x 1x 1x | export const dynamic = "force-dynamic";
/**
* Admin Hero Carousel API
* CRUD operations for hero carousel items
*/
import { NextRequest, NextResponse } from 'next/server';
import { } from "next-auth";
import { prisma } from "@/lib/prisma";
import { z } from "zod";
import {
withAdmin,
withErrorHandling,
successResponse,
createdResponse,
ApiError,
ApiSuccessResponse,
ApiErrorResponse } from "@/lib/api";
import { } from "@/lib/api/middleware";
// Validation schema for creating carousel item
const createCarouselItemSchema = z.object({
productId: z.number().int().positive(),
headline: z.string().max(200).optional().nullable(),
subheadline: z.string().optional().nullable(),
badgeText: z.string().max(50).optional().nullable(),
badgeSubtext: z.string().max(50).optional().nullable(),
ctaText: z.string().max(50).default("Shop Now"),
order: z.number().int().min(0).optional(),
isActive: z.boolean().default(true),
startDate: z.string().datetime().optional().nullable(),
endDate: z.string().datetime().optional().nullable() });
/**
* GET /api/admin/hero/carousel
* List all carousel items
*/
async function handleGet(): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
const items = await prisma.heroCarouselItem.findMany({
orderBy: { order: "asc" },
include: {
product: {
select: {
id: true,
title: true,
price: true,
discountedPrice: true,
images: {
select: {
id: true,
url: true,
thumbnailUrl: true },
orderBy: { order: "asc" },
take: 1 } } } } });
return successResponse({ items });
}
/**
* POST /api/admin/hero/carousel
* Create new carousel item
*/
async function handlePost(request: NextRequest): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
const body = await request.json();
// Validate input
const validationResult = createCarouselItemSchema.safeParse(body);
if (!validationResult.success) {
throw ApiError.validation("Validation failed", validationResult.error.issues);
}
const validatedData = validationResult.data;
// Check if product exists
const product = await prisma.product.findUnique({
where: { id: validatedData.productId } });
if (!product) {
throw ApiError.notFound("Product");
}
// Get max order if not provided
let order = validatedData.order;
if (order === undefined) {
const maxOrder = await prisma.heroCarouselItem.aggregate({
_max: { order: true } });
order = (maxOrder._max.order ?? -1) + 1;
}
const item = await prisma.heroCarouselItem.create({
data: {
productId: validatedData.productId,
headline: validatedData.headline,
subheadline: validatedData.subheadline,
badgeText: validatedData.badgeText,
badgeSubtext: validatedData.badgeSubtext,
ctaText: validatedData.ctaText,
order,
isActive: validatedData.isActive,
startDate: validatedData.startDate ? new Date(validatedData.startDate) : null,
endDate: validatedData.endDate ? new Date(validatedData.endDate) : null },
include: {
product: {
select: {
id: true,
title: true,
price: true,
discountedPrice: true,
images: {
select: {
id: true,
url: true,
thumbnailUrl: true },
orderBy: { order: "asc" },
take: 1 } } } } });
return createdResponse({ item });
}
export const GET = withErrorHandling(withAdmin(handleGet));
export const POST = withErrorHandling(withAdmin(handlePost));
|