name delete if 0 attributions
This commit is contained in:
@@ -12,7 +12,7 @@ use crate::{
|
||||
database,
|
||||
error::CompositeError,
|
||||
logs::{LogAction, LogEntry},
|
||||
persons::Person,
|
||||
persons::{Name, Person},
|
||||
users::{
|
||||
User,
|
||||
auth::{AuthError, UserAuthRequired, UserAuthenticate},
|
||||
@@ -29,7 +29,7 @@ pub async fn page(Path(id): Path<Uuid>, req: Request) -> Result<Response, AuthEr
|
||||
let p = Person::get_by_id(&conn, id);
|
||||
let title = match &p {
|
||||
Ok(p) => format!("~{} | Mnemosyne", p.primary_name),
|
||||
Err(_) => "Error!".into(),
|
||||
Err(_) => "Error! | Mnemosyne".into(),
|
||||
};
|
||||
|
||||
Ok(base(
|
||||
@@ -53,6 +53,13 @@ pub async fn page(Path(id): Path<Uuid>, req: Request) -> Result<Response, AuthEr
|
||||
@if name.is_primary {
|
||||
span class="text-xs text-neutral-500" {"(primary)"}
|
||||
}
|
||||
@if let Ok(0) = name.times_attributed(&conn) {
|
||||
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 {
|
||||
@@ -109,3 +116,32 @@ pub async fn add_name(
|
||||
|
||||
Ok(Redirect::to(&format!("/persons/{}", p.id)).into_response())
|
||||
}
|
||||
|
||||
pub async fn delete_name(
|
||||
Path(id): Path<Uuid>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<Response, CompositeError> {
|
||||
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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user