Skip to content

Commit

Permalink
feat: get all contacts
Browse files Browse the repository at this point in the history
- create ContactWithId to map Vec<Record> into a returnable value
  • Loading branch information
jasonribble committed Jul 8, 2024
1 parent 1b32888 commit 7af4466
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ This Rust project requires the following:
Add a contact

```
cargo run -- John Test [email protected] 9738978633
cargo run -- John Test [email protected] 123-456-7890
```

## Cleanup
Expand Down
37 changes: 30 additions & 7 deletions src/db.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::sync::Arc;

use crate::models;
use crate::models::{self, ContactWithId};
use async_trait::async_trait;
use sqlx::postgres::PgPool;

#[cfg_attr(test, mockall::automock)]
#[async_trait]
pub trait ContactRepo {
async fn save_contact(&self, contact: models::Contact) -> anyhow::Result<i64>;
async fn get_all(&self) -> anyhow::Result<()>;
async fn get_all(&self) -> anyhow::Result<Vec<models::ContactWithId>>;
}

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

async fn get_all(&self) -> anyhow::Result<()> {
async fn get_all(&self) -> anyhow::Result<Vec<models::ContactWithId>> {
let records = sqlx::query!(
r#"
SELECT id, first_name, last_name, display_name, email, phone_number
Expand All @@ -54,9 +54,21 @@ impl ContactRepo for PostgresContactRepo {
.fetch_all(&*self.pg_pool)
.await?;

println!("{:?}", records);

Ok(())
let contacts_with_id: Vec<models::ContactWithId> = records
.into_iter()
.map(|record| ContactWithId {
id: record.id,
contact: models::Contact {
first_name: record.first_name,
last_name: record.last_name,
display_name: record.display_name,
email: record.email,
phone_number: record.phone_number,
},
})
.collect();

Ok(contacts_with_id)
}
}

Expand Down Expand Up @@ -95,10 +107,21 @@ mod tests {
async fn test_get_all_contacts() {
let mut mock_contact_repo = MockContactRepo::new();

let contacts = vec![models::ContactWithId {
id: 1,
contact: models::Contact {
first_name: "John".to_string(),
last_name: "Doe".to_string(),
display_name: "John Doe".to_string(),
email: "[email protected]".to_string(),
phone_number: "1234567890".to_string(),
},
}];

mock_contact_repo
.expect_get_all()
.times(1)
.return_once(move || Ok(()));
.return_once(move || Ok(contacts));

let result = mock_contact_repo.get_all().await;

Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ async fn main() -> anyhow::Result<()> {

println!("{id}");

contact_repo.get_all().await?;
let all_contact = contact_repo.get_all().await?;

println!("{all_contact:?}");

Ok(())
}
Expand Down
6 changes: 6 additions & 0 deletions src/models/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ pub struct Contact {
pub phone_number: String,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ContactWithId {
pub id: i64,
pub contact: Contact,
}

impl Contact {
pub fn new(
first_name: String,
Expand Down
2 changes: 1 addition & 1 deletion src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod contact;

pub use contact::Contact;
pub use contact::{Contact, ContactWithId};

0 comments on commit 7af4466

Please sign in to comment.