pass_manager/entity/src/account.rs

90 lines
2.5 KiB
Rust
Raw Normal View History

//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
use futures::Stream;
use sea_orm::{entity::prelude::*, QueryOrder, QuerySelect};
2023-04-23 17:54:16 +00:00
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "account")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
2023-04-24 17:48:34 +00:00
pub user_id: u64,
2023-04-23 17:54:16 +00:00
#[sea_orm(primary_key, auto_increment = false)]
pub name: String,
#[sea_orm(column_type = "Binary(BlobSize::Blob(Some(64)))")]
pub salt: Vec<u8>,
#[sea_orm(column_type = "VarBinary(256)")]
pub enc_login: Vec<u8>,
#[sea_orm(column_type = "VarBinary(256)")]
pub enc_password: Vec<u8>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
2023-04-27 13:25:23 +00:00
impl Entity {
2023-05-09 17:27:58 +00:00
/// Gets all user's account from DB
2023-05-15 16:09:15 +00:00
#[inline]
pub async fn get_all(
user_id: u64,
db: &DatabaseConnection,
) -> crate::Result<impl Stream<Item = crate::Result<Model>> + '_> {
Self::find()
.filter(Column::UserId.eq(user_id))
.stream(db)
.await
}
/// Streams the names of the user accounts
2023-05-15 16:09:15 +00:00
#[inline]
pub async fn get_names(
user_id: u64,
db: &DatabaseConnection,
) -> crate::Result<impl Stream<Item = crate::Result<String>> + '_> {
Self::find()
2023-04-27 13:25:23 +00:00
.select_only()
.column(Column::Name)
.filter(Column::UserId.eq(user_id))
.order_by_asc(Column::Name)
.into_tuple()
.stream(db)
.await
2023-04-27 13:25:23 +00:00
}
2023-05-09 17:27:58 +00:00
/// Checks if the account exists
2023-05-15 16:09:15 +00:00
#[inline]
pub async fn exists(
user_id: u64,
account_name: impl Into<String>,
db: &DatabaseConnection,
) -> crate::Result<bool> {
let count = Self::find_by_id((user_id, account_name.into()))
.count(db)
.await?;
Ok(count != 0)
}
2023-05-06 17:37:26 +00:00
2023-05-09 17:27:58 +00:00
/// Gets the account from the DB
2023-05-15 16:09:15 +00:00
#[inline]
2023-05-06 17:37:26 +00:00
pub async fn get(
user_id: u64,
account_name: impl Into<String>,
db: &DatabaseConnection,
) -> crate::Result<Option<Model>> {
Self::find_by_id((user_id, account_name.into()))
2023-05-06 17:37:26 +00:00
.one(db)
.await
}
2023-05-09 17:32:07 +00:00
/// Deletes all the user's accounts from DB
2023-05-15 16:09:15 +00:00
#[inline]
2023-05-09 17:32:07 +00:00
pub async fn delete_all(user_id: u64, db: &DatabaseConnection) -> crate::Result<()> {
Self::delete_many()
.filter(Column::UserId.eq(user_id))
.exec(db)
.await?;
Ok(())
}
2023-04-27 13:25:23 +00:00
}