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::{Name, Person}, users::{ User, auth::{AuthError, UserAuthRequired, UserAuthenticate}, }, web::{components::nav::nav, pages::base}, }; pub async fn page(Path(id): Path, req: Request) -> Result { 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! | Mnemosyne".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)"} } @if let Ok(0) = name.times_attributed(&conn) && !name.is_primary { form action=(format!("/names/{}/delete", name.id)) method="post" class="flex items-center ml-1" { button type="submit" class="text-neutral-500 hover:text-red-400 flex items-center justify-center cursor-pointer" title="Delete" { "✕" } } } } } } @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, headers: HeaderMap, Form(form): Form, ) -> Result { 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()) } pub async fn delete_name( Path(id): Path, headers: HeaderMap, ) -> Result { let u = User::authenticate(&headers)?.required()?; let mut conn = database::conn()?; let tx = conn.transaction()?; let n = Name::get_by_id(&tx, id)?; let p = Person::get_by_id(&tx, n.person_id)?; let nn = n.name.clone(); n.delete(&tx)?; LogEntry::new( &tx, u, LogAction::DeletePersonName { pid: p.id, nid: id, pn: p.primary_name, n: nn, }, )?; tx.commit()?; Ok(Redirect::to(&format!("/persons/{}", p.id)).into_response()) }