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]
|
||||
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:
|
||||
// - reimplement all methods
|
||||
// - remove owned data
|
||||
// - return sqlx result so we don't have .into() calls everywhere
|
||||
// - use compile time queries
|
||||
// - find replacement for diesel migrations
|
||||
#[rocket::async_trait]
|
||||
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 = ?")
|
||||
.bind(user_id)
|
||||
.fetch_optional(self)
|
||||
.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 = ?")
|
||||
.bind(for_username)
|
||||
.fetch_optional(self)
|
||||
.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 (?, ?)")
|
||||
.bind(new_user.username)
|
||||
.bind(new_user.password)
|
||||
|
@ -65,10 +60,10 @@ impl Dao for SqlitePool {
|
|||
|
||||
self.load_user(new_user.username)
|
||||
.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!(
|
||||
StrippedCharacter,
|
||||
r#"SELECT id as "id: _",
|
||||
|
@ -81,10 +76,9 @@ impl Dao for SqlitePool {
|
|||
)
|
||||
.fetch_all(self)
|
||||
.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!(
|
||||
Character,
|
||||
r#"SELECT id as "id: _",
|
||||
|
@ -97,10 +91,9 @@ impl Dao for SqlitePool {
|
|||
)
|
||||
.fetch_optional(self)
|
||||
.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(
|
||||
"INSERT INTO characters
|
||||
(user_id, viewable, character_name, data_type, data_version, data)
|
||||
|
@ -118,7 +111,7 @@ impl Dao for SqlitePool {
|
|||
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 = ?")
|
||||
.bind(&character.data)
|
||||
.bind(character.id)
|
||||
|
|
Loading…
Reference in New Issue