69 lines
1.6 KiB
Rust
69 lines
1.6 KiB
Rust
use sea_orm::entity::prelude::*;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize, utoipa::ToSchema)]
|
|
#[sea_orm(table_name = "BookInstances")]
|
|
#[schema(title="BookInstance", as=entities::BookInstance)]
|
|
pub struct Model {
|
|
#[sea_orm(primary_key, auto_increment = true)]
|
|
pub id: u32,
|
|
pub price: f32,
|
|
pub sold_price: Option<f32>,
|
|
pub available: bool,
|
|
pub book_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
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
|
pub enum Relation {
|
|
#[sea_orm(
|
|
belongs_to = "super::book::Entity",
|
|
from = "Column::BookId",
|
|
to = "super::book::Column::Id"
|
|
)]
|
|
Book,
|
|
#[sea_orm(
|
|
belongs_to = "super::owner::Entity",
|
|
from = "Column::OwnerId",
|
|
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 {
|
|
fn to() -> RelationDef {
|
|
Relation::Book.def()
|
|
}
|
|
}
|
|
|
|
impl Related<super::owner::Entity> for Entity {
|
|
fn to() -> RelationDef {
|
|
Relation::Owner.def()
|
|
}
|
|
}
|
|
|
|
impl Related<super::bal::Entity> for Entity {
|
|
fn to() -> RelationDef {
|
|
Relation::Bal.def()
|
|
}
|
|
}
|
|
|
|
impl ActiveModelBehavior for ActiveModel {}
|