90 lines
2.5 KiB
Rust
90 lines
2.5 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>> + '_> {
|
|
Self::find()
|
|
.filter(Column::UserId.eq(user_id))
|
|
.stream(db)
|
|
.await
|
|
}
|
|
|
|
/// Streams the names of the user accounts
|
|
#[inline]
|
|
pub async fn get_names(
|
|
user_id: u64,
|
|
db: &DatabaseConnection,
|
|
) -> crate::Result<impl Stream<Item = crate::Result<String>> + '_> {
|
|
Self::find()
|
|
.select_only()
|
|
.column(Column::Name)
|
|
.filter(Column::UserId.eq(user_id))
|
|
.order_by_asc(Column::Name)
|
|
.into_tuple()
|
|
.stream(db)
|
|
.await
|
|
}
|
|
|
|
/// Checks if the account exists
|
|
#[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)
|
|
}
|
|
|
|
/// 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
|
|
}
|
|
|
|
/// Deletes all the user's accounts from DB
|
|
#[inline]
|
|
pub async fn delete_all(user_id: u64, db: &impl ConnectionTrait) -> crate::Result<()> {
|
|
Self::delete_many()
|
|
.filter(Column::UserId.eq(user_id))
|
|
.exec(db)
|
|
.await?;
|
|
Ok(())
|
|
}
|
|
}
|