This commit is contained in:
2026-06-22 15:19:46 +01:00
parent de96bc030d
commit 808d6ac5db
21 changed files with 948 additions and 282 deletions
+5 -1
View File
@@ -4,5 +4,9 @@ version = "0.1.0"
edition = "2024"
[dependencies]
sea-orm = { version = "1", features = ["sqlx-sqlite", "runtime-tokio-rustls", "macros"] }
sea-orm = { version = "1", features = [ "sqlx-sqlite", "runtime-tokio-rustls", "macros" ] }
serde = { version = "1", features = ["derive"] }
chrono = { version = "0.4", features = ["serde"] }
rand = "0.10.1"
argon2 = "0.5.3"
uuid = { version = "1.23.3", features = ["v4"] }
+54
View File
@@ -0,0 +1,54 @@
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 {}
+2
View File
@@ -1,3 +1,5 @@
pub mod auth_session;
pub mod prelude;
pub mod stream_key;
pub mod stream_session;
pub mod users;
+2
View File
@@ -1,2 +1,4 @@
pub use super::auth_session::Entity as AuthSession;
pub use super::stream_key::Entity as StreamKey;
pub use super::stream_session::Entity as StreamSession;
pub use super::users::Entity as Users;
+54 -1
View File
@@ -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
}
}
+44 -1
View File
@@ -1,4 +1,8 @@
use sea_orm::entity::prelude::*;
use sea_orm::{
ActiveValue::{NotSet, Set},
entity::prelude::*,
sqlx::types::chrono::{self, Utc},
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
@@ -28,3 +32,42 @@ impl Related<super::stream_key::Entity> for Entity {
}
impl ActiveModelBehavior for ActiveModel {}
impl Model {
pub async fn create_stream_session(
db: &DatabaseConnection,
stream_key_id: i32,
started_at: chrono::DateTime<Utc>,
) -> Result<Model, DbErr> {
ActiveModel {
id: NotSet,
stream_key_id: Set(stream_key_id),
started_at: Set(started_at),
..Default::default()
}
.insert(db)
.await
}
pub async fn get_stream_session(
db: &DatabaseConnection,
stream_session_id: i32,
) -> Result<Model, DbErr> {
Entity::find_by_id(stream_session_id)
.one(db)
.await?
.ok_or(DbErr::RecordNotFound(format!(
"stream_session {stream_session_id}"
)))
}
}
impl ActiveModel {
pub async fn finish_stream_session(
mut self,
db: &DatabaseConnection,
ended_at: chrono::DateTime<Utc>,
) -> Result<Model, DbErr> {
self.ended_at = Set(Some(ended_at));
self.update(db).await
}
}
+92
View File
@@ -0,0 +1,92 @@
use sea_orm::{Set, entity::prelude::*};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "users")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub username: String,
pub hashed_password: String,
pub stream_key_limit: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::stream_key::Entity")]
StreamKey,
#[sea_orm(has_many = "super::auth_session::Entity")]
AuthSession,
}
impl Related<super::stream_key::Entity> for Entity {
fn to() -> RelationDef {
Relation::StreamKey.def()
}
}
impl Related<super::auth_session::Entity> for Entity {
fn to() -> RelationDef {
Relation::AuthSession.def()
}
}
impl ActiveModelBehavior for ActiveModel {}
impl Entity {
pub async fn create(
db: &DatabaseConnection,
username: String,
password: String,
) -> Result<Model, DbErr> {
ActiveModel {
username: Set(username),
hashed_password: Set(password),
..Default::default()
}
.insert(db)
.await
}
pub async fn find_by_auth_session(
db: &DatabaseConnection,
auth_session: String,
) -> Result<Option<Model>, DbErr> {
let sessions = crate::auth_session::Entity::find()
.filter(crate::auth_session::Column::Value.eq(auth_session.to_string()))
.one(db)
.await?;
if let Some(x) = sessions {
Entity::find_by_id(x.id_user).one(db).await
} else {
return Ok(None);
}
}
}
impl ActiveModel {
pub async fn update_username(
mut self,
db: &DatabaseConnection,
username: String,
) -> Result<Model, DbErr> {
self.username = Set(username);
self.update(db).await
}
pub async fn update_password(
mut self,
db: &DatabaseConnection,
hashed_password: String,
) -> Result<Model, DbErr> {
self.hashed_password = Set(hashed_password);
self.update(db).await
}
pub async fn change_stream_key_limit(
mut self,
db: &DatabaseConnection,
new_limit: i32,
) -> Result<Model, DbErr> {
self.stream_key_limit = Set(new_limit);
self.update(db).await
}
}