Skip to content

Commit

Permalink
fix: remove redundant get_all_by_user method
Browse files Browse the repository at this point in the history
  • Loading branch information
d-loose committed Sep 30, 2024
1 parent 95a7852 commit 1a3cfea
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 47 deletions.
8 changes: 4 additions & 4 deletions crates/ratings_new/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,17 @@ mod test {
vote.save_to_db(conn).await?;
}

let votes_client_1 = vote::Vote::get_all_by_client_hash_and_snap_id(
String::from(snap_id_1),
let votes_client_1 = vote::Vote::get_all_by_client_hash(
String::from(client_hash_1),
Some(String::from(snap_id_1)),
conn,
)
.await
.unwrap();

let votes_client_2 = vote::Vote::get_all_by_client_hash_and_snap_id(
String::from(snap_id_2),
let votes_client_2 = vote::Vote::get_all_by_client_hash(
String::from(client_hash_2),
Some(String::from(snap_id_2)),
conn,
)
.await
Expand Down
48 changes: 5 additions & 43 deletions crates/ratings_new/src/db/vote.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{ClientHash, DbError, Result};
use sqlx::{types::time::OffsetDateTime, FromRow, PgConnection};
use tracing::{debug, error};
use tracing::error;

/// A Vote, as submitted by a user
#[derive(Debug, Clone, FromRow, PartialEq, Eq)]
Expand All @@ -21,14 +21,13 @@ pub struct Vote {

/// Gets votes for a snap with the given ID from a given [`ClientHash`]
///
/// [`ClientHash`]: crate::features::user::entities::ClientHash
/// [`ClientHash`]: crate::db::ClientHash
impl Vote {
pub async fn get_all_by_client_hash_and_snap_id(
snap_id: String,
pub async fn get_all_by_client_hash(
client_hash: String,
snap_id_filter: Option<String>,
conn: &mut PgConnection,
) -> Result<Vec<Vote>> {
debug!("client_hash: '{}', snap_id: '{}'", &client_hash, &snap_id);
let votes = sqlx::query_as(
r#"
SELECT
Expand All @@ -46,46 +45,9 @@ impl Vote {
users.id = votes.user_id_fk
WHERE
users.client_hash = $1
AND
votes.snap_id = $2
"#,
)
.bind(client_hash)
.bind(snap_id)
.fetch_all(conn)
.await
.map_err(|error| {
error!("{error:?}");
DbError::FailedToGetUserVote
})?;

Ok(votes)
}

pub async fn get_all_by_user(
client_hash: String,
snap_id_filter: Option<String>,
conn: &mut PgConnection,
) -> Result<Vec<Vote>> {
let votes = sqlx::query_as(
r#"
SELECT
votes.id,
votes.created,
votes.snap_id,
votes.snap_revision,
votes.vote_up
FROM
users
INNER JOIN
votes
ON
users.id = votes.user_id_fk
WHERE
users.client_hash = $1
AND
($2 IS NULL OR votes.snap_id = $2);
"#,
"#,
)
.bind(client_hash)
.bind(snap_id_filter)
Expand Down

0 comments on commit 1a3cfea

Please sign in to comment.