postgres via sqlx - workable?
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use axum::{
|
||||
Form,
|
||||
extract::{Path, Request},
|
||||
extract::{Path, Request, State},
|
||||
http::HeaderMap,
|
||||
response::{IntoResponse, Redirect, Response},
|
||||
};
|
||||
@@ -9,29 +9,44 @@ use serde::Deserialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
database,
|
||||
error::CompositeError,
|
||||
logs::{LogAction, LogEntry},
|
||||
persons::{Name, Person},
|
||||
users::{
|
||||
User,
|
||||
auth::{AuthError, UserAuthRequired, UserAuthenticate},
|
||||
auth::{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())? {
|
||||
pub async fn page(
|
||||
State(state): State<crate::MnemoState>,
|
||||
Path(id): Path<Uuid>,
|
||||
req: Request,
|
||||
) -> Result<Response, CompositeError> {
|
||||
let mut conn = state.pool.acquire().await?;
|
||||
let u = match User::authenticate(&mut *conn, req.headers()).await? {
|
||||
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 p = Person::get_by_id(&mut *conn, id).await;
|
||||
let title = match &p {
|
||||
Ok(p) => format!("~{} | Mnemosyne", p.primary_name),
|
||||
Err(_) => "Error! | Mnemosyne".into(),
|
||||
};
|
||||
|
||||
let mut names_with_attribution = Vec::new();
|
||||
let mut names_ok = false;
|
||||
if let Ok(ref person) = p {
|
||||
if let Ok(names) = person.get_all_names(&mut *conn).await {
|
||||
names_ok = true;
|
||||
for name in names {
|
||||
let attr = name.times_attributed(&mut *conn).await.unwrap_or(0);
|
||||
names_with_attribution.push((name, attr));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(base(
|
||||
&title,
|
||||
html!(
|
||||
@@ -46,14 +61,14 @@ pub async fn page(Path(id): Path<Uuid>, req: Request) -> Result<Response, AuthEr
|
||||
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 {
|
||||
@if names_ok {
|
||||
@for (name, attr) in names_with_attribution {
|
||||
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 {
|
||||
@if attr == 0 && !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" {
|
||||
"✕"
|
||||
@@ -91,19 +106,19 @@ pub struct AddNameForm {
|
||||
}
|
||||
|
||||
pub async fn add_name(
|
||||
State(state): State<crate::MnemoState>,
|
||||
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 mut tx = state.pool.begin().await?;
|
||||
let u = User::authenticate(&mut *tx, &headers).await?.required()?;
|
||||
|
||||
let p = Person::get_by_id(&tx, id)?;
|
||||
let n = p.add_name(&tx, form.name, u.id)?;
|
||||
let p = Person::get_by_id(&mut *tx, id).await?;
|
||||
let n = p.add_name(&mut *tx, form.name, u.id).await?;
|
||||
|
||||
LogEntry::new(
|
||||
&tx,
|
||||
&mut *tx,
|
||||
u,
|
||||
LogAction::AddPersonName {
|
||||
pid: p.id,
|
||||
@@ -111,28 +126,29 @@ pub async fn add_name(
|
||||
pn: p.primary_name,
|
||||
nn: n.name,
|
||||
},
|
||||
)?;
|
||||
tx.commit()?;
|
||||
)
|
||||
.await?;
|
||||
tx.commit().await?;
|
||||
|
||||
Ok(Redirect::to(&format!("/persons/{}", p.id)).into_response())
|
||||
}
|
||||
|
||||
pub async fn delete_name(
|
||||
State(state): State<crate::MnemoState>,
|
||||
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 mut tx = state.pool.begin().await?;
|
||||
let u = User::authenticate(&mut *tx, &headers).await?.required()?;
|
||||
|
||||
let n = Name::get_by_id(&tx, id)?;
|
||||
let p = Person::get_by_id(&tx, n.person_id)?;
|
||||
let n = Name::get_by_id(&mut *tx, id).await?;
|
||||
let p = Person::get_by_id(&mut *tx, n.person_id).await?;
|
||||
|
||||
let nn = n.name.clone();
|
||||
n.delete(&tx)?;
|
||||
n.delete(&mut *tx).await?;
|
||||
|
||||
LogEntry::new(
|
||||
&tx,
|
||||
&mut *tx,
|
||||
u,
|
||||
LogAction::DeletePersonName {
|
||||
pid: p.id,
|
||||
@@ -140,8 +156,9 @@ pub async fn delete_name(
|
||||
pn: p.primary_name,
|
||||
n: nn,
|
||||
},
|
||||
)?;
|
||||
tx.commit()?;
|
||||
)
|
||||
.await?;
|
||||
tx.commit().await?;
|
||||
|
||||
Ok(Redirect::to(&format!("/persons/{}", p.id)).into_response())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user