-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add model layer for abstraction over db types
- Loading branch information
1 parent
049b39f
commit f8c1f13
Showing
6 changed files
with
119 additions
and
36 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
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,9 @@ | ||
use cfg_if::cfg_if; | ||
|
||
cfg_if! { | ||
if #[cfg(feature = "ssr")] { | ||
mod user; | ||
|
||
pub use self::user::*; | ||
} | ||
} |
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,50 @@ | ||
use std::error::Error; | ||
|
||
use crate::repository::UserRepository; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub struct User { | ||
pub id: String, | ||
pub username: String, | ||
pub email: String, | ||
pub password: String, | ||
} | ||
|
||
impl User { | ||
pub async fn create(self) -> Result<(), Box<dyn Error>> { | ||
let User { | ||
username, | ||
email, | ||
password, | ||
.. | ||
} = self; | ||
UserRepository::create(username, password, email).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn get_by_username(username: impl ToString) -> Option<User> { | ||
let Some(user) = UserRepository::get_by_username(username) | ||
.await | ||
.ok() | ||
.flatten() | ||
else { | ||
return None; | ||
}; | ||
|
||
let id = user.id().expect("user from database should have id"); | ||
let UserRepository { | ||
username, | ||
password, | ||
email, | ||
.. | ||
} = user; | ||
|
||
Some(Self { | ||
id, | ||
username, | ||
password, | ||
email, | ||
}) | ||
} | ||
} |
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