a lot
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use sea_orm::{Set, entity::prelude::*};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
|
||||
@@ -8,8 +8,10 @@ pub struct Model {
|
||||
pub id: i32,
|
||||
#[sea_orm(unique)]
|
||||
pub key_value: String,
|
||||
pub user_id: i32,
|
||||
pub label: String,
|
||||
pub is_active: bool,
|
||||
pub is_unlisted: bool,
|
||||
pub created_at: DateTimeUtc,
|
||||
}
|
||||
|
||||
@@ -17,6 +19,12 @@ pub struct Model {
|
||||
pub enum Relation {
|
||||
#[sea_orm(has_many = "super::stream_session::Entity")]
|
||||
StreamSession,
|
||||
#[sea_orm(
|
||||
belongs_to = "super::users::Entity",
|
||||
from = "Column::UserId",
|
||||
to = "super::users::Column::Id"
|
||||
)]
|
||||
User,
|
||||
}
|
||||
|
||||
impl Related<super::stream_session::Entity> for Entity {
|
||||
@@ -25,4 +33,49 @@ impl Related<super::stream_session::Entity> for Entity {
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::users::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::User.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
impl Entity {
|
||||
pub async fn create(
|
||||
db: &DatabaseConnection,
|
||||
user_id: i32,
|
||||
key_value: String,
|
||||
label: String,
|
||||
is_unlisted: bool,
|
||||
) -> Result<Model, DbErr> {
|
||||
ActiveModel {
|
||||
user_id: Set(user_id),
|
||||
key_value: Set(key_value),
|
||||
label: Set(label),
|
||||
is_active: Set(false),
|
||||
is_unlisted: Set(is_unlisted),
|
||||
created_at: Set(chrono::Utc::now()),
|
||||
..Default::default()
|
||||
}
|
||||
.insert(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn find_by_key(
|
||||
db: &DatabaseConnection,
|
||||
key_value: &str,
|
||||
) -> Result<Option<Model>, DbErr> {
|
||||
Entity::find()
|
||||
.filter(Column::KeyValue.eq(key_value))
|
||||
.one(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn find_by_user(db: &DatabaseConnection, user_id: i32) -> Result<Vec<Model>, DbErr> {
|
||||
Entity::find()
|
||||
.filter(Column::UserId.eq(user_id))
|
||||
.all(db)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user