first commit
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
import { getOptionalAppUserFromRequest } from "@/server/app/app-auth";
|
||||
import { getPublishedCollectionBySlug } from "@/server/site/photo-collections";
|
||||
import { NextResponse, type NextRequest } from "next/server";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ slug: string }> },
|
||||
) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const { slug } = await params;
|
||||
const page = parseInt(searchParams.get("page") || "1", 10);
|
||||
const pageSize = parseInt(
|
||||
searchParams.get("pageSize") ||
|
||||
searchParams.get("page_size") ||
|
||||
"24",
|
||||
10,
|
||||
);
|
||||
|
||||
if (page < 1 || pageSize < 1 || pageSize > 100) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Invalid pagination parameters." },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const user = await getOptionalAppUserFromRequest(request);
|
||||
const result = await getPublishedCollectionBySlug({
|
||||
slug,
|
||||
page,
|
||||
pageSize,
|
||||
userId: user?.id ?? null,
|
||||
});
|
||||
|
||||
if (!result) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Collection was not found." },
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
data: {
|
||||
collection: result.collection,
|
||||
photos: result.photos,
|
||||
},
|
||||
pagination: {
|
||||
page,
|
||||
pageSize,
|
||||
total: result.total,
|
||||
hasMore: result.total > page * pageSize,
|
||||
nextPage: result.total > page * pageSize ? page + 1 : null,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : "Unknown error.";
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: message },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { getOptionalAppUserFromRequest } from "@/server/app/app-auth";
|
||||
import { listPublishedCollections } from "@/server/site/photo-collections";
|
||||
import { NextResponse, type NextRequest } from "next/server";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const page = parseInt(searchParams.get("page") || "1", 10);
|
||||
const pageSize = parseInt(
|
||||
searchParams.get("pageSize") ||
|
||||
searchParams.get("page_size") ||
|
||||
"20",
|
||||
10,
|
||||
);
|
||||
|
||||
if (page < 1 || pageSize < 1 || pageSize > 50) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Invalid pagination parameters." },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const user = await getOptionalAppUserFromRequest(request);
|
||||
const result = await listPublishedCollections({
|
||||
page,
|
||||
pageSize,
|
||||
userId: user?.id ?? null,
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
data: result.data,
|
||||
pagination: {
|
||||
page,
|
||||
pageSize,
|
||||
total: result.total,
|
||||
hasMore: result.total > page * pageSize,
|
||||
nextPage: result.total > page * pageSize ? page + 1 : null,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : "Unknown error.";
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: message },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
import {
|
||||
getAppUserFromRequest,
|
||||
getOptionalAppUserFromRequest,
|
||||
} from "@/server/app/app-auth";
|
||||
import {
|
||||
addUserFavorite,
|
||||
removeUserFavorite,
|
||||
} from "@/server/app/user-favorites";
|
||||
import { getPickedGalleryPhotoById } from "@/server/site/gallery-photos";
|
||||
import { NextResponse, type NextRequest } from "next/server";
|
||||
import { z } from "zod";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
const paramsSchema = z.object({
|
||||
id: z.uuid(),
|
||||
});
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
try {
|
||||
const parsed = paramsSchema.safeParse(await params);
|
||||
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Invalid photo id." },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const user = await getOptionalAppUserFromRequest(request);
|
||||
const data = await getPickedGalleryPhotoById({
|
||||
id: parsed.data.id,
|
||||
userId: user?.id ?? null,
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Photo 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 POST(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
try {
|
||||
const user = await getAppUserFromRequest(request);
|
||||
|
||||
if (!user) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Unauthorized." },
|
||||
{ status: 401 },
|
||||
);
|
||||
}
|
||||
|
||||
const parsed = paramsSchema.safeParse(await params);
|
||||
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Invalid photo id." },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const data = await addUserFavorite({
|
||||
userId: user.id,
|
||||
photoId: parsed.data.id,
|
||||
});
|
||||
|
||||
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: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
try {
|
||||
const user = await getAppUserFromRequest(request);
|
||||
|
||||
if (!user) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Unauthorized." },
|
||||
{ status: 401 },
|
||||
);
|
||||
}
|
||||
|
||||
const parsed = paramsSchema.safeParse(await params);
|
||||
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Invalid photo id." },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
await removeUserFavorite({
|
||||
userId: user.id,
|
||||
photoId: parsed.data.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,54 @@
|
||||
import { getOptionalAppUserFromRequest } from "@/server/app/app-auth";
|
||||
import { listPickedGalleryPhotos } from "@/server/site/gallery-photos";
|
||||
import { NextResponse, type NextRequest } from "next/server";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const page = parseInt(searchParams.get("page") || "1", 10);
|
||||
const pageSize = parseInt(
|
||||
searchParams.get("pageSize") ||
|
||||
searchParams.get("page_size") ||
|
||||
"24",
|
||||
10,
|
||||
);
|
||||
const colorFamily = searchParams.get("colorFamily");
|
||||
|
||||
if (page < 1 || pageSize < 1 || pageSize > 100) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Invalid pagination parameters." },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const user = await getOptionalAppUserFromRequest(request);
|
||||
const result = await listPickedGalleryPhotos({
|
||||
colorFamily,
|
||||
page,
|
||||
pageSize,
|
||||
userId: user?.id ?? null,
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
data: result.data,
|
||||
pagination: {
|
||||
page,
|
||||
pageSize,
|
||||
total: result.total,
|
||||
hasMore: result.total > page * pageSize,
|
||||
nextPage: result.total > page * pageSize ? page + 1 : null,
|
||||
},
|
||||
});
|
||||
} 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