1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use crate::db::schema::votes;
use crate::db::DbConnection;
use crate::{
    db::{models::vote::Vote, schema::votes::dsl as vote_dsl, DbConnectionPool},
    v0::errors::HandleError,
};
use diesel::{ExpressionMethods, Insertable, QueryDsl, QueryResult, RunQueryDsl};

pub async fn query_votes_by_caster_and_voteplan_id(
    caster: String,
    voteplan_id: String,
    pool: &DbConnectionPool,
) -> Result<Vec<Vote>, HandleError> {
    let db_conn = pool.get().map_err(HandleError::DatabaseError)?;
    tokio::task::spawn_blocking(move || {
        diesel::QueryDsl::filter(vote_dsl::votes, vote_dsl::caster.eq(&caster))
            .filter(vote_dsl::voteplan_id.eq(&voteplan_id))
            .load::<Vote>(&db_conn)
            .map_err(|_e| HandleError::NotFound("Error loading vote".to_string()))
    })
    .await
    .map_err(|_e| HandleError::InternalError("Error executing request".to_string()))?
}

pub fn batch_insert_votes_data(
    votes: &[<Vote as Insertable<votes::table>>::Values],
    db_conn: &DbConnection,
) -> QueryResult<usize> {
    diesel::insert_into(votes::table)
        .values(votes)
        .execute(db_conn)
}