From 9688b6197633dc6acc6f0e424a466b9326d36a69 Mon Sep 17 00:00:00 2001 From: Jason Ribble Date: Wed, 10 Jul 2024 09:05:33 -0700 Subject: [PATCH] feat: update contact takes an update parameter - models::UpdateContact is a parameter for update_contact - next we make sure the database actually get updated... --- src/db.rs | 12 ++++++++---- src/main.rs | 10 ++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/db.rs b/src/db.rs index 572f4b7..c4c68b9 100644 --- a/src/db.rs +++ b/src/db.rs @@ -9,7 +9,7 @@ use sqlx::postgres::PgPool; pub trait ContactRepo { async fn save_contact(&self, contact: models::Contact) -> anyhow::Result; async fn get_all(&self) -> anyhow::Result>; - async fn update_contact(&self) -> anyhow::Result<()>; + async fn update_contact(&self, update: models::UpdateContact) -> anyhow::Result<()>; } pub struct PostgresContactRepo { @@ -58,7 +58,8 @@ impl ContactRepo for PostgresContactRepo { Ok(contacts_with_id) } - async fn update_contact(&self) -> anyhow::Result<()> { + async fn update_contact(&self, update: models::UpdateContact) -> anyhow::Result<()> { + println!("{update:?}"); Ok(()) } } @@ -112,12 +113,15 @@ mod tests { async fn test_update_contact() { let mut mock_contact_repo = MockContactRepo::new(); + let edits = models::UpdateContact::new(1).email("new_email@example.com"); + mock_contact_repo .expect_update_contact() .times(1) - .return_once(move || Ok(())); + .with(eq(edits.clone())) + .return_once(|_| Ok(())); - let result = mock_contact_repo.update_contact().await; + let result = mock_contact_repo.update_contact(edits).await; assert!(result.is_ok()); } diff --git a/src/main.rs b/src/main.rs index 7957c30..9452462 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,16 @@ async fn main() -> anyhow::Result<()> { println!("{most_recent_contact:?}"); + let edits = models::UpdateContact::new(id) + .first_name("New Name") + .last_name("Yep") + .email("completely@new.com") + .phone_number("1233211233") + .display_name("Nickname") + .build(); + + let _ = contact_repo.update_contact(edits).await; + Ok(()) }