postgres via sqlx - workable?
This commit is contained in:
@@ -4,14 +4,12 @@ use axum::{
|
||||
response::{IntoResponse, Redirect, Response},
|
||||
};
|
||||
use axum_extra::extract::Form;
|
||||
use chrono::{TimeZone, Utc};
|
||||
use chrono_tz::Europe::Warsaw;
|
||||
use chrono::NaiveDateTime;
|
||||
use maud::{Markup, PreEscaped, html};
|
||||
use serde::Deserialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
database,
|
||||
error::CompositeError,
|
||||
logs::{LogAction, LogEntry},
|
||||
persons::Name,
|
||||
@@ -26,13 +24,16 @@ use crate::{
|
||||
const LINE_ADD_RM_SCRIPT: &str = include_str!("line-add-rm.js");
|
||||
const PREFILL_TIME_SCRIPT: &str = include_str!("prefill-time.js");
|
||||
|
||||
pub async fn page(req: Request) -> Result<Response, CompositeError> {
|
||||
let u = match User::authenticate(req.headers())? {
|
||||
pub async fn page(
|
||||
axum::extract::State(state): axum::extract::State<crate::MnemoState>,
|
||||
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 names = Name::get_all(&conn)?;
|
||||
let names = Name::get_all(&mut *conn).await?;
|
||||
|
||||
Ok(base(
|
||||
"Add Quote | Mnemosyne",
|
||||
@@ -137,30 +138,27 @@ pub struct IncomingQuote {
|
||||
authors: Vec<Uuid>,
|
||||
location: String,
|
||||
time: String,
|
||||
tz_offset: Option<i32>,
|
||||
context: String,
|
||||
}
|
||||
pub async fn form(
|
||||
axum::extract::State(state): axum::extract::State<crate::MnemoState>,
|
||||
headers: HeaderMap,
|
||||
Form(form): Form<IncomingQuote>,
|
||||
) -> 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 authors = form
|
||||
.authors
|
||||
.into_iter()
|
||||
.map(|nid| Name::get_by_id(&tx, nid).unwrap());
|
||||
let mut authors = Vec::new();
|
||||
for nid in form.authors {
|
||||
authors.push(Name::get_by_id(&mut *tx, nid).await.unwrap());
|
||||
}
|
||||
let lines = form.lines.into_iter().zip(authors).collect();
|
||||
let offset = form
|
||||
.tz_offset
|
||||
.and_then(|mins| chrono::FixedOffset::west_opt(mins * 60))
|
||||
.unwrap_or_else(|| chrono::FixedOffset::west_opt(0).unwrap());
|
||||
|
||||
let timestamp = chrono::NaiveDateTime::parse_from_str(&form.time, "%Y-%m-%dT%H:%M")
|
||||
.map(|ndt| offset.from_local_datetime(&ndt).unwrap())
|
||||
.unwrap_or_else(|_| Utc::now().with_timezone(&Warsaw).fixed_offset());
|
||||
let timestamp = match NaiveDateTime::parse_from_str(&form.time, "%Y-%m-%dT%H:%M") {
|
||||
Ok(ts) => ts,
|
||||
Err(_) => return Ok("Time was formatted wrong.".into_response()),
|
||||
};
|
||||
|
||||
let context = match form.context.trim() {
|
||||
"" => None,
|
||||
s => Some(s.to_string()),
|
||||
@@ -170,9 +168,9 @@ pub async fn form(
|
||||
s => Some(s.to_string()),
|
||||
};
|
||||
|
||||
let q = Quote::create(&tx, lines, timestamp, context, location, u.id, false)?;
|
||||
LogEntry::new(&tx, u, LogAction::CreateQuote { id: q.id })?;
|
||||
tx.commit()?;
|
||||
let q = Quote::create(&mut *tx, lines, timestamp, context, location, u.id, false).await?;
|
||||
LogEntry::new(&mut *tx, u, LogAction::CreateQuote { id: q.id }).await?;
|
||||
tx.commit().await?;
|
||||
|
||||
Ok(Redirect::to("/dashboard").into_response())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user