61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
|
|
import { getMaestroDetail, updateMaestro, updateMaestroSchema } from "@/lib/maestros";
|
|
import { ApiError } from "@/lib/errors";
|
|
import { withApiHandler } from "@/lib/api-handler";
|
|
import { logger } from "@/lib/logger";
|
|
|
|
const CTX = "maestros/[id]";
|
|
|
|
export const GET = withApiHandler<{ id: string }>(
|
|
CTX,
|
|
async (_request, { params }) => {
|
|
const { id } = await params;
|
|
const maestroID = Number(id);
|
|
|
|
if (!Number.isInteger(maestroID) || maestroID <= 0) {
|
|
throw new ApiError("Invalid maestro id", 400);
|
|
}
|
|
|
|
const result = await getMaestroDetail(maestroID);
|
|
|
|
if (!result) {
|
|
throw new ApiError("Not found", 404);
|
|
}
|
|
|
|
return NextResponse.json(result);
|
|
}
|
|
);
|
|
|
|
export const PATCH = withApiHandler<{ id: string }>(
|
|
CTX,
|
|
async (request, { params, t0 }) => {
|
|
const { id } = await params;
|
|
const maestroID = Number(id);
|
|
|
|
if (!Number.isInteger(maestroID) || maestroID <= 0) {
|
|
throw new ApiError("Invalid maestro id", 400);
|
|
}
|
|
|
|
const body = updateMaestroSchema.safeParse(
|
|
await request.json().catch(() => ({}))
|
|
);
|
|
if (!body.success) {
|
|
throw new ApiError("Invalid request body", 400);
|
|
}
|
|
|
|
const result = await updateMaestro(maestroID, body.data);
|
|
|
|
if (!result) {
|
|
throw new ApiError("Not found", 404);
|
|
}
|
|
|
|
logger.info(CTX, result.changed ? "updated" : "no change", {
|
|
id: maestroID,
|
|
duration: Date.now() - t0,
|
|
});
|
|
|
|
return NextResponse.json(result);
|
|
}
|
|
);
|