use arcature_db::Db;
use arcature_db::sea_orm;
use arcature_db::sea_orm::{ActiveModelTrait, EntityTrait};
use crate::error::DataError;
type Row<A> = <<A as sea_orm::ActiveModelTrait>::Entity as EntityTrait>::Model;
pub async fn insert<A>(db: &Db, active: A) -> Result<Row<A>, DataError>
where
A: ActiveModelTrait + sea_orm::ActiveModelBehavior + Send,
<A::Entity as EntityTrait>::Model: sea_orm::IntoActiveModel<A>,
{
active.insert(db.orm()).await.map_err(DataError::from)
}
pub async fn update<A>(db: &Db, active: A) -> Result<Row<A>, DataError>
where
A: ActiveModelTrait + sea_orm::ActiveModelBehavior + Send,
<A::Entity as EntityTrait>::Model: sea_orm::IntoActiveModel<A>,
{
active.update(db.orm()).await.map_err(DataError::from)
}
pub async fn delete<A>(db: &Db, active: A) -> Result<sea_orm::DeleteResult, DataError>
where
A: ActiveModelTrait + sea_orm::ActiveModelBehavior + Send,
<A::Entity as EntityTrait>::Model: sea_orm::IntoActiveModel<A>,
{
active.delete(db.orm()).await.map_err(DataError::from)
}
pub async fn find_by_pk<E, P>(db: &Db, pk: P) -> Result<Option<E::Model>, DataError>
where
E: sea_orm::EntityTrait,
P: Into<<E::PrimaryKey as sea_orm::PrimaryKeyTrait>::ValueType> + Send,
{
E::find_by_id(pk)
.one(db.orm())
.await
.map_err(DataError::from)
}