From e15c30fe06e16a55241576905726d5a9cdae17a5 Mon Sep 17 00:00:00 2001 From: Alex Selimov Date: Thu, 19 Mar 2026 12:55:57 -0400 Subject: [PATCH] Rework repositories to simplify the structure --- src/votes/repository.rs | 93 +++++++++++------------------------------ 1 file changed, 24 insertions(+), 69 deletions(-) diff --git a/src/votes/repository.rs b/src/votes/repository.rs index e890721..8bf5e78 100644 --- a/src/votes/repository.rs +++ b/src/votes/repository.rs @@ -4,12 +4,10 @@ use uuid::Uuid; use crate::votes::model::BestSlugs; -use super::model::Vote; - -pub async fn insert_new_vote(vote: &Vote, db: &PgPool) -> Result<()> { +pub async fn insert_new_vote(slug: &str, voter_id: &Uuid, db: &PgPool) -> Result<()> { query("insert into votes (slug, voter_id) values ($1, $2)") - .bind(vote.slug.clone()) - .bind(vote.voter_id) + .bind(slug) + .bind(voter_id) .execute(db) .await?; Ok(()) @@ -58,66 +56,27 @@ mod postgres_tests { use crate::{ test_helpers::db::test_pool, - votes::{ - model::Vote, - repository::{delete_vote, get_top_n_slugs, get_vote_count_for_slug, insert_new_vote}, - }, + votes::repository::{delete_vote, get_top_n_slugs, get_vote_count_for_slug, insert_new_vote}, }; - async fn cleanup(db: &PgPool) { - for vote in test_votes() { - delete_vote(&vote.slug, &vote.voter_id, db).await.unwrap() - } + fn test_votes() -> [(&'static str, Uuid); 9] { + [ + ("blog_post1", Uuid::from_u128(0x1)), + ("blog_post1", Uuid::from_u128(0x2)), + ("blog_post2", Uuid::from_u128(0x3)), + ("blog_post2", Uuid::from_u128(0x4)), + ("blog_post3", Uuid::from_u128(0x5)), + ("blog_post3", Uuid::from_u128(0x6)), + ("blog_post1", Uuid::from_u128(0x7)), + ("blog_post1", Uuid::from_u128(0x8)), + ("blog_post3", Uuid::from_u128(0x9)), + ] } - fn test_votes() -> [Vote; 9] { - [ - Vote { - slug: "blog_post1".into(), - voter_id: Uuid::from_u128(0x1), - created_at: chrono::Utc::now(), - }, - Vote { - slug: "blog_post1".into(), - voter_id: Uuid::from_u128(0x2), - created_at: chrono::Utc::now(), - }, - Vote { - slug: "blog_post2".into(), - voter_id: Uuid::from_u128(0x3), - created_at: chrono::Utc::now(), - }, - Vote { - slug: "blog_post2".into(), - voter_id: Uuid::from_u128(0x4), - created_at: chrono::Utc::now(), - }, - Vote { - slug: "blog_post3".into(), - voter_id: Uuid::from_u128(0x5), - created_at: chrono::Utc::now(), - }, - Vote { - slug: "blog_post3".into(), - voter_id: Uuid::from_u128(0x6), - created_at: chrono::Utc::now(), - }, - Vote { - slug: "blog_post1".into(), - voter_id: Uuid::from_u128(0x7), - created_at: chrono::Utc::now(), - }, - Vote { - slug: "blog_post1".into(), - voter_id: Uuid::from_u128(0x8), - created_at: chrono::Utc::now(), - }, - Vote { - slug: "blog_post3".into(), - voter_id: Uuid::from_u128(0x9), - created_at: chrono::Utc::now(), - }, - ] + async fn cleanup(db: &PgPool) { + for (slug, voter_id) in test_votes() { + delete_vote(slug, &voter_id, db).await.unwrap() + } } #[tokio::test] @@ -127,8 +86,8 @@ mod postgres_tests { cleanup(&db).await; let votes = test_votes(); - for vote in votes.iter() { - insert_new_vote(vote, &db) + for (slug, voter_id) in votes.iter() { + insert_new_vote(slug, voter_id, &db) .await .expect("Insertions to db failed"); } @@ -141,12 +100,8 @@ mod postgres_tests { assert_eq!(top_2[0].slug, "blog_post1"); assert_eq!(top_2[1].slug, "blog_post3"); - delete_vote(&votes[4].slug, &votes[4].voter_id, &db) - .await - .unwrap(); - delete_vote(&votes[5].slug, &votes[5].voter_id, &db) - .await - .unwrap(); + delete_vote(votes[4].0, &votes[4].1, &db).await.unwrap(); + delete_vote(votes[5].0, &votes[5].1, &db).await.unwrap(); assert_eq!(get_vote_count_for_slug("blog_post1", &db).await.unwrap(), 4); assert_eq!(get_vote_count_for_slug("blog_post2", &db).await.unwrap(), 2);