Updated account entity for better readability, now Account::get_names always orders by name

This commit is contained in:
StNicolay 2023-06-12 17:55:21 +03:00
parent 7ece22c081
commit ef27285e96
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
3 changed files with 11 additions and 15 deletions

View File

@ -30,29 +30,26 @@ impl Entity {
user_id: u64, user_id: u64,
db: &DatabaseConnection, db: &DatabaseConnection,
) -> crate::Result<impl Stream<Item = crate::Result<Model>> + '_> { ) -> crate::Result<impl Stream<Item = crate::Result<Model>> + '_> {
let result = Self::find() Self::find()
.filter(Column::UserId.eq(user_id)) .filter(Column::UserId.eq(user_id))
.stream(db) .stream(db)
.await?; .await
Ok(result)
} }
/// Gets a list of account names of a user /// Streams the names of the user accounts
#[inline] #[inline]
pub async fn get_names( pub async fn get_names(
user_id: u64, user_id: u64,
db: &DatabaseConnection, db: &DatabaseConnection,
ordered: bool,
) -> crate::Result<impl Stream<Item = crate::Result<String>> + '_> { ) -> crate::Result<impl Stream<Item = crate::Result<String>> + '_> {
let mut select = Self::find() Self::find()
.select_only() .select_only()
.column(Column::Name) .column(Column::Name)
.filter(Column::UserId.eq(user_id)); .filter(Column::UserId.eq(user_id))
if ordered { .order_by_asc(Column::Name)
select = select.order_by_asc(Column::Name); .into_tuple()
} .stream(db)
let result = select.into_tuple().stream(db).await?; .await
Ok(result)
} }
/// Checks if the account exists /// Checks if the account exists
@ -81,7 +78,6 @@ impl Entity {
Self::find_by_id((user_id, account_name.into())) Self::find_by_id((user_id, account_name.into()))
.one(db) .one(db)
.await .await
.map_err(Into::into)
} }
/// Deletes all the user's accounts from DB /// Deletes all the user's accounts from DB

View File

@ -12,7 +12,7 @@ pub async fn get_accounts(
db: DatabaseConnection, db: DatabaseConnection,
) -> crate::Result<()> { ) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let mut account_names = Account::get_names(user_id, &db, true).await?; let mut account_names = Account::get_names(user_id, &db).await?;
let mut text = match account_names.try_next().await? { let mut text = match account_names.try_next().await? {
Some(name) => format!("Accounts:\n`{name}`"), Some(name) => format!("Accounts:\n`{name}`"),
None => { None => {

View File

@ -9,7 +9,7 @@ pub async fn account_markup(
user_id: u64, user_id: u64,
db: &DatabaseConnection, db: &DatabaseConnection,
) -> crate::Result<KeyboardMarkup> { ) -> crate::Result<KeyboardMarkup> {
let account_names: Vec<Vec<KeyboardButton>> = Account::get_names(user_id, db, true) let account_names: Vec<Vec<KeyboardButton>> = Account::get_names(user_id, db)
.await? .await?
.map_ok(KeyboardButton::new) .map_ok(KeyboardButton::new)
.try_chunks(3) .try_chunks(3)