This commit is contained in:
2026-07-01 01:49:51 +01:00
parent 808d6ac5db
commit c1da3abe7d
12 changed files with 1234 additions and 266 deletions
+27
View File
@@ -1,6 +1,7 @@
use sea_orm::{
ActiveValue::{NotSet, Set},
entity::prelude::*,
sea_query::Expr,
sqlx::types::chrono::{self, Utc},
};
use serde::{Deserialize, Serialize};
@@ -43,6 +44,7 @@ impl Model {
id: NotSet,
stream_key_id: Set(stream_key_id),
started_at: Set(started_at),
ended_at: NotSet,
..Default::default()
}
.insert(db)
@@ -59,6 +61,31 @@ impl Model {
"stream_session {stream_session_id}"
)))
}
pub async fn get_all_active_sessions(db: &DatabaseConnection) -> Result<Vec<Model>, DbErr> {
Ok(Entity::find()
.filter(Column::EndedAt.is_null())
.all(db)
.await?)
}
pub async fn get_active_by_stream_key_id(
db: &DatabaseConnection,
stream_key_id: i32,
) -> Result<Option<Model>, DbErr> {
Ok(Entity::find()
.filter(Column::StreamKeyId.eq(stream_key_id))
.filter(Column::EndedAt.is_null())
.one(db)
.await?)
}
pub async fn clean_unended_streams(db: &DatabaseConnection) -> Result<u64, DbErr> {
let result = Entity::update_many()
.filter(Column::EndedAt.is_null())
.col_expr(Column::EndedAt, Expr::col(Column::StartedAt).into())
.exec(db)
.await?;
Ok(result.rows_affected)
}
}
impl ActiveModel {
+10
View File
@@ -61,6 +61,16 @@ impl Entity {
return Ok(None);
}
}
pub async fn find_by_username(
db: &DatabaseConnection,
username: String,
) -> Result<Option<Model>, DbErr> {
let user = Entity::find()
.filter(Column::Username.eq(username))
.one(db)
.await;
user
}
}
impl ActiveModel {