Skip to content

Commit

Permalink
style: fix linting error for ContactWithId
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jasonribble committed Jul 8, 2024
1 parent 63f348e commit ac6533c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
19 changes: 9 additions & 10 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use sqlx::postgres::PgPool;
#[async_trait]
pub trait ContactRepo {
async fn save_contact(&self, contact: models::Contact) -> anyhow::Result<i64>;
async fn get_all(&self) -> anyhow::Result<Vec<models::ContactWithId>>;
async fn get_all(&self) -> anyhow::Result<Vec<models::IndexedContact>>;
}

pub struct PostgresContactRepo {
Expand Down Expand Up @@ -43,15 +43,14 @@ impl ContactRepo for PostgresContactRepo {
Ok(id)
}

async fn get_all(&self) -> anyhow::Result<Vec<models::ContactWithId>> {
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<Vec<models::IndexedContact>> {
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<models::ContactWithId> =
sqlx::query_as::<_, models::ContactWithId>(get_contacts_query)
let contacts_with_id: Vec<models::IndexedContact> =
sqlx::query_as::<_, models::IndexedContact>(get_contacts_query)
.fetch_all(&*self.pg_pool)
.await?;

Expand Down Expand Up @@ -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", "[email protected]", "1234567890")
.unwrap(),
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/models/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod contact;

pub use contact::{Contact, ContactWithId};
pub use contact::Contact;
pub use contact::Indexed as IndexedContact;

0 comments on commit ac6533c

Please sign in to comment.