Доделываю перед сдачей
This commit is contained in:
parent
1175ccff74
commit
0ebbe91f4f
15 changed files with 906 additions and 15 deletions
39
src/pages/docsapi/delete.ts
Normal file
39
src/pages/docsapi/delete.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import type { APIContext } from "astro";
|
||||
import { getSessionUser, deleteDocument } 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))!;
|
||||
|
||||
const json = await request.json();
|
||||
const docId = json.docId as string;
|
||||
if (!docId) {
|
||||
throw new Error("Предоставлены некорректные данные");
|
||||
}
|
||||
|
||||
const ok = await deleteDocument(docId);
|
||||
if (!ok) {
|
||||
throw new Error("Неизвестная ошибка");
|
||||
}
|
||||
|
||||
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),
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue