-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor bot metadata handling and add votes table
Refactor the bot metadata handling to use async/await for improved asynchronous performance when fetching data. This change simplifies the code by removing blocking calls and enhances the readability of the `show` function in `metadata.rs`. Additionally, introduce a new `bot_votes` table in the database to track votes associated with bots. This includes creating a new `BotVote` model to manage vote data effectively. The changes are made to support future features related to bot voting.
- Loading branch information
Showing
9 changed files
with
119 additions
and
39 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
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,15 @@ | ||
// build.rs | ||
use std::env; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
fn main() { | ||
let version = env::var("CARGO_PKG_VERSION").unwrap(); | ||
let out_dir = env::var("OUT_DIR").unwrap(); | ||
let dest_path = Path::new(&out_dir).join("version.rs"); | ||
fs::write( | ||
dest_path, | ||
format!("pub const VERSION: &str = \"{}\";", version), | ||
) | ||
.unwrap(); | ||
} |
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
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
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
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
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
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,11 +1,46 @@ | ||
use crate::diesel::ExpressionMethods; | ||
use crate::models::util::diesel::Conn; | ||
use crate::models::Bot; | ||
use crate::schema::bot_votes; | ||
use chrono::NaiveDate; | ||
use diesel::RunQueryDsl; | ||
use diesel::{QueryResult, SelectableHelper}; | ||
|
||
#[derive(Queryable, Identifiable, Associations, Debug, Clone)] | ||
#[derive(Queryable, Identifiable, Associations, Selectable, Debug, Clone)] | ||
#[diesel(primary_key(bot_id, date), belongs_to(Bot))] | ||
pub struct BotVote { | ||
pub bot_id: String, | ||
pub date: NaiveDate, | ||
pub votes: i32, | ||
} | ||
|
||
#[derive(Insertable, Debug, Clone)] | ||
#[diesel( | ||
table_name = bot_votes, | ||
check_for_backend(diesel::pg::Pg), | ||
)] | ||
pub struct NewBotVote<'a> { | ||
pub bot_id: &'a str, | ||
} | ||
|
||
impl<'a> NewBotVote<'a> { | ||
pub fn new(bot_id: &'a str) -> NewBotVote { | ||
Self { bot_id } | ||
} | ||
|
||
pub fn create(&self, conn: &mut impl Conn) -> QueryResult<BotVote> { | ||
conn.transaction(|conn| { | ||
use crate::schema::bot_votes::dsl::*; | ||
|
||
let vote: BotVote = diesel::insert_into(bot_votes) | ||
.values(self) | ||
.on_conflict((bot_id, date)) | ||
.do_update() | ||
.set(votes.eq(votes + 1)) | ||
.returning(BotVote::as_returning()) | ||
.get_result(conn)?; | ||
|
||
Ok(vote) | ||
}) | ||
} | ||
} |
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