27 lines
947 B
TypeScript
27 lines
947 B
TypeScript
import { db } from "../../../db/index";
|
|
import { courses } from "../../../db/schema";
|
|
import { eq } from "drizzle-orm";
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const id = getRouterParam(event, "id")!;
|
|
const body = await readBody(event);
|
|
|
|
const course = await db.query.courses.findFirst({ where: eq(courses.id, id) });
|
|
if (!course) throw createError({ statusCode: 404, message: "Course not found" });
|
|
|
|
const updates: Partial<typeof courses.$inferInsert> = {};
|
|
|
|
if (body.title !== undefined) {
|
|
if (!body.title.trim()) throw createError({ statusCode: 400, message: "Title cannot be empty" });
|
|
updates.title = body.title.trim();
|
|
}
|
|
if (body.subject !== undefined) updates.subject = body.subject;
|
|
|
|
if (Object.keys(updates).length === 0) {
|
|
throw createError({ statusCode: 400, message: "Nothing to update" });
|
|
}
|
|
|
|
await db.update(courses).set(updates).where(eq(courses.id, id));
|
|
|
|
return { ok: true };
|
|
});
|