pub fn vote(vote: PostVote, conn: &PgConnection) -> BackendResult<()> {
        use crate::schema::{
            post_downvotes,
            post_upvotes,
        };
        use diesel::{
            dsl::exists,
            select,
        };

        let v: &Vote = match vote {
            PostVote::Up(ref vote) => vote,
            PostVote::Down(ref vote) => vote,
        };

        let upvote_exists: bool = select(exists(
            post_upvotes::table
                .filter(post_upvotes::user_uuid.eq(v.user_uuid.0))
                .filter(post_upvotes::post_uuid.eq(v.post_uuid.0)),
        ))
        .get_result(conn)
        .map_err(handle_err::<Post>)?;

        let downvote_exists: bool = select(exists(
            post_downvotes::table
                .filter(post_downvotes::user_uuid.eq(v.user_uuid.0))
                .filter(post_downvotes::post_uuid.eq(v.post_uuid.0)),
        ))
        .get_result(conn)
        .map_err(handle_err::<Post>)?;

        match vote {
            PostVote::Up(vote) => {
                if downvote_exists {
                    Post::remove_downvote(vote.user_uuid, vote.post_uuid, conn)?
                }
                if !upvote_exists {
                    // Insert the new upvote
                    let upvote: NewUpvote = vote.into();
                    return diesel::insert_into(post_upvotes::table)
                        .values(upvote)
                        .execute(conn)
                        .map_err(handle_err::<Post>)
                        .map(|_| ());
                } else {
                    return Err(Error::BadRequest);
                }
            }
            PostVote::Down(vote) => {
                if upvote_exists {
                    Post::remove_upvote(vote.user_uuid, vote.post_uuid, conn)?
                }
                if !downvote_exists {
                    // Add the new downvote
                    let downvote: NewDownvote = vote.into();
                    return diesel::insert_into(post_downvotes::table)
                        .values(downvote)
                        .execute(conn)
                        .map_err(handle_err::<Post>)
                        .map(|_| ());
                } else {
                    return Err(Error::BadRequest);
                }
            }
        };
    }
/// Add a vote record to a post.
Add a vote record to a post.
Add a vote record to a post .
pub fn vote(vote: PostVote, conn: &PgConnection) -> BackendResult<()> {
        use crate::schema::{
            post_downvotes,
            post_upvotes,
        };
        use diesel::{
            dsl::exists,
            select,
        };
        let v: &Vote = match vote {
            PostVote::Up(ref vote) => vote,
            PostVote::Down(ref vote) => vote,
        };
        let upvote_exists: bool = select(exists(
            post_upvotes::table
                .filter(post_upvotes::user_uuid.eq(v.user_uuid.0))
                .filter(post_upvotes::post_uuid.eq(v.post_uuid.0)),
        ))
        .get_result(conn)
        .map_err(handle_err::<Post>)?;
        let downvote_exists: bool = select(exists(
            post_downvotes::table
                .filter(post_downvotes::user_uuid.eq(v.user_uuid.0))
                .filter(post_downvotes::post_uuid.eq(v.post_uuid.0)),
        ))
        .get_result(conn)
        .map_err(handle_err::<Post>)?;
        match vote {
            PostVote::Up(vote) => {
                if downvote_exists {
                    Post::remove_downvote(vote.user_uuid, vote.post_uuid, conn)?
                }
                if !upvote_exists {
                    // Insert the new upvote
                    let upvote: NewUpvote = vote.into();
                    return diesel::insert_into(post_upvotes::table)
                        .values(upvote)
                        .execute(conn)
                        .map_err(handle_err::<Post>)
                        .map(|_| ());
                } else {
                    return Err(Error::BadRequest);
                }
            }
            PostVote::Down(vote) => {
                if upvote_exists {
                    Post::remove_upvote(vote.user_uuid, vote.post_uuid, conn)?
                }
                if !downvote_exists {
                    // Add the new downvote
                    let downvote: NewDownvote = vote.into();
                    return diesel::insert_into(post_downvotes::table)
                        .values(downvote)
                        .execute(conn)
                        .map_err(handle_err::<Post>)
                        .map(|_| ());
                } else {
                    return Err(Error::BadRequest);
                }
            }
        };
    }
pub fn vote ( vote : PostVote , conn : & PgConnection ) -> BackendResult < ( ) > { use crate :: schema :: { post_downvotes , post_upvotes , } ; use diesel :: { dsl :: exists , select , } ; let v : & Vote = match vote { PostVote :: Up ( ref vote ) => vote , PostVote :: Down ( ref vote ) => vote , } ; let upvote_exists : bool = select ( exists ( post_upvotes :: table . filter ( post_upvotes :: user_uuid . eq ( v . user_uuid . 0 ) ) . filter ( post_upvotes :: post_uuid . eq ( v . post_uuid . 0 ) ) , ) ) . get_result ( conn ) . map_err ( handle_err :: < Post > ) ? ; let downvote_exists : bool = select ( exists ( post_downvotes :: table . filter ( post_downvotes :: user_uuid . eq ( v . user_uuid . 0 ) ) . filter ( post_downvotes :: post_uuid . eq ( v . post_uuid . 0 ) ) , ) ) . get_result ( conn ) . map_err ( handle_err :: < Post > ) ? ; match vote { PostVote :: Up ( vote ) => { if downvote_exists { Post :: remove_downvote ( vote . user_uuid , vote . post_uuid , conn ) ? } if ! upvote_exists { // Insert the new upvote let upvote : NewUpvote = vote . into ( ) ; return diesel :: insert_into ( post_upvotes :: table ) . values ( upvote ) . execute ( conn ) . map_err ( handle_err :: < Post > ) . map ( | _ | ( ) ) ; } else { return Err ( Error :: BadRequest ) ; } } PostVote :: Down ( vote ) => { if upvote_exists { Post :: remove_upvote ( vote . user_uuid , vote . post_uuid , conn ) ? } if ! downvote_exists { // Add the new downvote let downvote : NewDownvote = vote . into ( ) ; return diesel :: insert_into ( post_downvotes :: table ) . values ( downvote ) . execute ( conn ) . map_err ( handle_err :: < Post > ) . map ( | _ | ( ) ) ; } else { return Err ( Error :: BadRequest ) ; } } } ; }
Add a vote record to a post.
Add a vote record to a post .
vote
PostVote
conn
&PgConnection
vote
PostVote
conn
&PgConnection