use sea_orm::*; use crate::db::*; #[derive(Clone, Debug)] pub struct DistroQuery { conn: DatabaseConnection, } impl DistroQuery { pub fn new(conn: DatabaseConnection) -> Self { Self { conn } } pub async fn page(&self, per_page: u64, page: u64) -> Result<(u64, Vec)> { let paginator = Distro::find() .order_by_asc(distro::Column::Id) .paginate(&self.conn, per_page); let results = paginator.fetch_page(page).await?; let total_pages = paginator.num_pages().await?; Ok((total_pages, results)) } pub async fn by_id(&self, id: i32) -> Result> { distro::Entity::find_by_id(id).one(&self.conn).await } pub async fn insert( &self, slug: &str, name: &str, description: Option<&str>, url: Option<&str>, ) -> Result> { let model = distro::ActiveModel { id: NotSet, slug: Set(String::from(slug)), name: Set(String::from(name)), description: Set(description.map(String::from)), url: Set(url.map(String::from)), }; Distro::insert(model).exec(&self.conn).await } }