diff --git a/src/db/connection.rs b/src/db/connection.rs new file mode 100644 index 0000000..8dfd102 --- /dev/null +++ b/src/db/connection.rs @@ -0,0 +1,13 @@ +use sqlx::SqlitePool; +use std::sync::Arc; +pub struct Connection { + pub sqlite_pool: Arc, +} + +impl Connection { + pub fn new(pool: SqlitePool) -> Self { + Self { + sqlite_pool: Arc::new(pool), + } + } +} diff --git a/src/db/contact_repo.rs b/src/db/contact_repo.rs index e5e691c..86d0abc 100644 --- a/src/db/contact_repo.rs +++ b/src/db/contact_repo.rs @@ -1,33 +1,20 @@ -use std::sync::Arc; - use crate::models; use async_trait::async_trait; -use sqlx::SqlitePool; + +use super::connection::Connection; #[cfg_attr(test, mockall::automock)] #[async_trait] pub trait ContactRepo { - async fn create(&self, contact: models::Contact) -> anyhow::Result; - async fn get_all(&self) -> anyhow::Result>; - async fn update(&self, update: models::ContactBuilder) -> anyhow::Result<()>; - async fn get_by_id(&self, id: i64) -> anyhow::Result; -} - -pub struct Connection { - sqlite_pool: Arc, -} - -impl Connection { - pub fn new(pool: SqlitePool) -> Self { - Self { - sqlite_pool: Arc::new(pool), - } - } + async fn create_contact(&self, contact: models::Contact) -> anyhow::Result; + async fn get_all_contacts(&self) -> anyhow::Result>; + async fn update_contact(&self, update: models::ContactBuilder) -> anyhow::Result<()>; + async fn get_contact_by_id(&self, id: i64) -> anyhow::Result; } #[async_trait] impl ContactRepo for Connection { - async fn create(&self, contact: models::Contact) -> anyhow::Result { + async fn create_contact(&self, contact: models::Contact) -> anyhow::Result { let query = "INSERT INTO contacts (first_name, last_name, display_name, email, phone_number) VALUES (?, ?, ?, ?, ?)"; @@ -43,7 +30,7 @@ impl ContactRepo for Connection { Ok(result.last_insert_rowid()) } - async fn get_all(&self) -> anyhow::Result> { + async fn get_all_contacts(&self) -> anyhow::Result> { let get_contacts_query = "SELECT id, first_name, last_name, display_name, email, phone_number FROM contacts @@ -57,7 +44,7 @@ impl ContactRepo for Connection { Ok(contacts_with_id) } - async fn update(&self, contact: models::ContactBuilder) -> anyhow::Result<()> { + async fn update_contact(&self, contact: models::ContactBuilder) -> anyhow::Result<()> { sqlx::query!( r#" UPDATE contacts @@ -84,7 +71,7 @@ impl ContactRepo for Connection { Ok(()) } - async fn get_by_id(&self, id: i64) -> anyhow::Result { + async fn get_contact_by_id(&self, id: i64) -> anyhow::Result { let query_get_by_id = "SELECT * FROM contacts WHERE id=$1"; let contact: models::IndexedContact = @@ -110,12 +97,12 @@ mod tests { models::Contact::new("John", "Smith", "johndoe@example.com", "123-456-7890").unwrap(); mock_contact_repo - .expect_create() + .expect_create_contact() .times(1) .with(eq(test_contact.clone())) .returning(|_| Ok(1)); - let result = mock_contact_repo.create(test_contact).await; + let result = mock_contact_repo.create_contact(test_contact).await; let result = result.unwrap(); @@ -133,11 +120,11 @@ mod tests { }]; mock_contact_repo - .expect_get_all() + .expect_get_all_contacts() .times(1) .return_once(move || Ok(contacts)); - let result = mock_contact_repo.get_all().await; + let result = mock_contact_repo.get_all_contacts().await; assert!(result.is_ok()); } @@ -147,7 +134,7 @@ mod tests { let mut mock_contact_repo = MockContactRepo::new(); mock_contact_repo - .expect_update() + .expect_update_contact() .times(1) .return_once(|_| Ok(())); @@ -161,7 +148,7 @@ mod tests { ) .unwrap(); - let result = mock_contact_repo.update(edits).await; + let result = mock_contact_repo.update_contact(edits).await; assert!(result.is_ok()); } @@ -177,12 +164,12 @@ mod tests { }; mock_contact_repo - .expect_get_by_id() + .expect_get_contact_by_id() .times(1) .with(eq(contact.id)) .return_once(|_| Ok(contact)); - let result = mock_contact_repo.get_by_id(1).await; + let result = mock_contact_repo.get_contact_by_id(1).await; assert!(result.is_ok()); diff --git a/src/db/metadata_repo.rs b/src/db/metadata_repo.rs index f45b812..75adbe2 100644 --- a/src/db/metadata_repo.rs +++ b/src/db/metadata_repo.rs @@ -1,32 +1,19 @@ -use std::sync::Arc; - use crate::models; use async_trait::async_trait; use chrono::SecondsFormat; -use sqlx::SqlitePool; + +use super::Connection; #[cfg_attr(test, mockall::automock)] #[async_trait] pub trait MetadataRepo { - async fn create(&self, metadata: models::Metadata) -> anyhow::Result; - async fn get_by_id(&self, contact_id: i64) -> anyhow::Result; -} - -pub struct Connection { - sqlite_pool: Arc, -} - -impl Connection { - pub fn new(pool: SqlitePool) -> Self { - Self { - sqlite_pool: Arc::new(pool), - } - } + async fn create_metadata(&self, metadata: models::Metadata) -> anyhow::Result; + async fn get_metadata_by_id(&self, contact_id: i64) -> anyhow::Result; } #[async_trait] impl MetadataRepo for Connection { - async fn create(&self, metadata: models::Metadata) -> anyhow::Result { + async fn create_metadata(&self, metadata: models::Metadata) -> anyhow::Result { let query = "INSERT INTO contact_metadata (contact_id, starred, @@ -74,7 +61,7 @@ impl MetadataRepo for Connection { Ok(result.last_insert_rowid()) } - async fn get_by_id(&self, contact_id: i64) -> anyhow::Result { + async fn get_metadata_by_id(&self, contact_id: i64) -> anyhow::Result { let query_get_by_id = "SELECT * FROM contact_metadata WHERE contact_id=$1"; let metadata: models::Metadata = sqlx::query_as::<_, models::Metadata>(query_get_by_id) @@ -125,7 +112,7 @@ mod tests { let test_metadata = models::Metadata::default(); - let result = repo.create(test_metadata.clone()).await.unwrap(); + let result = repo.create_metadata(test_metadata.clone()).await.unwrap(); assert!(result > 0); } @@ -136,12 +123,12 @@ mod tests { let test_metadata = models::Metadata::default(); mock_metadata_repo - .expect_create() + .expect_create_metadata() .times(1) .with(eq(test_metadata.clone())) .returning(|_| Ok(1)); - let result = mock_metadata_repo.create(test_metadata).await; + let result = mock_metadata_repo.create_metadata(test_metadata).await; let result = result.unwrap(); @@ -161,12 +148,12 @@ mod tests { let test_metadata_clone = test_metadata.clone(); mock_metadata_repo - .expect_get_by_id() + .expect_get_metadata_by_id() .times(1) .with(eq(1)) .returning(move |_| Ok(test_metadata_clone.clone())); - let result = mock_metadata_repo.get_by_id(1).await; + let result = mock_metadata_repo.get_metadata_by_id(1).await; assert!(result.is_ok()); diff --git a/src/db/mod.rs b/src/db/mod.rs index 8c99117..3717fee 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,10 +1,10 @@ +mod connection; mod contact_repo; mod metadata_repo; #[cfg(test)] mod tests; -pub use contact_repo::Connection as ContactConnection; -pub use contact_repo::ContactRepo; +pub use connection::Connection; -pub use metadata_repo::Connection as MetadataConnection; +pub use contact_repo::ContactRepo; pub use metadata_repo::MetadataRepo; diff --git a/src/db/tests.rs b/src/db/tests.rs index 878eb0a..24120bb 100644 --- a/src/db/tests.rs +++ b/src/db/tests.rs @@ -1,7 +1,7 @@ use sqlx::{sqlite::SqlitePoolOptions, SqlitePool}; use crate::{ - db::{ContactConnection, ContactRepo, MetadataConnection, MetadataRepo}, + db::{Connection, ContactRepo, MetadataRepo}, models::{Contact, Metadata}, }; @@ -50,20 +50,19 @@ async fn setup_test_db() -> SqlitePool { async fn test_create_contact_get_metadata() { let pool = setup_test_db().await; - let contact_repo = ContactConnection::new(pool.clone()); - let metadata_repo = MetadataConnection::new(pool.clone()); + let data_repo = Connection::new(pool); let example_contact = Contact::new("Lewis", "Carroll", "lewis@wonderland.com", "777-777-7777").unwrap(); - let result_contact_id = contact_repo.create(example_contact).await; + let result_contact_id = data_repo.create_contact(example_contact).await; let contact_id = result_contact_id.unwrap(); assert_eq!(contact_id, 1); let default_metadata = Metadata::default(); - let result_expected_metadata = metadata_repo.get_by_id(contact_id).await; + let result_expected_metadata = data_repo.get_metadata_by_id(contact_id).await; let expected_metadata = result_expected_metadata.unwrap(); assert_eq!(default_metadata, expected_metadata); diff --git a/src/main.rs b/src/main.rs index 02d505d..a482a02 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ mod utils; use clap::Parser; use commander::{Cli, Commands}; -use db::{ContactRepo, ContactConnection}; +use db::{Connection, ContactRepo}; use models::{Contact, ContactBuilder}; use sqlx::SqlitePool; @@ -18,7 +18,7 @@ async fn main() -> anyhow::Result<()> { let pool = SqlitePool::connect(&env::var("DATABASE_URL")?).await?; - let contact_repo = ContactConnection::new(pool); + let data_repo = Connection::new(pool); let cli = Cli::parse(); @@ -35,25 +35,32 @@ async fn main() -> anyhow::Result<()> { let contact = contact.unwrap(); - let id = contact_repo.create(contact).await?; + let id = data_repo.create_contact(contact).await?; println!("Successfully saved contact {id}"); } Commands::Edit(value) => { - let contact = ContactBuilder::new(value.id, value.first_name.clone(), value.last_name.clone(), value.display_name.clone(), value.email.clone(), value.phone_number.clone()).unwrap(); - - - let _ = contact_repo.update(contact).await; + let contact = ContactBuilder::new( + value.id, + value.first_name.clone(), + value.last_name.clone(), + value.display_name.clone(), + value.email.clone(), + value.phone_number.clone(), + ) + .unwrap(); + + let _ = data_repo.update_contact(contact).await; } Commands::Show => { - let contacts = contact_repo.get_all().await?; + let contacts = data_repo.get_all_contacts().await?; println!("{contacts:?}"); } Commands::Get(value) => { let id = value.id; - let contact = contact_repo.get_by_id(id).await?; + let contact = data_repo.get_contact_by_id(id).await?; println!("{contact:?}"); }