Skip to content

Commit 63e52f1

Browse files
committed
controllers/krate/metadata: Extract load_recent_downloads() fn
1 parent 01311d9 commit 63e52f1

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/controllers/krate/metadata.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,7 @@ pub async fn find_crate(
148148

149149
let kws = load_keywords(&mut conn, &krate, include.keywords).await?;
150150
let cats = load_categories(&mut conn, &krate, include.categories).await?;
151-
let recent_downloads = if include.downloads {
152-
RecentCrateDownloads::belonging_to(&krate)
153-
.select(recent_crate_downloads::downloads)
154-
.get_result(&mut conn)
155-
.await
156-
.optional()?
157-
} else {
158-
None
159-
};
151+
let recent_downloads = load_recent_downloads(&mut conn, &krate, include.downloads).await?;
160152

161153
let top_versions = if let Some(versions) = versions_publishers_and_audit_actions
162154
.as_ref()
@@ -281,6 +273,21 @@ fn load_categories<'a>(
281273
async move { Ok(Some(fut.await?)) }.boxed()
282274
}
283275

276+
fn load_recent_downloads<'a>(
277+
conn: &mut AsyncPgConnection,
278+
krate: &'a Crate,
279+
includes: bool,
280+
) -> BoxFuture<'a, AppResult<Option<i64>>> {
281+
if !includes {
282+
return always_ready(|| Ok(None)).boxed();
283+
}
284+
285+
let fut = RecentCrateDownloads::belonging_to(&krate)
286+
.select(recent_crate_downloads::downloads)
287+
.get_result(conn);
288+
async move { Ok(fut.await.optional()?) }.boxed()
289+
}
290+
284291
fn _load_versions_and_publishers<'a>(
285292
conn: &mut AsyncPgConnection,
286293
krate: &'a Crate,

0 commit comments

Comments
 (0)