117 lines
4.7 KiB
Rust
117 lines
4.7 KiB
Rust
use axum::{
|
|
Form,
|
|
extract::Request,
|
|
http::HeaderMap,
|
|
response::{IntoResponse, Redirect, Response},
|
|
};
|
|
use maud::{PreEscaped, html};
|
|
use serde::Deserialize;
|
|
|
|
use crate::{
|
|
database::{self},
|
|
error::CompositeError,
|
|
logs::{LogAction, LogEntry},
|
|
persons::Person,
|
|
users::{
|
|
User,
|
|
auth::{AuthError, UserAuthRequired, UserAuthenticate},
|
|
},
|
|
web::{components::nav::nav, icons, pages::base},
|
|
};
|
|
|
|
pub async fn page(req: Request) -> Result<Response, AuthError> {
|
|
let u = User::authenticate(req.headers())?;
|
|
let mut conn = database::conn()?;
|
|
let tx = conn.transaction()?;
|
|
|
|
Ok(base(
|
|
"Persons | Mnemosyne",
|
|
html!(
|
|
(nav(u.as_ref(), req.uri().path()))
|
|
|
|
@if let Some(_) = u {
|
|
div class="mx-auto max-w-4xl px-2 my-4" {
|
|
p class="flex items-center gap-2" {
|
|
span class="text-neutral-500" {(PreEscaped(icons::CONTACT))}
|
|
span class="text-2xl font-semibold font-lora" {"Persons"}
|
|
}
|
|
p class="text-neutral-500 text-sm font-light" {
|
|
@if let Ok(c) = Person::total_count(&tx) {
|
|
(c) " persons in total."
|
|
} @else {
|
|
"Could not get total person count."
|
|
}
|
|
}
|
|
}
|
|
@if let Ok(persons) = Person::get_all(&tx) {
|
|
div class="max-w-4xl mx-auto px-2 mt-4 flex flex-wrap gap-2" {
|
|
@for person in &persons {
|
|
div class="rounded px-4 py-2 bg-neutral-200/10 border border-neutral-200/15 flex items-center" {
|
|
span class="text-neutral-400 mr-1 scale-125" {"~"}
|
|
span class="text-sm" {(person.primary_name)}
|
|
div class="w-px h-2/3 my-auto mx-2 bg-neutral-200/15" {}
|
|
div class="text-xs flex items-center" {
|
|
(
|
|
if let Ok(i) = person.get_in_quote_count(&tx) {
|
|
i.to_string()
|
|
} else {
|
|
"?".to_string()
|
|
}
|
|
) span class="*:size-3 ml-1 text-neutral-400" {(PreEscaped(icons::SCROLL_TEXT))}
|
|
// div class="ml-2" {}
|
|
// "4" span class="*:size-3 ml-1 text-neutral-400" {(PreEscaped(icons::FILE_IMAGE))}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@if persons.is_empty() {
|
|
p class="text-center p-2" {"No persons yet."}
|
|
}
|
|
div class="mx-auto max-w-4xl mt-4 px-2" {
|
|
h3 class="font-lora font-semibold text-xl" {"Add new person"}
|
|
form action="/persons/create" method="post" {
|
|
label for="primary_name" class="text-neutral-500 font-light mt-2" {"Primary Name"}
|
|
div class="flex gap-2" {
|
|
input type="text" autocomplete="off" id="primary_name" name="primary_name" placeholder="e.g. Frank"
|
|
class="px-2 py-1 border border-neutral-200/25 bg-neutral-950/50 rounded";
|
|
button type="submit"
|
|
class="px-4 py-1 border border-neutral-200/25 bg-neutral-200/5 rounded cursor-pointer hover:border-neutral-200/40" {"Submit"}
|
|
}
|
|
}
|
|
}
|
|
} @else {
|
|
p class="text-red-400 text-center" {"Failed to load persons."}
|
|
}
|
|
} @else {
|
|
p class="text-center p-2" {"You must be logged in to view this page."}
|
|
}
|
|
),
|
|
)
|
|
.into_response())
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct PersonNameForm {
|
|
primary_name: String,
|
|
}
|
|
pub async fn create(
|
|
headers: HeaderMap,
|
|
Form(form): Form<PersonNameForm>,
|
|
) -> Result<Response, CompositeError> {
|
|
let u = User::authenticate(&headers)?.required()?;
|
|
let mut conn = database::conn()?;
|
|
let tx = conn.transaction()?;
|
|
|
|
let p = Person::create(&tx, form.primary_name, u.id)?;
|
|
LogEntry::new(
|
|
&tx,
|
|
u,
|
|
LogAction::CreatePerson {
|
|
id: p.id,
|
|
pname: p.primary_name,
|
|
},
|
|
)?;
|
|
tx.commit()?;
|
|
Ok(Redirect::to("/persons").into_response())
|
|
}
|