person profile page, adding names

This commit is contained in:
2026-04-09 16:47:55 +02:00
parent 9fa19d6caf
commit 14abdc9e4a
4 changed files with 119 additions and 2 deletions

View File

@@ -0,0 +1,111 @@
use axum::{
Form,
extract::{Path, Request},
http::HeaderMap,
response::{IntoResponse, Redirect, Response},
};
use maud::html;
use serde::Deserialize;
use uuid::Uuid;
use crate::{
database,
error::CompositeError,
logs::{LogAction, LogEntry},
persons::Person,
users::{
User,
auth::{AuthError, UserAuthRequired, UserAuthenticate},
},
web::{components::nav::nav, pages::base},
};
pub async fn page(Path(id): Path<Uuid>, req: Request) -> Result<Response, AuthError> {
let u = match User::authenticate(req.headers())? {
Some(u) => u,
None => return Ok(Redirect::to(&format!("/login?r={}", req.uri().path())).into_response()),
};
let conn = database::conn()?;
let p = Person::get_by_id(&conn, id);
let title = match &p {
Ok(p) => format!("~{} | Mnemosyne", p.primary_name),
Err(_) => "Error!".into(),
};
Ok(base(
&title,
html!(
(nav(Some(&u), req.uri().path()))
div class="mx-auto max-w-4xl px-2 my-4" {
@if let Ok(p) = p {
div class="flex items-center gap-2 mb-6" {
span class="text-neutral-500 scale-150" {"~"}
h1 class="text-3xl font-semibold font-lora" {(p.primary_name)}
}
div {
h2 class="text-lg font-semibold font-lora mb-2 text-neutral-300" {"Names"}
div class="flex flex-wrap gap-2 mb-4" {
@if let Ok(names) = p.get_all_names(&conn) {
@for name in &names {
div class="rounded px-3 py-1 bg-neutral-200/5 border border-neutral-200/10 text-sm flex items-center gap-2" {
(name.name)
@if name.is_primary {
span class="text-xs text-neutral-500" {"(primary)"}
}
}
}
} @else {
"Failed to get names."
}
}
form action=(format!("/persons/{}/add-name", p.id)) method="post" {
label for="name" class="text-neutral-500 font-light text-sm" {"Add Name"}
div class="flex gap-2 mt-1" {
input type="text" autocomplete="off" id="name" name="name" placeholder="e.g. Frank"
class="px-2 py-1 border border-neutral-200/25 bg-neutral-950/50 rounded w-full sm:w-auto";
button type="submit"
class="px-4 py-1 border border-neutral-200/25 bg-neutral-200/5 rounded cursor-pointer hover:bg-neutral-200/10 hover:border-neutral-200/45" {"Add"}
}
}
}
} @else {
p class="text-center p-2 my-4 text-red-400" {"Person not found."}
}
}
),
)
.into_response())
}
#[derive(Deserialize)]
pub struct AddNameForm {
name: String,
}
pub async fn add_name(
Path(id): Path<Uuid>,
headers: HeaderMap,
Form(form): Form<AddNameForm>,
) -> Result<Response, CompositeError> {
let u = User::authenticate(&headers)?.required()?;
let mut conn = database::conn()?;
let tx = conn.transaction()?;
let p = Person::get_by_id(&tx, id)?;
let n = p.add_name(&tx, form.name, u.id)?;
LogEntry::new(
&tx,
u,
LogAction::AddPersonName {
pid: p.id,
nid: n.id,
pn: p.primary_name,
nn: n.name,
},
)?;
tx.commit()?;
Ok(Redirect::to(&format!("/persons/{}", p.id)).into_response())
}