feat: only return committed packages from the api

concurrent-repos
Jef Roosens 2024-06-19 22:07:30 +02:00
parent 730ae009b0
commit 76395afb10
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
6 changed files with 41 additions and 37 deletions

View File

@ -22,10 +22,10 @@ async fn get_repos(
Query(pagination): Query<pagination::Query>,
Query(filter): Query<db::query::repo::Filter>,
) -> crate::Result<Json<PaginatedResponse<db::repo::Model>>> {
let (total_pages, items) =
let items =
db::query::repo::page(&global.db, pagination.per_page, pagination.page - 1, filter).await?;
Ok(Json(pagination.res(total_pages, items)))
Ok(Json(pagination.res(items)))
}
async fn get_single_repo(
@ -44,11 +44,11 @@ async fn get_packages(
Query(pagination): Query<pagination::Query>,
Query(filter): Query<db::query::package::Filter>,
) -> crate::Result<Json<PaginatedResponse<db::package::Model>>> {
let (total_pages, pkgs) =
let items =
db::query::package::page(&global.db, pagination.per_page, pagination.page - 1, filter)
.await?;
Ok(Json(pagination.res(total_pages, pkgs)))
Ok(Json(pagination.res(items)))
}
async fn get_single_package(

View File

@ -1,19 +1,19 @@
use serde::{Deserialize, Serialize};
#[derive(Deserialize)]
#[serde(default)]
pub struct Query {
#[serde(default = "default_page")]
pub page: u64,
#[serde(default = "default_per_page")]
pub per_page: u64,
}
impl Default for Query {
fn default() -> Self {
Query {
page: 1,
per_page: 25,
}
}
fn default_page() -> u64 {
1
}
fn default_per_page() -> u64 {
25
}
#[derive(Serialize)]
@ -23,21 +23,15 @@ where
{
pub page: u64,
pub per_page: u64,
pub total_pages: u64,
pub count: usize,
pub items: Vec<T>,
}
impl Query {
pub fn res<T: for<'de> Serialize>(
self,
total_pages: u64,
items: Vec<T>,
) -> PaginatedResponse<T> {
pub fn res<T: for<'de> Serialize>(self, items: Vec<T>) -> PaginatedResponse<T> {
PaginatedResponse {
page: self.page,
per_page: self.per_page,
total_pages,
count: items.len(),
items,
}

View File

@ -26,6 +26,7 @@ pub struct Model {
pub pgp_sig_size: Option<i64>,
pub sha256_sum: String,
pub compression: String,
#[serde(skip_serializing)]
pub state: PackageState,
}

View File

@ -21,15 +21,14 @@ pub async fn page(
per_page: u64,
page: u64,
filter: Filter,
) -> Result<(u64, Vec<distro::Model>)> {
) -> Result<Vec<distro::Model>> {
let paginator = Distro::find()
.filter(filter)
.order_by_asc(distro::Column::Id)
.paginate(conn, per_page);
let repos = paginator.fetch_page(page).await?;
let total_pages = paginator.num_pages().await?;
Ok((total_pages, repos))
Ok(repos)
}
pub async fn by_id(conn: &DbConn, id: i32) -> Result<Option<distro::Model>> {

View File

@ -17,10 +17,7 @@ impl IntoCondition for Filter {
Condition::all()
.add_option(self.repo.map(|repo| package::Column::RepoId.eq(repo)))
.add_option(self.arch.map(|arch| package::Column::Arch.eq(arch)))
.add_option(
self.name
.map(|name| package::Column::Name.like(format!("%{}%", name))),
)
.add_option(self.name.map(|name| package::Column::Name.contains(name)))
}
}
@ -29,15 +26,29 @@ pub async fn page(
per_page: u64,
page: u64,
filter: Filter,
) -> super::Result<(u64, Vec<package::Model>)> {
let paginator = Package::find()
.filter(filter)
.order_by_asc(package::Column::Id)
.paginate(conn, per_page);
let packages = paginator.fetch_page(page).await?;
let total_pages = paginator.num_pages().await?;
) -> crate::Result<Vec<package::Model>> {
let p2 = Alias::new("p2");
let query = Query::select()
.columns(db::package::Column::iter().map(|c| (db::package::Entity, c)))
.from(db::package::Entity)
.join_subquery(
JoinType::InnerJoin,
max_pkg_ids_query(true),
p2.clone(),
Expr::col((db::package::Entity, db::package::Column::Id))
.eq(Expr::col((p2.clone(), Alias::new("max_id")))),
)
.cond_where(filter)
.order_by((db::package::Entity, db::package::Column::Id), Order::Asc)
.to_owned();
let builder = conn.get_database_backend();
let sql = builder.build(&query);
Ok((total_pages, packages))
Ok(db::Package::find()
.from_raw_sql(sql)
.paginate(conn, per_page)
.fetch_page(page)
.await?)
}
pub async fn by_id(conn: &DbConn, id: i32) -> Result<Option<package::Model>> {

View File

@ -21,15 +21,14 @@ pub async fn page(
per_page: u64,
page: u64,
filter: Filter,
) -> Result<(u64, Vec<repo::Model>)> {
) -> Result<Vec<repo::Model>> {
let paginator = Repo::find()
.filter(filter)
.order_by_asc(repo::Column::Id)
.paginate(conn, per_page);
let repos = paginator.fetch_page(page).await?;
let total_pages = paginator.num_pages().await?;
Ok((total_pages, repos))
Ok(repos)
}
pub async fn by_id(conn: &DbConn, id: i32) -> Result<Option<repo::Model>> {