-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add vote model + display votes in table
- Loading branch information
1 parent
6359cfb
commit 7dea228
Showing
3 changed files
with
123 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod logs; | ||
pub mod players; | ||
pub mod thread; | ||
pub mod votes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use crate::AppState; | ||
use actix_web::web::Data; | ||
use serde::{Deserialize, Serialize}; | ||
use sqlx::FromRow; | ||
use sqlx::{self, postgres::PgQueryResult}; | ||
|
||
#[derive(Serialize, Deserialize, FromRow, Debug)] | ||
pub struct Vote { | ||
pub id: i32, | ||
pub author: String, | ||
pub target: String, | ||
pub post_number: i32, | ||
pub target_correction: Option<String>, | ||
|
||
// FKs | ||
pub thread_id: String, | ||
pub player_id: i32, | ||
} | ||
|
||
pub enum VoteQuery { | ||
Thread(String), | ||
Player(i32), | ||
} | ||
|
||
pub async fn get_vote(state: &Data<AppState>, id: i32) -> Option<Vote> { | ||
let db = &state.db; | ||
match sqlx::query_as!( | ||
Vote, | ||
r#"SELECT id, author, target, post_number, target_correction, thread_id, player_id FROM votes WHERE id = $1"#, | ||
id | ||
) | ||
.fetch_one(db) | ||
.await | ||
{ | ||
Ok(vote) => Some(vote), | ||
_ => None, | ||
} | ||
} | ||
|
||
pub async fn get_votes(state: &Data<AppState>, query: VoteQuery) -> Option<Vec<Vote>> { | ||
let db = &state.db; | ||
match query { | ||
VoteQuery::Thread(thread_id) => match sqlx::query_as!( | ||
Vote, | ||
r#"SELECT id, author, target, post_number, target_correction, thread_id, player_id FROM votes WHERE thread_id = $1"#, | ||
thread_id | ||
) | ||
.fetch_all(db) | ||
.await | ||
{ | ||
Ok(votes) => Some(votes), | ||
_ => None, | ||
}, | ||
VoteQuery::Player(player_id) => match sqlx::query_as!( | ||
Vote, | ||
r#"SELECT id, author, target, post_number, target_correction, thread_id, player_id FROM votes WHERE player_id = $1"#, | ||
player_id | ||
) | ||
.fetch_all(db) | ||
.await | ||
{ | ||
Ok(votes) => Some(votes), | ||
_ => None, | ||
} | ||
} | ||
} | ||
|
||
pub async fn create_vote(state: &Data<AppState>, vote: Vote) -> Option<PgQueryResult> { | ||
let db = &state.db; | ||
match sqlx::query!( | ||
"INSERT INTO votes (author, target, post_number, target_correction, thread_id, player_id) VALUES ($1, $2, $3, $4, $5, $6)", | ||
vote.author, | ||
vote.target, | ||
vote.post_number, | ||
vote.target_correction, | ||
vote.thread_id, | ||
vote.player_id | ||
) | ||
.execute(db) | ||
.await | ||
{ | ||
Ok(query) => Some(query), | ||
_ => None, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters