Phase 10. 마에스트로 정보 수정 및 학생 목록 페이지네이션 적용
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
import { getMaestroDetail } from "@/lib/maestros";
|
||||
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 }>(
|
||||
"maestros/[id]",
|
||||
CTX,
|
||||
async (_request, { params }) => {
|
||||
const { id } = await params;
|
||||
const maestroID = Number(id);
|
||||
@@ -23,3 +26,35 @@ export const GET = withApiHandler<{ id: string }>(
|
||||
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);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
import { getMaestroStudents } from "@/lib/maestros";
|
||||
import { ApiError } from "@/lib/errors";
|
||||
import { withApiHandler } from "@/lib/api-handler";
|
||||
|
||||
const CTX = "maestros/[id]/students";
|
||||
|
||||
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 url = new URL(request.url);
|
||||
const result = await getMaestroStudents(maestroID, url.searchParams);
|
||||
|
||||
if (!result) {
|
||||
throw new ApiError("Not found", 404);
|
||||
}
|
||||
|
||||
return NextResponse.json(result);
|
||||
}
|
||||
);
|
||||
Reference in New Issue
Block a user