Use sqlx result in db.rs
This commit is contained in:
parent
769512f357
commit
b443f28a50
37
src/db.rs
37
src/db.rs
|
@ -17,46 +17,41 @@ pub(crate) async fn create_pool() -> Result<SqlitePool, crate::errors::Error> {
|
||||||
|
|
||||||
#[rocket::async_trait]
|
#[rocket::async_trait]
|
||||||
pub(crate) trait Dao {
|
pub(crate) trait Dao {
|
||||||
async fn load_user_by_id(&self, id: i32) -> Result<Option<User>, Error>;
|
async fn load_user_by_id(&self, id: i32) -> sqlx::Result<Option<User>>;
|
||||||
|
|
||||||
async fn load_user(&self, for_username: &str) -> Result<Option<User>, Error>;
|
async fn load_user(&self, for_username: &str) -> sqlx::Result<Option<User>>;
|
||||||
|
|
||||||
async fn insert_user(&self, new_user: NewUser<'_>) -> Result<User, Error>;
|
async fn insert_user(&self, new_user: NewUser<'_>) -> sqlx::Result<User>;
|
||||||
|
|
||||||
async fn load_character_list(&self, for_user_id: i32) -> Result<Vec<StrippedCharacter>, Error>;
|
async fn load_character_list(&self, for_user_id: i32) -> sqlx::Result<Vec<StrippedCharacter>>;
|
||||||
|
|
||||||
async fn load_character(&self, character_id: i32) -> Result<Option<Character>, Error>;
|
async fn load_character(&self, character_id: i32) -> sqlx::Result<Option<Character>>;
|
||||||
|
|
||||||
async fn insert_character(&self, new_character: NewCharacter<'_>) -> Result<(), Error>;
|
async fn insert_character(&self, new_character: NewCharacter<'_>) -> sqlx::Result<()>;
|
||||||
|
|
||||||
async fn update_character_sheet<'a>(&self, character: &'a Character) -> Result<(), Error>;
|
async fn update_character_sheet<'a>(&self, character: &'a Character) -> sqlx::Result<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO is:
|
//TODO is:
|
||||||
// - reimplement all methods
|
|
||||||
// - remove owned data
|
|
||||||
// - return sqlx result so we don't have .into() calls everywhere
|
|
||||||
// - use compile time queries
|
// - use compile time queries
|
||||||
// - find replacement for diesel migrations
|
// - find replacement for diesel migrations
|
||||||
#[rocket::async_trait]
|
#[rocket::async_trait]
|
||||||
impl Dao for SqlitePool {
|
impl Dao for SqlitePool {
|
||||||
async fn load_user_by_id(&self, user_id: i32) -> Result<Option<User>, Error> {
|
async fn load_user_by_id(&self, user_id: i32) -> sqlx::Result<Option<User>> {
|
||||||
sqlx::query_as::<_, User>("SELECT * FROM users WHERE id = ?")
|
sqlx::query_as::<_, User>("SELECT * FROM users WHERE id = ?")
|
||||||
.bind(user_id)
|
.bind(user_id)
|
||||||
.fetch_optional(self)
|
.fetch_optional(self)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.into())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn load_user(&self, for_username: &str) -> Result<Option<User>, Error> {
|
async fn load_user(&self, for_username: &str) -> sqlx::Result<Option<User>> {
|
||||||
sqlx::query_as::<_, User>("SELECT * FROM users WHERE username = ?")
|
sqlx::query_as::<_, User>("SELECT * FROM users WHERE username = ?")
|
||||||
.bind(for_username)
|
.bind(for_username)
|
||||||
.fetch_optional(self)
|
.fetch_optional(self)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.into())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn insert_user(&self, new_user: NewUser<'_>) -> Result<User, Error> {
|
async fn insert_user(&self, new_user: NewUser<'_>) -> sqlx::Result<User> {
|
||||||
sqlx::query("INSERT INTO users (username, password) values (?, ?)")
|
sqlx::query("INSERT INTO users (username, password) values (?, ?)")
|
||||||
.bind(new_user.username)
|
.bind(new_user.username)
|
||||||
.bind(new_user.password)
|
.bind(new_user.password)
|
||||||
|
@ -65,10 +60,10 @@ impl Dao for SqlitePool {
|
||||||
|
|
||||||
self.load_user(new_user.username)
|
self.load_user(new_user.username)
|
||||||
.await
|
.await
|
||||||
.and_then(|user| user.ok_or(Error::NotFound))
|
.and_then(|user| user.ok_or(sqlx::Error::RowNotFound))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn load_character_list(&self, for_user_id: i32) -> Result<Vec<StrippedCharacter>, Error> {
|
async fn load_character_list(&self, for_user_id: i32) -> sqlx::Result<Vec<StrippedCharacter>> {
|
||||||
sqlx::query_as!(
|
sqlx::query_as!(
|
||||||
StrippedCharacter,
|
StrippedCharacter,
|
||||||
r#"SELECT id as "id: _",
|
r#"SELECT id as "id: _",
|
||||||
|
@ -81,10 +76,9 @@ impl Dao for SqlitePool {
|
||||||
)
|
)
|
||||||
.fetch_all(self)
|
.fetch_all(self)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.into())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn load_character(&self, character_id: i32) -> Result<Option<Character>, Error> {
|
async fn load_character(&self, character_id: i32) -> sqlx::Result<Option<Character>> {
|
||||||
sqlx::query_as!(
|
sqlx::query_as!(
|
||||||
Character,
|
Character,
|
||||||
r#"SELECT id as "id: _",
|
r#"SELECT id as "id: _",
|
||||||
|
@ -97,10 +91,9 @@ impl Dao for SqlitePool {
|
||||||
)
|
)
|
||||||
.fetch_optional(self)
|
.fetch_optional(self)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.into())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn insert_character(&self, new_character: NewCharacter<'_>) -> Result<(), Error> {
|
async fn insert_character(&self, new_character: NewCharacter<'_>) -> sqlx::Result<()> {
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
"INSERT INTO characters
|
"INSERT INTO characters
|
||||||
(user_id, viewable, character_name, data_type, data_version, data)
|
(user_id, viewable, character_name, data_type, data_version, data)
|
||||||
|
@ -118,7 +111,7 @@ impl Dao for SqlitePool {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_character_sheet<'a>(&self, character: &'a Character) -> Result<(), Error> {
|
async fn update_character_sheet<'a>(&self, character: &'a Character) -> sqlx::Result<()> {
|
||||||
sqlx::query("UPDATE characters set data = ? where id = ?")
|
sqlx::query("UPDATE characters set data = ? where id = ?")
|
||||||
.bind(&character.data)
|
.bind(&character.data)
|
||||||
.bind(character.id)
|
.bind(character.id)
|
||||||
|
|
Loading…
Reference in New Issue