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, pub available: bool, pub book_id: u32, pub owner_id: u32, pub bal_id: u32 } impl Entity { pub async fn get_by_id(db_conn: &C, id: u32) -> Option 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 for Entity { fn to() -> RelationDef { Relation::Book.def() } } impl Related for Entity { fn to() -> RelationDef { Relation::Owner.def() } } impl Related for Entity { fn to() -> RelationDef { Relation::Bal.def() } } impl ActiveModelBehavior for ActiveModel {}