From 4ab41b9f3adcaf470a68cf9621f5ffc242595af4 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Mon, 16 Jan 2023 14:44:34 -0300 Subject: [PATCH] Trying to save the incoming entry --- store/Cargo.toml | 1 + store/src/domain/entry.rs | 32 ++++++++++++++++++++++++++++++++ store/src/main.rs | 11 ++++++++--- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/store/Cargo.toml b/store/Cargo.toml index 4069efe..a5cfefd 100644 --- a/store/Cargo.toml +++ b/store/Cargo.toml @@ -16,3 +16,4 @@ tracing = "0.1.37" tracing-subscriber = { version = "0.3.16", default-features = false, features = ["env-filter", "fmt", "ansi"] } axum = { version = "0.6", features = ["json"] } serde = { version = "1.0.152", features = ["derive"] } +uuid = { version = "1.2.2", features = ["fast-rng", "v4"] } diff --git a/store/src/domain/entry.rs b/store/src/domain/entry.rs index c228819..a89ea16 100644 --- a/store/src/domain/entry.rs +++ b/store/src/domain/entry.rs @@ -12,3 +12,35 @@ pub struct IncomingEntry { meta: Vec, attachments: Vec, } + +impl IncomingEntry { + pub async fn save( + &self, + transaction: &mut sqlx::Transaction<'_, sqlx::Sqlite>, + ) -> Result { + let id: String = uuid::Uuid::new_v4() + .hyphenated() + .encode_lower(&mut uuid::Uuid::encode_buffer()) + .into(); + + sqlx::query( + r#"INSERT INTO entry + (id, title, content, source) + VALUES + (?, ?, ?, ?)"#, + ) + .bind(&id) + .bind(&self.title) + .bind(&self.content) + .bind(&self.source) + .execute(transaction) + .await?; + + Ok(id.into()) + } +} + +#[derive(Serialize)] +pub struct OutgoingEntry { + pub id: String, +} diff --git a/store/src/main.rs b/store/src/main.rs index c97e32c..9499cbf 100644 --- a/store/src/main.rs +++ b/store/src/main.rs @@ -9,6 +9,7 @@ use axum::Json; use axum::Router; use clap::Parser; use domain::entry::IncomingEntry; +use domain::entry::OutgoingEntry; use sqlx::sqlite::SqliteConnectOptions; use sqlx::sqlite::SqlitePoolOptions; use sqlx::SqlitePool; @@ -60,8 +61,12 @@ async fn serve(state: Arc, addr: &str) { /// async fn add_entry( - State(state): State>, + State(pool): State>, Json(payload): Json, -) -> Json { - Json(payload) +) -> Result, sqlx::Error> { + let mut transaction = pool.begin().await?; + payload.save(&mut transaction).await?; + transaction.commit().await?; + + Ok(Json(OutgoingEntry { id: "id".into() })) }