fix: ensure the list-bags command with no id lists all bags
This commit is contained in:
parent
57260a71c2
commit
673f76f09a
3 changed files with 16 additions and 10 deletions
|
|
@ -178,16 +178,7 @@ pub(crate) async fn list_bags(
|
||||||
.list_by_roast(roast_id)
|
.list_by_roast(roast_id)
|
||||||
.await
|
.await
|
||||||
.map_err(AppError::from)?,
|
.map_err(AppError::from)?,
|
||||||
None => {
|
None => state.bag_repo.list_all().await.map_err(AppError::from)?,
|
||||||
// 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![]
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
Ok(Json(bags))
|
Ok(Json(bags))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -136,4 +136,5 @@ pub trait BagRepository: Send + Sync {
|
||||||
&self,
|
&self,
|
||||||
request: &ListRequest<BagSortKey>,
|
request: &ListRequest<BagSortKey>,
|
||||||
) -> Result<Page<BagWithRoast>, RepositoryError>;
|
) -> Result<Page<BagWithRoast>, RepositoryError>;
|
||||||
|
async fn list_all(&self) -> Result<Vec<BagWithRoast>, RepositoryError>;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -268,6 +268,20 @@ impl BagRepository for SqlBagRepository {
|
||||||
)
|
)
|
||||||
.await
|
.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)]
|
#[derive(sqlx::FromRow)]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue