diff --git a/sqlxtest/src/main.rs b/sqlxtest/src/main.rs index 65c8c60..f18fc5a 100644 --- a/sqlxtest/src/main.rs +++ b/sqlxtest/src/main.rs @@ -1,33 +1,53 @@ +use sqlx::sqlite::Sqlite; use sqlx::sqlite::SqliteConnectOptions; use sqlx::sqlite::SqlitePoolOptions; use sqlx::Pool; -// use std::env; +use std::env; struct Label { - id: u32, + id: Option, description: String, } -struct Repository<'s, T: sqlx::Database + sqlx::Executor<'s>> { - pool: Pool, +struct LabelRepository<'a> { + pool: &'a Pool, } -impl<'s, T: sqlx::Database + sqlx::Executor<'s>> Repository<'s, T> { - fn new(pool: Pool) -> Self { +impl<'a> LabelRepository<'a> { + fn new(pool: &'a Pool) -> Self { Self { pool } } async fn save(&self, label: &Label) -> Result<(), sqlx::Error> { sqlx::query(r#"INSERT INTO testing (label) VALUES (?)"#) - .bind(label.description) - .execute(&self.pool) + .bind(&label.description) + .execute(self.pool) + .await?; + Ok(()) + } + + async fn find_by_description(&self, label: &str) -> Result { + let result: (u32, String) = + sqlx::query_as(r#"SELECT id, label FROM testing WHERE label = ?"#) + .bind(label) + .fetch_one(self.pool) + .await?; + Ok(Label { + id: Some(result.0), + description: result.1, + }) + } + + async fn delete(&self, record: &Label) -> Result<(), sqlx::Error> { + sqlx::query(r#"DELETE FROM testing WHERE id = ?"#) + .bind(&record.id) + .execute(self.pool) .await?; Ok(()) } } -#[tokio::main] -async fn main() -> Result<(), sqlx::Error> { +async fn connect() -> Result, sqlx::Error> { println!("Open database"); let pool = SqlitePoolOptions::new() .connect_with( @@ -37,21 +57,38 @@ async fn main() -> Result<(), sqlx::Error> { ) .await?; sqlx::migrate!("db/migrations").run(&pool).await?; + Ok(pool) +} + +#[tokio::main] +async fn main() -> Result<(), sqlx::Error> { + let pool = connect().await?; + let repo = LabelRepository::new(&pool); - let repo = Repository::new(pool); - - // let command = env::args().nth(1).unwrap(); - // println!("Command: \"{}\"", command); - // if command == "add" { - // let value = env::args().nth(2).unwrap(); - // println!("Should add \"{}\"", value); - // } else if command == "remove" { - // let value = env::args().nth(2).unwrap(); - // println!("Should remove \"{}\"", value); - // sqlx::query(r#"DELETE FROM testing WHERE label = ?"#) - // .bind(value) - // .execute(&pool) - // .await?; - // } + let command = env::args() + .nth(1) + .expect("Need a command: \"add\" or \"remove\""); + let value = env::args() + .nth(2) + .expect("Besides the command, need a label to deal with"); + + println!("Command: \"{}\"", command); + + if command == "add" { + println!("Should add \"{}\"", value); + let record = Label { + id: None, + description: value.into(), + }; + repo.save(&record).await?; + } else if command == "remove" { + println!("Should remove \"{}\"", value); + if let Ok(record) = repo.find_by_description(&value).await { + repo.delete(&record).await?; + println!("Removed"); + } else { + println!("Label does not exist"); + } + } Ok(()) }