Renamed DatabaseConnection into Pool

This commit is contained in:
StNicolay 2024-02-03 16:23:39 +03:00
parent bd10acb438
commit e8f0fcd819
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
27 changed files with 37 additions and 65 deletions

View File

@ -7,7 +7,7 @@ use tokio::{task::spawn_blocking, try_join};
#[inline]
async fn update_account(
user_id: u64,
db: &DatabaseConnection,
db: &Pool,
name: String,
field: AlterableField,
field_value: String,
@ -43,7 +43,7 @@ async fn update_account(
async fn get_master_pass(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
db: Pool,
dialogue: MainDialogue,
mut ids: MessageIds,
name: String,
@ -73,7 +73,7 @@ handler!(get_field(name:String, field:AlterableField, field_value:String), "Send
pub async fn alter(
bot: Throttle<Bot>,
q: CallbackQuery,
db: DatabaseConnection,
db: Pool,
dialogue: MainDialogue,
(hash, field): (super::NameHash, AlterableField),
) -> crate::Result<()> {

View File

@ -6,7 +6,7 @@ use tokio::{task::spawn_blocking, try_join};
async fn get_master_pass(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
db: Pool,
dialogue: MainDialogue,
mut ids: MessageIds,
name: String,
@ -45,7 +45,7 @@ async fn get_master_pass(
pub async fn decrypt(
bot: Throttle<Bot>,
q: CallbackQuery,
db: DatabaseConnection,
db: Pool,
dialogue: MainDialogue,
hash: super::NameHash,
) -> crate::Result<()> {

View File

@ -7,7 +7,7 @@ use crate::{change_state, prelude::*};
async fn get_master_pass(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
db: Pool,
dialogue: MainDialogue,
mut ids: MessageIds,
name: String,
@ -33,7 +33,7 @@ async fn get_master_pass(
pub async fn delete(
bot: Throttle<Bot>,
q: CallbackQuery,
db: DatabaseConnection,
db: Pool,
dialogue: MainDialogue,
(hash, is_command): (super::NameHash, bool),
) -> crate::Result<()> {

View File

@ -6,7 +6,7 @@ use teloxide::types::ParseMode;
pub async fn get(
bot: Throttle<Bot>,
q: CallbackQuery,
db: DatabaseConnection,
db: Pool,
hash: super::NameHash,
) -> crate::Result<()> {
let user_id = q.from.id.0;

View File

@ -1,11 +1,7 @@
use crate::prelude::*;
#[inline]
pub async fn get_menu(
bot: Throttle<Bot>,
q: CallbackQuery,
db: DatabaseConnection,
) -> crate::Result<()> {
pub async fn get_menu(bot: Throttle<Bot>, q: CallbackQuery, db: Pool) -> crate::Result<()> {
let user_id = q.from.id.0;
let mut ids: MessageIds = q.message.as_ref().unwrap().into();

View File

@ -7,7 +7,7 @@ use tokio::task::spawn_blocking;
async fn get_master_pass(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
db: Pool,
dialogue: MainDialogue,
mut ids: MessageIds,
name: String,

View File

@ -2,7 +2,7 @@ use crate::prelude::*;
use tokio::task::spawn_blocking;
#[inline]
pub async fn delete(bot: Throttle<Bot>, msg: Message, db: DatabaseConnection) -> crate::Result<()> {
pub async fn delete(bot: Throttle<Bot>, msg: Message, db: Pool) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let names: Vec<String> = Account::get_names(user_id, &db).try_collect().await?;

View File

@ -7,7 +7,7 @@ use log::error;
async fn get_master_pass(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
db: Pool,
dialogue: MainDialogue,
mut ids: MessageIds,
_: String,

View File

@ -22,7 +22,7 @@ async fn decrypt_account(
async fn get_master_pass(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
db: Pool,
dialogue: MainDialogue,
ids: MessageIds,
master_pass: String,

View File

@ -2,11 +2,7 @@ use crate::prelude::*;
use tokio::task::spawn_blocking;
#[inline]
pub async fn get_account(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
) -> crate::Result<()> {
pub async fn get_account(bot: Throttle<Bot>, msg: Message, db: Pool) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let names: Vec<String> = Account::get_names(user_id, &db).try_collect().await?;

View File

@ -5,11 +5,7 @@ use teloxide::types::ParseMode;
/// Handles /`get_accounts` command by sending the list of copyable account names to the user
#[inline]
pub async fn get_accounts(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
) -> crate::Result<()> {
pub async fn get_accounts(bot: Throttle<Bot>, msg: Message, db: Pool) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let mut account_names = Account::get_names(user_id, &db);

View File

@ -11,7 +11,7 @@ use tokio::task::spawn_blocking;
async fn encrypt_account(
account: DecryptedAccount,
user_id: u64,
db: &DatabaseConnection,
db: &Pool,
master_pass: Arc<str>,
failed: &Mutex<&mut Vec<String>>,
) {
@ -30,7 +30,7 @@ async fn encrypt_account(
async fn get_master_pass(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
db: Pool,
dialogue: MainDialogue,
mut ids: MessageIds,
user: User,

View File

@ -2,7 +2,7 @@ use crate::prelude::*;
use tokio::task::spawn_blocking;
#[inline]
pub async fn menu(bot: Throttle<Bot>, msg: Message, db: DatabaseConnection) -> crate::Result<()> {
pub async fn menu(bot: Throttle<Bot>, msg: Message, db: Pool) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let names: Vec<String> = Account::get_names(user_id, &db).try_collect().await?;

View File

@ -6,7 +6,7 @@ use tokio::task::spawn_blocking;
async fn get_master_pass2(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
db: Pool,
dialogue: MainDialogue,
mut ids: MessageIds,
hash: HashedBytes<[u8; 64], [u8; 64]>,
@ -45,7 +45,7 @@ async fn get_master_pass2(
async fn get_master_pass(
bot: Throttle<Bot>,
_: Message,
_: DatabaseConnection,
_: Pool,
dialogue: MainDialogue,
mut ids: MessageIds,
master_pass: String,
@ -71,7 +71,7 @@ pub async fn set_master_pass(
bot: Throttle<Bot>,
msg: Message,
dialogue: MainDialogue,
db: DatabaseConnection,
db: Pool,
) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
if MasterPass::exists(user_id, &db).await? {

View File

@ -21,7 +21,7 @@ use crate::callbacks::CallbackCommand;
fn get_dispatcher(
token: String,
db: DatabaseConnection,
db: Pool,
) -> Dispatcher<Throttle<Bot>, crate::Error, teloxide::dispatching::DefaultKey> {
use dptree::{case, deps};
@ -87,7 +87,7 @@ async fn main() -> Result<()> {
let token = env::var("TOKEN").expect("expected TOKEN in the enviroment");
let database_url = env::var("DATABASE_URL").expect("expected DATABASE_URL in the enviroment");
let pool = DatabaseConnection::connect(&database_url).await?;
let pool = Pool::connect(&database_url).await?;
entity::migrate(&pool).await?;

View File

@ -29,7 +29,7 @@ pub fn menu_markup_sync(
pub async fn menu_markup(
command: impl Into<String> + Send,
user_id: u64,
db: &DatabaseConnection,
db: &Pool,
) -> crate::Result<InlineKeyboardMarkup> {
let command: String = command.into();
let names: Vec<String> = Account::get_names(user_id, db).try_collect().await?;

View File

@ -14,7 +14,7 @@ type DynError = Arc<dyn std::error::Error + Send + Sync>;
/// Returns None if account exists, Some(None) if there's an account and Some(Some(String)) if an error occures.
/// The String represents the error that occured
#[inline]
async fn master_pass_exists(update: Update, db: DatabaseConnection) -> Option<Option<DynError>> {
async fn master_pass_exists(update: Update, db: Pool) -> Option<Option<DynError>> {
let user_id = match update.user() {
Some(user) => user.id.0,
None => return Some(Some(Arc::new(NoUserInfo))),

View File

@ -8,6 +8,6 @@ pub use crate::{
utils::*,
};
pub use cryptography::prelude::*;
pub use entity::{prelude::*, Pool as DatabaseConnection};
pub use entity::{prelude::*, Pool};
pub use futures::{StreamExt, TryStreamExt};
pub use teloxide::{adaptors::Throttle, prelude::*};

View File

@ -6,18 +6,14 @@ use futures::future::BoxFuture;
pub async fn generic<F>(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
db: Pool,
dialogue: MainDialogue,
check: F,
no_text_message: impl Into<String> + Send,
handler: PackagedHandler<String>,
) -> crate::Result<()>
where
for<'a> F: FnOnce(
&'a Message,
&'a DatabaseConnection,
&'a str,
) -> BoxFuture<'a, crate::Result<Option<String>>>
for<'a> F: FnOnce(&'a Message, &'a Pool, &'a str) -> BoxFuture<'a, crate::Result<Option<String>>>
+ Send,
{
let mut handler = handler.lock().await;

View File

@ -1,11 +1,7 @@
use crate::prelude::*;
#[inline]
async fn check_login(
_: &Message,
_: &DatabaseConnection,
login: &str,
) -> crate::Result<Option<String>> {
async fn check_login(_: &Message, _: &Pool, login: &str) -> crate::Result<Option<String>> {
let is_valid = validate_field(login);
if !is_valid {
return Ok(Some("Invalid login. Try again".to_owned()));

View File

@ -7,7 +7,7 @@ use tokio::task::spawn_blocking;
#[inline]
async fn check_master_pass(
msg: &Message,
db: &DatabaseConnection,
db: &Pool,
master_pass: &str,
) -> crate::Result<Option<String>> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;

View File

@ -34,7 +34,7 @@ fn process_validity(validity: PasswordValidity) -> Result<(), String> {
#[inline]
async fn check_new_master_pass(
_: &Message,
_: &DatabaseConnection,
_: &Pool,
password: &str,
) -> crate::Result<Option<String>> {
let validity = check_master_pass(password);

View File

@ -4,7 +4,7 @@ use crate::prelude::*;
#[inline]
async fn check_new_account_name(
msg: &Message,
db: &DatabaseConnection,
db: &Pool,
name: &str,
) -> crate::Result<Option<String>> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;

View File

@ -1,11 +1,7 @@
use crate::prelude::*;
#[inline]
async fn check_password(
_: &Message,
_: &DatabaseConnection,
password: &str,
) -> crate::Result<Option<String>> {
async fn check_password(_: &Message, _: &Pool, password: &str) -> crate::Result<Option<String>> {
let is_valid = validate_field(password);
if !is_valid {
return Ok(Some("Invalid password. Try again".to_owned()));

View File

@ -132,7 +132,7 @@ fn user_from_vec(
pub async fn get_user(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
db: Pool,
dialogue: MainDialogue,
handler: PackagedHandler<User>,
) -> crate::Result<()> {

View File

@ -65,7 +65,7 @@ type DynHanlder<T> = Box<
dyn FnOnce(
Throttle<Bot>,
Message,
DatabaseConnection,
Pool,
MainDialogue,
MessageIds,
T,
@ -89,7 +89,7 @@ impl<T> Handler<T> {
H: FnOnce(
Throttle<Bot>,
Message,
DatabaseConnection,
Pool,
MainDialogue,
MessageIds,
T,

View File

@ -7,11 +7,7 @@ pub async fn delete_message(bot: Throttle<Bot>, msg: Message) {
}
#[inline]
pub async fn name_from_hash(
db: &DatabaseConnection,
user_id: u64,
hash: &[u8],
) -> crate::Result<Option<String>> {
pub async fn name_from_hash(db: &Pool, user_id: u64, hash: &[u8]) -> crate::Result<Option<String>> {
let hash = hex::encode(hash);
Account::get_name_by_hash(user_id, hash, db)
.await