diff --git a/prisma/schema.prisma b/prisma/schema.prisma index f9cb756..c55a71e 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -58,6 +58,7 @@ model users { pass String @db.VarChar(100) fullName String? @db.VarChar(100) is_admin Boolean @default(false) + lore String? @db.Text() timetable timetable[] user_session user_session[] } diff --git a/src/db.ts b/src/db.ts index 723060f..b37857b 100644 --- a/src/db.ts +++ b/src/db.ts @@ -56,6 +56,15 @@ export async function getUserFromAuth(login: string, password: string) { return user; } +export async function getUserFromLogin(login: string) { + const user = await client.users.findFirst({ + where: { + login: login, + }, + }); + return user; +} + export async function createUser(login: string, password: string) { const anyAdmins = await isThereAnyAdmins(); const user = await client.users.create({ @@ -80,6 +89,30 @@ export async function updateUserPassword(login: string, newPassword: string) { return user; } +export async function updateUserFullName(login: string, newFullName: string) { + const user = await client.users.update({ + where: { + login: login, + }, + data: { + fullName: newFullName, + }, + }); + return user; +} + +export async function updateUserLore(login: string, newLore: string) { + const user = await client.users.update({ + where: { + login: login, + }, + data: { + lore: newLore, + }, + }); + return user; +} + export async function deleteUser(login: string) { const user = await client.users.delete({ where: { @@ -173,7 +206,11 @@ export async function searchUsers(params: { login?: string; isAdmin?: boolean; f contains: params.login, }, is_admin: params.isAdmin, - fullName: params.fullName, + fullName: params.fullName + ? { + contains: params.fullName, + } + : undefined, }, orderBy: { id: "asc", diff --git a/src/pages/user/[user].astro b/src/pages/user/[user].astro new file mode 100644 index 0000000..4492ef6 --- /dev/null +++ b/src/pages/user/[user].astro @@ -0,0 +1,275 @@ +--- +import Layout from "../../layouts/Layout.astro"; + +import { getUserSession, getSessionUser, getUserFromLogin } from "../../db"; +import Navbar from "../../components/Navbar.astro"; + +if (Astro.cookies.has("session")) { + const sessId = Astro.cookies.get("session").value!; + const dbSess = await getUserSession(sessId); + if (dbSess === null) { + Astro.cookies.delete("session"); + return Astro.redirect("/login"); + } +} else { + return Astro.redirect("/login"); +} + +const sessId = Astro.cookies.get("session").value!; +const user = (await getSessionUser(sessId))!; +if (user === null) { + return Astro.redirect("/"); +} + +const openedUser = await getUserFromLogin(Astro.params.user ?? ""); +if (openedUser === null) { + return Astro.redirect("/users"); +} + +const isCurrentUser = user.id === openedUser.id; +--- + + +
+ +
+
+ + +
+
+ + { + isCurrentUser ? ( +
+ + +
+ ) : ( + + ) + } +
+
+ + { + isCurrentUser ? + + : null + } +
+
+
+
+ + + + + + + + + { + isCurrentUser ? + : } + + +
+ +{ + isCurrentUser ? + : null +} + + diff --git a/src/pages/userapi/updateFullName.ts b/src/pages/userapi/updateFullName.ts new file mode 100644 index 0000000..544002f --- /dev/null +++ b/src/pages/userapi/updateFullName.ts @@ -0,0 +1,44 @@ +import type { APIContext } from "astro"; +import { updateUserFullName, 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 fullName = formData.get("fullName"); + if (login === null || fullName === null) { + throw new Error("Не предоставлены данные для обновления Ф.И.О."); + } + + if (user.login !== login.toString()) { + throw new Error("Доступно только этому пользователю"); + } + + const updatedUser = await updateUserFullName(login.toString(), fullName.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), + }; +} diff --git a/src/pages/userapi/updateUserLore.ts b/src/pages/userapi/updateUserLore.ts new file mode 100644 index 0000000..374c1ec --- /dev/null +++ b/src/pages/userapi/updateUserLore.ts @@ -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), + }; +} diff --git a/src/pages/users.astro b/src/pages/users.astro index 8868e5b..37573c6 100644 --- a/src/pages/users.astro +++ b/src/pages/users.astro @@ -65,7 +65,7 @@ const users = await searchUsers({ - + Открыть профиль