Added Account::get method

This commit is contained in:
StNicolay 2023-05-06 20:37:26 +03:00
parent 71940762ff
commit 4a0760d711
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
2 changed files with 21 additions and 7 deletions

View File

@ -129,4 +129,17 @@ impl Entity {
.await?; .await?;
Ok(result.is_some()) Ok(result.is_some())
} }
pub async fn get(
user_id: u64,
account_name: impl Into<String>,
db: &DatabaseConnection,
) -> crate::Result<Option<Model>> {
Self::find()
.filter(Column::UserId.eq(user_id))
.filter(Column::Name.eq(account_name.into()))
.one(db)
.await
.map_err(Into::into)
}
} }

View File

@ -1,5 +1,5 @@
use crate::{ use crate::{
entity::{account, prelude::Account}, entity::prelude::Account,
errors::NoUserInfo, errors::NoUserInfo,
handlers::{markups, utils::package_handler, MainDialogue, State}, handlers::{markups, utils::package_handler, MainDialogue, State},
}; };
@ -19,12 +19,13 @@ async fn get_master_pass(
let _ = bot.delete_message(previous.chat.id, previous.id).await; let _ = bot.delete_message(previous.chat.id, previous.id).await;
dialogue.exit().await?; dialogue.exit().await?;
let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let account = Account::find() let account = match Account::get(user_id, &name, &db).await? {
.filter(account::Column::UserId.eq(user_id)) Some(account) => account,
.filter(account::Column::Name.eq(&name)) None => {
.one(&db) bot.send_message(msg.chat.id, "Account not found").await?;
.await? return Ok(());
.unwrap(); }
};
let (login, password) = spawn_blocking(move || account.decrypt(&master_pass)).await??; let (login, password) = spawn_blocking(move || account.decrypt(&master_pass)).await??;
let message = format!("Name:\n`{name}`\nLogin:\n`{login}`\nPassword:\n`{password}`"); let message = format!("Name:\n`{name}`\nLogin:\n`{login}`\nPassword:\n`{password}`");
bot.send_message(msg.chat.id, message) bot.send_message(msg.chat.id, message)