Add vote_exists and additional delete/get vote count handler
This commit is contained in:
parent
f81b29c5e9
commit
c77e58f21b
4 changed files with 90 additions and 10 deletions
|
|
@ -4,6 +4,14 @@ use uuid::Uuid;
|
|||
|
||||
use crate::votes::model::BestSlugs;
|
||||
|
||||
pub async fn vote_exists(slug: &str, voter_id: &Uuid, db: &PgPool) -> Result<bool> {
|
||||
let count: i64 = query_scalar("select count(*) from votes where slug=$1 and voter_id=$2")
|
||||
.bind(slug)
|
||||
.bind(voter_id)
|
||||
.fetch_one(db)
|
||||
.await?;
|
||||
Ok(count > 0)
|
||||
}
|
||||
pub async fn insert_new_vote(slug: &str, voter_id: &Uuid, db: &PgPool) -> Result<()> {
|
||||
query(
|
||||
r#"insert into votes (slug, voter_id)
|
||||
|
|
@ -61,7 +69,7 @@ mod postgres_tests {
|
|||
use crate::{
|
||||
test_helpers::db::test_pool,
|
||||
votes::repository::{
|
||||
delete_vote, get_top_n_slugs, get_vote_count_for_slug, insert_new_vote,
|
||||
delete_vote, get_top_n_slugs, get_vote_count_for_slug, insert_new_vote, vote_exists,
|
||||
},
|
||||
};
|
||||
|
||||
|
|
@ -230,4 +238,35 @@ mod postgres_tests {
|
|||
|
||||
cleanup(&db, &votes).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
pub async fn vote_exists_test() {
|
||||
let db = test_pool().await;
|
||||
let votes = vec![
|
||||
(
|
||||
"vote_exists_test_blog_post1".to_string(),
|
||||
Uuid::from_u128(0x1),
|
||||
),
|
||||
(
|
||||
"vote_exists_test_blog_post2".to_string(),
|
||||
Uuid::from_u128(0x2),
|
||||
),
|
||||
(
|
||||
"vote_exists_test_blog_post3".to_string(),
|
||||
Uuid::from_u128(0x3),
|
||||
),
|
||||
];
|
||||
cleanup(&db, &votes).await;
|
||||
|
||||
insert_new_vote(&votes[0].0, &votes[0].1, &db)
|
||||
.await
|
||||
.unwrap();
|
||||
insert_new_vote(&votes[1].0, &votes[1].1, &db)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(vote_exists(&votes[0].0, &votes[0].1, &db).await.unwrap());
|
||||
assert!(vote_exists(&votes[1].0, &votes[1].1, &db).await.unwrap());
|
||||
assert!(!vote_exists(&votes[2].0, &votes[2].1, &db).await.unwrap());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue