Добавил изменение ФИО и инфы о пользователе
This commit is contained in:
parent
c8bc1c745d
commit
5c72b569da
6 changed files with 403 additions and 2 deletions
44
src/pages/userapi/updateUserLore.ts
Normal file
44
src/pages/userapi/updateUserLore.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import type { APIContext } from "astro";
|
||||
import { updateUserLore, 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))!;
|
||||
|
||||
const formData = await request.formData();
|
||||
const login = formData.get("login");
|
||||
const userLore = formData.get("userLore");
|
||||
if (login === null || userLore === null) {
|
||||
throw new Error("Не предоставлены данные для обновления информации");
|
||||
}
|
||||
|
||||
if (user.login !== login.toString()) {
|
||||
throw new Error("Доступно только этому пользователю");
|
||||
}
|
||||
|
||||
const updatedUser = await updateUserLore(login.toString(), userLore.toString());
|
||||
if (updatedUser === null) {
|
||||
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