#[cfg(feature="sea-orm")]
use sea_orm::entity::prelude::*;
#[cfg_attr(feature="sea-orm",derive(DeriveEntityModel))]
#[cfg_attr(feature="serde",derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature="sea-orm",sea_orm(table_name = "user"))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Model {
#[cfg_attr(feature="sea-orm",sea_orm(primary_key))]
pub id: i64,
}
#[cfg(feature="sea-orm")]
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::bank_account::Entity")]
BankAccount,
#[sea_orm(has_many = "super::transaction::Entity")]
Transaction,
#[sea_orm(has_many = "super::credit_cards::Entity")]
CreditCards
}
#[cfg(feature="sea-orm")]
impl Related<super::bank_account::Entity> for Entity {
fn to() -> RelationDef {
Relation::BankAccount.def()
}
}
#[cfg(feature="sea-orm")]
impl Related<super::transaction::Entity> for Entity {
fn to() -> RelationDef {
Relation::Transaction.def()
}
}
#[cfg(feature="sea-orm")]
impl ActiveModelBehavior for ActiveModel {}
#[cfg_attr(feature="serde",derive(serde::Serialize,serde::Deserialize))]
#[cfg_attr(feature="clap",derive(clap::Args))]
#[derive(Clone,PartialEq,Debug)]
pub struct CreateUserOptions {
#[cfg_attr(feature="clap",arg(long))]
pub id: Option<i64>
}
#[cfg(feature="sea-orm")]
pub async fn create(conn: &sea_orm::DatabaseConnection,opts: &CreateUserOptions) ->Result<Model,anyhow::Error> {
let mut active_model: ActiveModel = <ActiveModel as ActiveModelTrait>::default();
if opts.id.is_some() {
active_model.id = sea_orm::ActiveValue::Set(opts.id.unwrap())
}
active_model.save(conn).await.map(|a_m| a_m.try_into()).map_err(|e| anyhow::Error::new(e).context("While attempting to create a user in the database"))?.map_err(|e| anyhow::Error::new(e).context("While converting an active model to a model"))
}
#[cfg(feature="sea-orm")]
pub async fn get_by_id(conn: &sea_orm::DatabaseConnection,id: i64) -> Result<Option<Model>,anyhow::Error> {
Entity::find_by_id(id).one(conn).await.map_err(|e| anyhow::Error::new(e).context("While getting a user model by id"))
}