From ac6533c8f8101bbe2e522bb83fb8d3a65be537ed Mon Sep 17 00:00:00 2001 From: Jason Ribble Date: Mon, 8 Jul 2024 15:07:50 -0700 Subject: [PATCH] style: fix linting error for ContactWithId I think `Indexed` and `IndexContact` are better Though something I noticed is that when debugging the default display says `Indexed` isn't descriptive Later when logging becomes important i'll look into it --- src/db.rs | 19 +++++++++---------- src/main.rs | 4 +++- src/models/contact.rs | 2 +- src/models/mod.rs | 3 ++- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/db.rs b/src/db.rs index 07c0f0c..84d015f 100644 --- a/src/db.rs +++ b/src/db.rs @@ -8,7 +8,7 @@ use sqlx::postgres::PgPool; #[async_trait] pub trait ContactRepo { async fn save_contact(&self, contact: models::Contact) -> anyhow::Result; - async fn get_all(&self) -> anyhow::Result>; + async fn get_all(&self) -> anyhow::Result>; } pub struct PostgresContactRepo { @@ -43,15 +43,14 @@ impl ContactRepo for PostgresContactRepo { Ok(id) } - async fn get_all(&self) -> anyhow::Result> { - let get_contacts_query = r#" - SELECT id, first_name, last_name, display_name, email, phone_number - FROM contacts - ORDER BY id - "#; + async fn get_all(&self) -> anyhow::Result> { + let get_contacts_query = + "SELECT id, first_name, last_name, display_name, email, phone_number + FROM contacts + ORDER BY id"; - let contacts_with_id: Vec = - sqlx::query_as::<_, models::ContactWithId>(get_contacts_query) + let contacts_with_id: Vec = + sqlx::query_as::<_, models::IndexedContact>(get_contacts_query) .fetch_all(&*self.pg_pool) .await?; @@ -88,7 +87,7 @@ mod tests { async fn test_get_all_contacts() { let mut mock_contact_repo = MockContactRepo::new(); - let contacts = vec![models::ContactWithId { + let contacts = vec![models::IndexedContact { id: 1, contact: models::Contact::new("John", "Doe", "johndoe@example.com", "1234567890") .unwrap(), diff --git a/src/main.rs b/src/main.rs index d42b56a..7957c30 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,9 @@ async fn main() -> anyhow::Result<()> { let all_contact = contact_repo.get_all().await?; - println!("{all_contact:?}"); + let most_recent_contact = &all_contact[all_contact.len() - 1..]; + + println!("{most_recent_contact:?}"); Ok(()) } diff --git a/src/models/contact.rs b/src/models/contact.rs index d7ebce5..fbd9564 100644 --- a/src/models/contact.rs +++ b/src/models/contact.rs @@ -10,7 +10,7 @@ pub struct Contact { } #[derive(Debug, PartialEq, Eq, Clone, sqlx::FromRow)] -pub struct ContactWithId { +pub struct Indexed { pub id: i64, #[sqlx(flatten)] pub contact: Contact, diff --git a/src/models/mod.rs b/src/models/mod.rs index 389d042..7614ff7 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,3 +1,4 @@ mod contact; -pub use contact::{Contact, ContactWithId}; +pub use contact::Contact; +pub use contact::Indexed as IndexedContact;