47 lines
1.2 KiB
Rust
47 lines
1.2 KiB
Rust
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<distro::Model>)> {
|
|
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<Option<distro::Model>> {
|
|
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<InsertResult<distro::ActiveModel>> {
|
|
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
|
|
}
|
|
}
|