Добавил работу с расписанием

This commit is contained in:
Artem VV 2023-05-19 21:15:27 +07:00
parent e9b8b44d1b
commit 35a20acce6
13 changed files with 939 additions and 25 deletions

View file

@ -0,0 +1,39 @@
import type { APIContext } from "astro";
import { createStudyItem, getSessionUser } from "../../db";
import { Prisma } from "@prisma/client";
export async function post({ request, cookies }: APIContext) {
const response: { ok: boolean; reason?: string } = {
ok: true,
};
try {
const sessId = cookies.get("session").value!;
const user = (await getSessionUser(sessId))!;
if (!user.is_admin) {
throw new Error("Доступно только администраторам");
}
const formData = await request.formData();
const studyItemName = formData.get("studyItemName");
if (studyItemName === null) {
throw new Error("Не предоставлены данные создания предмета");
}
await createStudyItem(studyItemName.toString());
response.ok = true;
} catch (e: any) {
response.ok = false;
if (e instanceof Prisma.PrismaClientKnownRequestError) {
response.reason = `Неизвестная ошибка базы данных. Код ${e.code}`;
} else if (e instanceof Error) {
response.reason = e.message.trim();
} else {
response.reason = e.toString().trim();
}
}
return {
body: JSON.stringify(response),
};
}