database: update docs

This commit is contained in:
MedzikUser 2022-05-01 18:47:26 +02:00
parent 08a61f1a48
commit 2cca093d26
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
2 changed files with 20 additions and 4 deletions

View File

@ -23,8 +23,8 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::UserNotFound => write!(f, "user not found"),
Error::SQLx(err) => write!(f, "sqlx error: {}", err),
Error::Io(err) => write!(f, "std::io error: {}", err),
Error::SQLx(err) => write!(f, "kind `sqlx`: {:?}", err),
Error::Io(err) => write!(f, "kind `io`: {:?}", err),
}
}
}

View File

@ -43,8 +43,15 @@ impl Database {
}
/// Find user
/// ```ignore
/// use homedisk_database::{Database, User};
///
/// let user = User::new("username", "password");
///
/// db.find_user(&user.username, &user.password).await?;
/// ```
pub async fn find_user(&self, username: &str, password: &str) -> Result<User, Error> {
debug!("search for user - {}", username);
debug!("searching for user - {}", username);
let query =
sqlx::query_as::<_, User>("SELECT * FROM user WHERE username = ? AND password = ?")
@ -67,7 +74,16 @@ impl Database {
}
/// Find user by UUID
/// ```ignore
/// use homedisk_database::{Database, User};
///
/// let user = User::new("username", "password");
///
/// db.find_user_by_id(&user.id).await?;
/// ```
pub async fn find_user_by_id(&self, id: String) -> Result<User, Error> {
debug!("searching for a user by UUID - {}", id);
let query = sqlx::query_as::<_, User>("SELECT * FROM user WHERE id = ?").bind(id);
let mut stream = self.conn.fetch(query);
@ -191,7 +207,7 @@ mod tests {
new_user(&db).await;
let other_user = User::new("other_user", "secretpassphrase123!");
let other_user = User::new("other_user", "secret@passphrase123!");
let err = db.find_user_by_id(other_user.id).await.unwrap_err();