52 lines
1.3 KiB
Rust
52 lines
1.3 KiB
Rust
use sea_orm::*;
|
|
|
|
use crate::db::*;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct RepoQuery {
|
|
conn: DatabaseConnection,
|
|
}
|
|
|
|
impl RepoQuery {
|
|
pub fn new(conn: DatabaseConnection) -> Self {
|
|
Self { conn }
|
|
}
|
|
|
|
pub async fn page(&self, per_page: u64, page: u64) -> Result<(u64, Vec<repo::Model>)> {
|
|
let paginator = Repo::find()
|
|
.order_by_asc(repo::Column::Id)
|
|
.paginate(&self.conn, per_page);
|
|
let repos = paginator.fetch_page(page).await?;
|
|
let total_pages = paginator.num_pages().await?;
|
|
|
|
Ok((total_pages, repos))
|
|
}
|
|
|
|
pub async fn by_id(&self, id: i32) -> Result<Option<repo::Model>> {
|
|
repo::Entity::find_by_id(id).one(&self.conn).await
|
|
}
|
|
|
|
pub async fn by_name(&self, name: &str) -> Result<Option<repo::Model>> {
|
|
Repo::find()
|
|
.filter(repo::Column::Name.eq(name))
|
|
.one(&self.conn)
|
|
.await
|
|
}
|
|
|
|
pub async fn insert(
|
|
&self,
|
|
name: &str,
|
|
description: Option<&str>,
|
|
) -> Result<InsertResult<repo::ActiveModel>> {
|
|
let model = repo::ActiveModel {
|
|
id: NotSet,
|
|
// TODO CHANGE THIS
|
|
distro_id: NotSet,
|
|
name: Set(String::from(name)),
|
|
description: Set(description.map(String::from)),
|
|
};
|
|
|
|
Repo::insert(model).exec(&self.conn).await
|
|
}
|
|
}
|