Skip to content

Commit

Permalink
refactor: improve contact repo method names
Browse files Browse the repository at this point in the history
The way we call the repo means we can have very simple method names. We'll see if it sticks
  • Loading branch information
jasonribble committed Jul 14, 2024
1 parent 031463b commit 6b2568b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ 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 save(&self, contact: models::Contact) -> anyhow::Result<i64>;
async fn get_all(&self) -> anyhow::Result<Vec<models::IndexedContact>>;
async fn update_contact(&self, update: models::ContactBuilder) -> anyhow::Result<()>;
async fn update(&self, update: models::ContactBuilder) -> anyhow::Result<()>;
async fn get_by_id(&self, id: i64) -> anyhow::Result<models::IndexedContact>;
}

Expand All @@ -27,7 +27,7 @@ impl PostgresContactRepo {

#[async_trait]
impl ContactRepo for PostgresContactRepo {
async fn save_contact(&self, contact: models::Contact) -> anyhow::Result<i64> {
async fn save(&self, contact: models::Contact) -> anyhow::Result<i64> {
let query = "INSERT INTO contacts
(first_name, last_name, display_name, email, phone_number)
VALUES ($1, $2, $3, $4, $5)
Expand Down Expand Up @@ -59,7 +59,7 @@ impl ContactRepo for PostgresContactRepo {
Ok(contacts_with_id)
}

async fn update_contact(&self, contact: models::ContactBuilder) -> anyhow::Result<()> {
async fn update(&self, contact: models::ContactBuilder) -> anyhow::Result<()> {
sqlx::query!(
r#"
UPDATE contacts
Expand Down Expand Up @@ -115,7 +115,7 @@ mod tests {
.with(eq(test_contact.clone()))
.returning(|_| Ok(1));

let result = mock_contact_repo.save_contact(test_contact).await;
let result = mock_contact_repo.save(test_contact).await;

let result = result.unwrap();

Expand Down Expand Up @@ -153,7 +153,7 @@ mod tests {

let edits = models::ContactBuilder::new(1).set_email("[email protected]");

let result = mock_contact_repo.update_contact(edits).await;
let result = mock_contact_repo.update(edits).await;

assert!(result.is_ok());
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn main() -> anyhow::Result<()> {

let contact = parse_arguments()?;

let id = contact_repo.save_contact(contact).await?;
let id = contact_repo.save(contact).await?;

println!("{id}");

Expand All @@ -40,7 +40,7 @@ async fn main() -> anyhow::Result<()> {
.build()
.unwrap();

let _ = contact_repo.update_contact(edits).await;
let _ = contact_repo.update(edits).await;

let my_first_contact = contact_repo.get_by_id(1).await.unwrap();

Expand Down

0 comments on commit 6b2568b

Please sign in to comment.