first commit
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
import { auth } from "@/lib/auth";
|
||||
import {
|
||||
deletePhotoCollection,
|
||||
getAdminPhotoCollection,
|
||||
updatePhotoCollection,
|
||||
} from "@/server/admin/photo-collections";
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
const collectionInputSchema = z.object({
|
||||
title: z.string().min(1),
|
||||
slug: z.string().optional().nullable(),
|
||||
description: z.string().optional().nullable(),
|
||||
coverPhotoId: z.string().uuid().optional().nullable(),
|
||||
isPublished: z.boolean().default(false),
|
||||
photoIds: z.array(z.string().uuid()).default([]),
|
||||
});
|
||||
|
||||
async function requireAdminSession() {
|
||||
const session = await auth();
|
||||
|
||||
if (!session) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
_request: Request,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
try {
|
||||
const session = await requireAdminSession();
|
||||
|
||||
if (!session) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Unauthorized." },
|
||||
{ status: 401 },
|
||||
);
|
||||
}
|
||||
|
||||
const { id } = await params;
|
||||
const data = await getAdminPhotoCollection(id);
|
||||
|
||||
if (!data) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Collection was not found." },
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
data,
|
||||
});
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : "Unknown error.";
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: message },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PATCH(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
try {
|
||||
const session = await requireAdminSession();
|
||||
|
||||
if (!session) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Unauthorized." },
|
||||
{ status: 401 },
|
||||
);
|
||||
}
|
||||
|
||||
const parsed = collectionInputSchema.safeParse(await request.json());
|
||||
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Invalid request body." },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const { id } = await params;
|
||||
const data = await updatePhotoCollection(id, parsed.data);
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
data,
|
||||
});
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : "Unknown error.";
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: message },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
_request: Request,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
try {
|
||||
const session = await requireAdminSession();
|
||||
|
||||
if (!session) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Unauthorized." },
|
||||
{ status: 401 },
|
||||
);
|
||||
}
|
||||
|
||||
const { id } = await params;
|
||||
await deletePhotoCollection(id);
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
});
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : "Unknown error.";
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: message },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import { auth } from "@/lib/auth";
|
||||
import {
|
||||
createPhotoCollection,
|
||||
listAdminPhotoCollections,
|
||||
} from "@/server/admin/photo-collections";
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
const collectionInputSchema = z.object({
|
||||
title: z.string().min(1),
|
||||
slug: z.string().optional().nullable(),
|
||||
description: z.string().optional().nullable(),
|
||||
coverPhotoId: z.string().uuid().optional().nullable(),
|
||||
isPublished: z.boolean().default(false),
|
||||
photoIds: z.array(z.string().uuid()).default([]),
|
||||
});
|
||||
|
||||
async function requireAdminSession() {
|
||||
const session = await auth();
|
||||
|
||||
if (!session) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const session = await requireAdminSession();
|
||||
|
||||
if (!session) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Unauthorized." },
|
||||
{ status: 401 },
|
||||
);
|
||||
}
|
||||
|
||||
const data = await listAdminPhotoCollections();
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
data,
|
||||
});
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : "Unknown error.";
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: message },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const session = await requireAdminSession();
|
||||
|
||||
if (!session) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Unauthorized." },
|
||||
{ status: 401 },
|
||||
);
|
||||
}
|
||||
|
||||
const parsed = collectionInputSchema.safeParse(await request.json());
|
||||
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Invalid request body." },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const data = await createPhotoCollection(parsed.data);
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
data,
|
||||
});
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : "Unknown error.";
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: message },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user