pass_manager/entity/src/account.rs

97 lines
2.7 KiB
Rust

//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
use futures::Stream;
use sea_orm::{entity::prelude::*, QueryOrder, QuerySelect};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "account")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub user_id: u64,
#[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 {}
impl Entity {
/// Gets all user's account from DB
#[inline]
pub async fn get_all(
user_id: u64,
db: &DatabaseConnection,
) -> crate::Result<impl Stream<Item = crate::Result<Model>> + '_> {
let result = Self::find()
.filter(Column::UserId.eq(user_id))
.stream(db)
.await?;
Ok(result)
}
/// Gets a list of account names of a user
#[inline]
pub async fn get_names(
user_id: u64,
db: &DatabaseConnection,
ordered: bool,
) -> crate::Result<impl Stream<Item = crate::Result<String>> + '_> {
let mut select = Self::find()
.select_only()
.column(Column::Name)
.filter(Column::UserId.eq(user_id));
if ordered {
select = select.order_by_asc(Column::Name);
}
let result = select.into_tuple().stream(db).await?;
Ok(result)
}
/// Checks if the account exists
#[inline]
pub async fn exists(
user_id: u64,
account_name: impl Into<String>,
db: &DatabaseConnection,
) -> crate::Result<bool> {
let result = Self::find_by_id((user_id, account_name.into()))
.select_only()
.column(Column::UserId)
.into_tuple::<u64>()
.one(db)
.await?;
Ok(result.is_some())
}
/// Gets the account from the DB
#[inline]
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()))
.one(db)
.await
.map_err(Into::into)
}
/// Deletes all the user's accounts from DB
#[inline]
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(())
}
}