-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- create ContactWithId to map Vec<Record> into a returnable value
- Loading branch information
1 parent
1b32888
commit 7af4466
Showing
5 changed files
with
41 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
@@ -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 | ||
|
@@ -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) | ||
} | ||
} | ||
|
||
|
@@ -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; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |