Browse Source

Trying to save the incoming entry

main
Julio Biason 2 years ago
parent
commit
4ab41b9f3a
  1. 1
      store/Cargo.toml
  2. 32
      store/src/domain/entry.rs
  3. 11
      store/src/main.rs

1
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"] } tracing-subscriber = { version = "0.3.16", default-features = false, features = ["env-filter", "fmt", "ansi"] }
axum = { version = "0.6", features = ["json"] } axum = { version = "0.6", features = ["json"] }
serde = { version = "1.0.152", features = ["derive"] } serde = { version = "1.0.152", features = ["derive"] }
uuid = { version = "1.2.2", features = ["fast-rng", "v4"] }

32
store/src/domain/entry.rs

@ -12,3 +12,35 @@ pub struct IncomingEntry {
meta: Vec<super::meta::IncomingMeta>, meta: Vec<super::meta::IncomingMeta>,
attachments: Vec<super::attachment::IncomingAttachment>, attachments: Vec<super::attachment::IncomingAttachment>,
} }
impl IncomingEntry {
pub async fn save(
&self,
transaction: &mut sqlx::Transaction<'_, sqlx::Sqlite>,
) -> Result<String, sqlx::Error> {
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,
}

11
store/src/main.rs

@ -9,6 +9,7 @@ use axum::Json;
use axum::Router; use axum::Router;
use clap::Parser; use clap::Parser;
use domain::entry::IncomingEntry; use domain::entry::IncomingEntry;
use domain::entry::OutgoingEntry;
use sqlx::sqlite::SqliteConnectOptions; use sqlx::sqlite::SqliteConnectOptions;
use sqlx::sqlite::SqlitePoolOptions; use sqlx::sqlite::SqlitePoolOptions;
use sqlx::SqlitePool; use sqlx::SqlitePool;
@ -60,8 +61,12 @@ async fn serve(state: Arc<SqlitePool>, addr: &str) {
/// ///
async fn add_entry( async fn add_entry(
State(state): State<Arc<SqlitePool>>, State(pool): State<Arc<SqlitePool>>,
Json(payload): Json<IncomingEntry>, Json(payload): Json<IncomingEntry>,
) -> Json<IncomingEntry> { ) -> Result<Json<OutgoingEntry>, sqlx::Error> {
Json(payload) let mut transaction = pool.begin().await?;
payload.save(&mut transaction).await?;
transaction.commit().await?;
Ok(Json(OutgoingEntry { id: "id".into() }))
} }

Loading…
Cancel
Save