-
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.
- Loading branch information
1 parent
6bedc51
commit 55aebb3
Showing
3 changed files
with
48 additions
and
28 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 |
---|---|---|
|
@@ -58,8 +58,28 @@ impl ContactRepo for PostgresContactRepo { | |
Ok(contacts_with_id) | ||
} | ||
|
||
async fn update_contact(&self, update: models::ContactBuilder) -> anyhow::Result<()> { | ||
println!("{update:?}"); | ||
async fn update_contact(&self, contact: models::ContactBuilder) -> anyhow::Result<()> { | ||
sqlx::query!( | ||
r#" | ||
UPDATE contacts | ||
SET | ||
first_name = COALESCE($1, first_name), | ||
last_name = COALESCE($2, last_name), | ||
display_name = COALESCE($3, display_name), | ||
email = COALESCE($4, email), | ||
phone_number = COALESCE($5, phone_number) | ||
WHERE id = $6 | ||
"#, | ||
contact.update.first_name, | ||
contact.update.last_name, | ||
contact.update.display_name, | ||
contact.update.email, | ||
contact.update.phone_number, | ||
contact.id | ||
) | ||
.fetch_all(&*self.pg_pool) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
@@ -118,7 +138,7 @@ mod tests { | |
.times(1) | ||
.return_once(|_| Ok(())); | ||
|
||
let edits = models::ContactBuilder::new(1).email("[email protected]"); | ||
let edits = models::ContactBuilder::new(1).set_email("[email protected]"); | ||
|
||
let result = mock_contact_repo.update_contact(edits).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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,11 +32,11 @@ async fn main() -> anyhow::Result<()> { | |
println!("{most_recent_contact:?}"); | ||
|
||
let edits = models::ContactBuilder::new(id) | ||
.first_name("New Name") | ||
.last_name("Yep") | ||
.email("[email protected]") | ||
.phone_number("1233211233") | ||
.display_name("Nickname") | ||
.set_first_name("New Name") | ||
.set_last_name("Yep") | ||
.set_email("[email protected]") | ||
.set_phone_number("1233211233") | ||
.set_display_name("Nickname") | ||
.build() | ||
.unwrap(); | ||
|
||
|
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 |
---|---|---|
|
@@ -17,18 +17,18 @@ pub struct Indexed { | |
} | ||
|
||
#[derive(Debug, PartialEq, Eq, Clone)] | ||
struct Update { | ||
first_name: Option<String>, | ||
last_name: Option<String>, | ||
display_name: Option<String>, | ||
email: Option<String>, | ||
phone_number: Option<String>, | ||
pub struct Update { | ||
pub first_name: Option<String>, | ||
pub last_name: Option<String>, | ||
pub display_name: Option<String>, | ||
pub email: Option<String>, | ||
pub phone_number: Option<String>, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Builder { | ||
id: i64, | ||
update: Update, | ||
pub id: i64, | ||
pub update: Update, | ||
errors: Vec<AppError>, | ||
} | ||
impl Builder { | ||
|
@@ -54,30 +54,30 @@ impl Builder { | |
&& self.update.phone_number.is_none() | ||
} | ||
|
||
pub fn first_name(mut self, first_name: &str) -> Self { | ||
pub fn set_first_name(mut self, first_name: &str) -> Self { | ||
self.update.first_name = Some(first_name.to_string()); | ||
self | ||
} | ||
|
||
pub fn last_name(mut self, last_name: &str) -> Self { | ||
pub fn set_last_name(mut self, last_name: &str) -> Self { | ||
self.update.last_name = Some(last_name.to_string()); | ||
self | ||
} | ||
|
||
pub fn email(mut self, email: &str) -> Self { | ||
pub fn set_email(mut self, email: &str) -> Self { | ||
if utils::is_not_valid_email(email) { | ||
self.errors.push(AppError::InvalidEmail(email.to_string())); | ||
} | ||
self.update.email = Some(email.to_string()); | ||
self | ||
} | ||
|
||
pub fn display_name(mut self, display_name: &str) -> Self { | ||
pub fn set_display_name(mut self, display_name: &str) -> Self { | ||
self.update.display_name = Some(display_name.to_string()); | ||
self | ||
} | ||
|
||
pub fn phone_number(mut self, phone_number: &str) -> Self { | ||
pub fn set_phone_number(mut self, phone_number: &str) -> Self { | ||
if utils::is_not_valid_phone_number(phone_number) { | ||
self.errors | ||
.push(AppError::InvalidPhoneNumber(phone_number.to_string())); | ||
|
@@ -142,8 +142,8 @@ mod tests { | |
#[test] | ||
fn test_contact_update_builder() { | ||
let edits = Builder::new(1) | ||
.display_name("Nickname") | ||
.phone_number("123-233-1221") | ||
.set_display_name("Nickname") | ||
.set_phone_number("123-233-1221") | ||
.build() | ||
.unwrap(); | ||
|
||
|
@@ -158,9 +158,9 @@ mod tests { | |
#[test] | ||
fn test_contact_update_builder_2() { | ||
let edits = Builder::new(2) | ||
.first_name("Mary") | ||
.last_name("Smith") | ||
.email("[email protected]") | ||
.set_first_name("Mary") | ||
.set_last_name("Smith") | ||
.set_email("[email protected]") | ||
.build() | ||
.unwrap(); | ||
|
||
|
@@ -193,13 +193,13 @@ mod tests { | |
|
||
#[test] | ||
fn test_invalid_email_builder() { | ||
let result = Builder::new(1).email("invalid@example").build(); | ||
let result = Builder::new(1).set_email("invalid@example").build(); | ||
assert!(result.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_invalid_builder_phone_number() { | ||
let result = Builder::new(1).phone_number("invalid number").build(); | ||
let result = Builder::new(1).set_phone_number("invalid number").build(); | ||
assert!(result.is_err()); | ||
} | ||
} |