diff --git a/src/main.rs b/src/main.rs index 441af8f0ca..854fce23f8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ pub mod server_state; +pub mod queries; use actix_cors::Cors; use actix_http::http; @@ -9,6 +10,7 @@ use std::sync::Arc; use crate::server_state::{ServerState, EnvConfig}; use sqlx::MySqlPool; use sqlx::mysql::MySqlPoolOptions; +use crate::queries::badges::NewBadge; const DEFAULT_BIND_ADDRESS : &'static str = "0.0.0.0:12345"; const DEFAULT_RUST_LOG: &'static str = "debug,actix_web=info"; @@ -80,9 +82,26 @@ pub async fn serve(server_state: ServerState) -> AnyhowResult<()> */ //create_user(&pool).await?; - let id = create_badge(&pool, "slug", "this is title", "decr", "img").await?; + + let badge = NewBadge { + slug: "slug2".to_string(), + title: "title".to_string(), + description: "description".to_string(), + image_url: "url".to_string() + }; + + let id = badge.insert(&pool).await?; println!("Created id: {}", id); + let badge = NewBadge { + slug: "slug3".to_string(), + title: "title".to_string(), + description: "description".to_string(), + image_url: "url".to_string() + }; + + let id = badge.insert(&pool).await?; + println!("Created id: {}", id); let server_state_arc = web::Data::new(Arc::new(server_state)); @@ -166,25 +185,6 @@ pub async fn serve(server_state: ServerState) -> AnyhowResult<()> Ok(()) } -pub async fn create_badge(pool: &MySqlPool, slug: &str, title: &str, description: &str, image_url: &str) -> AnyhowResult { - // Insert the task, then obtain the ID of this row - let todo_id = sqlx::query!( - r#" -INSERT INTO badges ( slug, title, description, image_url ) -VALUES ( ?, ?, ?, ? ) - "#, - slug, - title, - description, - image_url, - ) - .execute(pool) - .await? - .last_insert_id(); - - Ok(todo_id) -} - pub async fn create_user(pool: &MySqlPool) -> AnyhowResult<()> { let mut tx = pool.begin().await?; let todo = sqlx::query("INSERT INTO badges (slug, title, description, image_url) VALUES ($1, $2, $3, $4)") diff --git a/src/queries/badges.rs b/src/queries/badges.rs new file mode 100644 index 0000000000..9425f0069e --- /dev/null +++ b/src/queries/badges.rs @@ -0,0 +1,29 @@ +use sqlx::MySqlPool; +use crate::AnyhowResult; + +pub struct NewBadge { + pub slug: String, + pub title: String, + pub description: String, + pub image_url: String, +} + +impl NewBadge { + pub async fn insert(&self, pool: &MySqlPool) -> AnyhowResult { + let record_id = sqlx::query!( + r#" +INSERT INTO badges ( slug, title, description, image_url ) +VALUES ( ?, ?, ?, ? ) + "#, + self.slug, + self.title, + self.description, + self.image_url, + ) + .execute(pool) + .await? + .last_insert_id(); + + Ok(record_id) + } +} diff --git a/src/queries/mod.rs b/src/queries/mod.rs new file mode 100644 index 0000000000..2e6f0082b7 --- /dev/null +++ b/src/queries/mod.rs @@ -0,0 +1,2 @@ +pub mod users; +pub mod badges; \ No newline at end of file diff --git a/src/queries/users.rs b/src/queries/users.rs new file mode 100644 index 0000000000..e69de29bb2