From b443f28a50ebfa20d93a729b0e9c4d9e895ed929 Mon Sep 17 00:00:00 2001 From: jeff Date: Wed, 30 Dec 2020 15:50:45 +0000 Subject: [PATCH] Use sqlx result in db.rs --- src/db.rs | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/src/db.rs b/src/db.rs index ffd7536..7da95db 100644 --- a/src/db.rs +++ b/src/db.rs @@ -17,46 +17,41 @@ pub(crate) async fn create_pool() -> Result { #[rocket::async_trait] pub(crate) trait Dao { - async fn load_user_by_id(&self, id: i32) -> Result, Error>; + async fn load_user_by_id(&self, id: i32) -> sqlx::Result>; - async fn load_user(&self, for_username: &str) -> Result, Error>; + async fn load_user(&self, for_username: &str) -> sqlx::Result>; - async fn insert_user(&self, new_user: NewUser<'_>) -> Result; + async fn insert_user(&self, new_user: NewUser<'_>) -> sqlx::Result; - async fn load_character_list(&self, for_user_id: i32) -> Result, Error>; + async fn load_character_list(&self, for_user_id: i32) -> sqlx::Result>; - async fn load_character(&self, character_id: i32) -> Result, Error>; + async fn load_character(&self, character_id: i32) -> sqlx::Result>; - 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, Error> { + async fn load_user_by_id(&self, user_id: i32) -> sqlx::Result> { 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, Error> { + async fn load_user(&self, for_username: &str) -> sqlx::Result> { 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 { + async fn insert_user(&self, new_user: NewUser<'_>) -> sqlx::Result { 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, Error> { + async fn load_character_list(&self, for_user_id: i32) -> sqlx::Result> { 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, Error> { + async fn load_character(&self, character_id: i32) -> sqlx::Result> { 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)