55 lines
1.3 KiB
Rust
55 lines
1.3 KiB
Rust
use sea_orm::{ActiveValue::Set, entity::prelude::*};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
|
|
#[sea_orm(table_name = "auth_session")]
|
|
pub struct Model {
|
|
#[sea_orm(primary_key)]
|
|
pub id: i32,
|
|
pub id_user: i32,
|
|
pub value: String,
|
|
}
|
|
|
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
|
pub enum Relation {
|
|
#[sea_orm(
|
|
belongs_to = "super::users::Entity",
|
|
from = "Column::IdUser",
|
|
to = "super::users::Column::Id"
|
|
)]
|
|
Users,
|
|
}
|
|
|
|
impl Related<super::users::Entity> for Entity {
|
|
fn to() -> RelationDef {
|
|
Relation::Users.def()
|
|
}
|
|
}
|
|
|
|
impl ActiveModelBehavior for ActiveModel {}
|
|
|
|
impl Entity {
|
|
pub async fn create(db: &DatabaseConnection, user_id: i32) -> Result<Model, DbErr> {
|
|
let value = uuid::Uuid::new_v4().to_string();
|
|
|
|
ActiveModel {
|
|
id_user: Set(user_id),
|
|
value: Set(value),
|
|
..Default::default()
|
|
}
|
|
.insert(db)
|
|
.await
|
|
}
|
|
pub async fn find_by_user_id(
|
|
db: &DatabaseConnection,
|
|
user_id: i32,
|
|
) -> Result<Option<Model>, DbErr> {
|
|
Entity::find()
|
|
.filter(Column::IdUser.eq(user_id))
|
|
.one(db)
|
|
.await
|
|
}
|
|
}
|
|
|
|
impl ActiveModel {}
|