feat: bal and user permission checks

This commit is contained in:
Ninjdai 2025-08-03 20:10:00 +02:00
parent e3f954679a
commit e078bffc25
13 changed files with 207 additions and 19 deletions

37
src/entities/bal.rs Normal file
View file

@ -0,0 +1,37 @@
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize, utoipa::ToSchema)]
#[sea_orm(table_name = "BAL")]
#[schema(title="Book", as=entities::BAL)]
pub struct Model {
#[sea_orm(primary_key, auto_increment = true)]
pub id: u32,
pub user_id: u32,
pub name: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::book_instance::Entity")]
BookInstance,
}
impl Related<super::book_instance::Entity> for Entity {
fn to() -> RelationDef {
Relation::BookInstance.def()
}
}
impl Entity {
pub async fn get_by_id<C>(db_conn: &C, id: u32) -> Option<Model>
where C: ConnectionTrait,
{
match Self::find_by_id(id).one(db_conn).await {
Ok(res) => res,
Err(_) => None
}
}
}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -11,9 +11,20 @@ pub struct Model {
pub sold_price: Option<i32>,
pub status: BookStatus,
pub book_id: u32,
pub owner_id: u32
pub owner_id: u32,
pub bal_id: u32
}
impl Entity {
pub async fn get_by_id<C>(db_conn: &C, id: u32) -> Option<Model>
where C: ConnectionTrait,
{
match Self::find_by_id(id).one(db_conn).await {
Ok(res) => res,
Err(_) => None
}
}
}
// Missing: Bal
#[derive(EnumIter, DeriveActiveEnum, PartialEq, Eq ,Deserialize, Serialize, Clone, Copy, Debug, utoipa::ToSchema)]
#[sea_orm(rs_type = "String", db_type = "String(StringLen::N(1))")]
@ -40,6 +51,12 @@ pub enum Relation {
to = "super::owner::Column::Id"
)]
Owner,
#[sea_orm(
belongs_to = "super::bal::Entity",
from = "Column::BalId",
to = "super::bal::Column::Id"
)]
Bal,
}
impl Related<super::book::Entity> for Entity {
@ -54,4 +71,10 @@ impl Related<super::owner::Entity> for Entity {
}
}
impl Related<super::bal::Entity> for Entity {
fn to() -> RelationDef {
Relation::Bal.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -1,5 +1,6 @@
pub mod prelude;
pub mod bal;
pub mod book;
pub mod book_instance;
pub mod owner;

View file

@ -7,15 +7,33 @@ use serde::{Deserialize, Serialize};
pub struct Model {
#[sea_orm(primary_key, auto_increment = true)]
pub id: u32,
pub user_id: u32,
pub first_name: String,
pub last_name: String,
pub contact: String
}
impl Entity {
pub async fn get_by_id<C>(db_conn: &C, id: u32) -> Option<Model>
where C: ConnectionTrait,
{
match Self::find_by_id(id).one(db_conn).await {
Ok(res) => res,
Err(_) => None
}
}
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::book_instance::Entity")]
BookInstance
BookInstance,
#[sea_orm(
belongs_to = "super::user::Entity",
from = "Column::UserId",
to = "super::user::Column::Id"
)]
User,
}
impl Related<super::book_instance::Entity> for Entity {
@ -24,4 +42,10 @@ impl Related<super::book_instance::Entity> for Entity {
}
}
impl Related<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -1,4 +1,5 @@
pub use super::bal::Entity as Bal;
pub use super::book::Entity as Book;
pub use super::book_instance::Entity as BookInstance;
pub use super::owner::Entity as Owner;

View file

@ -21,6 +21,14 @@ impl Model {
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::owner::Entity")]
Owner,
}
impl Related<super::owner::Entity> for Entity {
fn to() -> RelationDef {
Relation::Owner.def()
}
}
impl ActiveModelBehavior for ActiveModel {}