fix: ensure the list-bags command with no id lists all bags

This commit is contained in:
Jon Seager 2025-11-27 14:32:48 +00:00
parent 57260a71c2
commit 673f76f09a
No known key found for this signature in database
3 changed files with 16 additions and 10 deletions

View file

@ -178,16 +178,7 @@ pub(crate) async fn list_bags(
.list_by_roast(roast_id)
.await
.map_err(AppError::from)?,
None => {
// For API list all, we might want to implement list_all in repo or reuse list with pagination
// For now, let's just return empty or implement list_all if needed.
// The spec implies we need list endpoints.
// Let's implement list_all in repo later if needed, or just use list with large page size?
// Actually, let's just use list_by_roast for now as that's the main use case for API likely.
// Or better, let's add list_all to repo.
// For now, I'll return an error if no filter is provided, or empty list.
vec![]
}
None => state.bag_repo.list_all().await.map_err(AppError::from)?,
};
Ok(Json(bags))
}

View file

@ -136,4 +136,5 @@ pub trait BagRepository: Send + Sync {
&self,
request: &ListRequest<BagSortKey>,
) -> Result<Page<BagWithRoast>, RepositoryError>;
async fn list_all(&self) -> Result<Vec<BagWithRoast>, RepositoryError>;
}

View file

@ -268,6 +268,20 @@ impl BagRepository for SqlBagRepository {
)
.await
}
async fn list_all(&self) -> Result<Vec<BagWithRoast>, RepositoryError> {
let query = format!("{} ORDER BY b.roast_date DESC", BASE_SELECT);
let records = query_as::<_, BagWithRoastRecord>(&query)
.fetch_all(&self.pool)
.await
.map_err(|err| RepositoryError::unexpected(err.to_string()))?;
Ok(records
.into_iter()
.map(Self::to_domain_with_roast)
.collect())
}
}
#[derive(sqlx::FromRow)]