Skip to content

Commit

Permalink
feat: update contact in database
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonribble committed Jul 12, 2024
1 parent 6bedc51 commit 55aebb3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 28 deletions.
26 changes: 23 additions & 3 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
Expand Down Expand Up @@ -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;

Expand Down
10 changes: 5 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
40 changes: 20 additions & 20 deletions src/models/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()));
Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand Down Expand Up @@ -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());
}
}

0 comments on commit 55aebb3

Please sign in to comment.