-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ormlite to simplify sqlx queries
- Loading branch information
1 parent
1a2f6b0
commit 7003202
Showing
11 changed files
with
835 additions
and
538 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
2 changes: 1 addition & 1 deletion
2
crates/libs/core/migrations/20240307175731_create_tournament_table.down.sql
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 +1 @@ | ||
DROP DATABASE IF EXISTS tournaments | ||
DROP DATABASE IF EXISTS tournament |
2 changes: 1 addition & 1 deletion
2
crates/libs/core/migrations/20240307175731_create_tournament_table.up.sql
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,59 +1,27 @@ | ||
mod acronym; | ||
mod name; | ||
|
||
use anyhow::Context; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::DbPool; | ||
use acronym::TournamentAcronym; | ||
use name::TournamentName; | ||
|
||
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
use ormlite::model::*; | ||
|
||
#[derive(Model, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
pub struct Tournament { | ||
pub id: i32, | ||
pub name: String, | ||
pub acronym: String, | ||
#[ormlite(default)] | ||
pub created_at: chrono::DateTime<chrono::Utc>, | ||
#[ormlite(default)] | ||
pub updated_at: chrono::DateTime<chrono::Utc>, | ||
} | ||
|
||
#[derive(Debug, Deserialize, Clone)] | ||
pub struct NewTournament { | ||
#[derive(Insert, Debug, Deserialize, Clone)] | ||
#[ormlite(returns = "Tournament")] | ||
pub struct InsertTournament { | ||
pub name: TournamentName, | ||
pub acronym: TournamentAcronym, | ||
} | ||
|
||
impl Tournament { | ||
pub async fn get_all(db_pool: &DbPool) -> Result<Vec<Tournament>, anyhow::Error> { | ||
sqlx::query_as!(Tournament, "SELECT * FROM tournaments") | ||
.fetch_all(db_pool) | ||
.await | ||
.context("Could not retrieve tournaments from database") | ||
} | ||
|
||
pub async fn get_by_id(db_pool: &DbPool, entry_id: i32) -> Result<Tournament, anyhow::Error> { | ||
sqlx::query_as!( | ||
Tournament, | ||
"SELECT * FROM tournaments WHERE id = $1", | ||
entry_id | ||
) | ||
.fetch_one(db_pool) | ||
.await | ||
.context("Could not retrieve tournament by id from database") | ||
} | ||
} | ||
|
||
impl NewTournament { | ||
pub async fn insert(&self, db_pool: &DbPool) -> Result<Tournament, anyhow::Error> { | ||
let tournament = sqlx::query_as!( | ||
Tournament, | ||
"INSERT INTO tournaments (name, acronym) VALUES ($1, $2) RETURNING *", | ||
self.name.as_ref(), | ||
self.acronym.as_ref() | ||
) | ||
.fetch_one(db_pool) | ||
.await | ||
.context("Could not retrieve tournament by id from database")?; | ||
Ok(tournament) | ||
} | ||
} |
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