postgres via sqlx - workable?
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
use axum::{
|
||||
Json,
|
||||
extract::Path,
|
||||
extract::{Path, State},
|
||||
http::{HeaderMap, StatusCode},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use chrono::{DateTime, FixedOffset};
|
||||
use chrono::NaiveDateTime;
|
||||
use serde::Deserialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
database::{self},
|
||||
MnemoState,
|
||||
error::CompositeError,
|
||||
logs::{LogAction, LogEntry},
|
||||
persons::Name,
|
||||
@@ -21,12 +21,13 @@ use crate::{
|
||||
};
|
||||
|
||||
pub async fn get_by_id(
|
||||
State(state): State<MnemoState>,
|
||||
Path(id): Path<Uuid>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<Response, CompositeError> {
|
||||
User::authenticate(&headers)?.required()?;
|
||||
let conn = database::conn()?;
|
||||
Ok(Json(Quote::get_by_id(&conn, id)?).into_response())
|
||||
let mut conn = state.pool.acquire().await?;
|
||||
User::authenticate(&mut conn, &headers).await?.required()?;
|
||||
Ok(Json(Quote::get_by_id(&mut conn, id).await?).into_response())
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@@ -38,37 +39,38 @@ pub struct QuoteLineForm {
|
||||
#[derive(Deserialize)]
|
||||
pub struct QuoteCreateForm {
|
||||
pub lines: Vec<QuoteLineForm>,
|
||||
pub timestamp: DateTime<FixedOffset>,
|
||||
pub timestamp: NaiveDateTime,
|
||||
pub context: Option<String>,
|
||||
pub location: Option<String>,
|
||||
pub public: bool,
|
||||
}
|
||||
|
||||
pub async fn create(
|
||||
State(state): State<MnemoState>,
|
||||
headers: HeaderMap,
|
||||
Json(form): Json<QuoteCreateForm>,
|
||||
) -> 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 lines = form
|
||||
.lines
|
||||
.into_iter()
|
||||
.map(|l| Ok((l.content, Name::get_by_id(&tx, l.name_id)?)))
|
||||
.collect::<Result<Vec<(String, Name)>, CompositeError>>()?;
|
||||
let mut lines = Vec::with_capacity(form.lines.len());
|
||||
for l in form.lines {
|
||||
let name = Name::get_by_id(&mut tx, l.name_id).await?;
|
||||
lines.push((l.content, name));
|
||||
}
|
||||
|
||||
let q = Quote::create(
|
||||
&tx,
|
||||
&mut tx,
|
||||
lines,
|
||||
form.timestamp,
|
||||
form.context,
|
||||
form.location,
|
||||
u.id,
|
||||
form.public,
|
||||
)?;
|
||||
)
|
||||
.await?;
|
||||
|
||||
LogEntry::new(&tx, u, LogAction::CreateQuote { id: q.id })?;
|
||||
tx.commit()?;
|
||||
LogEntry::new(&mut tx, u, LogAction::CreateQuote { id: q.id }).await?;
|
||||
tx.commit().await?;
|
||||
Ok((StatusCode::CREATED, Json(q)).into_response())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user