Skip to content

Commit 3bb07a7

Browse files
committed
controllers/version/metadata: Move get_version_dependencies() to dedicated module
1 parent 3bc7e27 commit 3bb07a7

File tree

4 files changed

+48
-38
lines changed

4 files changed

+48
-38
lines changed

src/controllers/version.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod dependencies;
12
pub mod downloads;
23
pub mod metadata;
34
pub mod readme;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use super::CrateVersionPath;
2+
use crate::app::AppState;
3+
use crate::models::Dependency;
4+
use crate::util::errors::AppResult;
5+
use crate::views::EncodableDependency;
6+
use axum_extra::json;
7+
use axum_extra::response::ErasedJson;
8+
use crates_io_database::schema::{crates, dependencies};
9+
use diesel::prelude::*;
10+
use diesel_async::RunQueryDsl;
11+
12+
/// Get crate version dependencies.
13+
///
14+
/// This information can also be obtained directly from the index.
15+
///
16+
/// In addition to returning cached data from the index, this returns
17+
/// fields for `id`, `version_id`, and `downloads` (which appears to always
18+
/// be 0)
19+
#[utoipa::path(
20+
get,
21+
path = "/api/v1/crates/{name}/{version}/dependencies",
22+
params(CrateVersionPath),
23+
tag = "versions",
24+
responses((status = 200, description = "Successful Response")),
25+
)]
26+
pub async fn get_version_dependencies(
27+
state: AppState,
28+
path: CrateVersionPath,
29+
) -> AppResult<ErasedJson> {
30+
let mut conn = state.db_read().await?;
31+
let version = path.load_version(&mut conn).await?;
32+
33+
let deps = Dependency::belonging_to(&version)
34+
.inner_join(crates::table)
35+
.select((Dependency::as_select(), crates::name))
36+
.order((dependencies::optional, crates::name))
37+
.load::<(Dependency, String)>(&mut conn)
38+
.await?
39+
.into_iter()
40+
.map(|(dep, crate_name)| EncodableDependency::from_dep(dep, &crate_name))
41+
.collect::<Vec<_>>();
42+
43+
Ok(json!({ "dependencies": deps }))
44+
}

src/controllers/version/metadata.rs

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use axum::Json;
88
use axum_extra::json;
99
use axum_extra::response::ErasedJson;
10-
use crates_io_database::schema::{crates, dependencies};
1110
use crates_io_worker::BackgroundJob;
1211
use diesel::prelude::*;
1312
use diesel_async::{AsyncPgConnection, RunQueryDsl};
@@ -19,12 +18,12 @@ use crate::app::AppState;
1918
use crate::auth::{AuthCheck, Authentication};
2019
use crate::models::token::EndpointScope;
2120
use crate::models::{
22-
Crate, Dependency, NewVersionOwnerAction, Rights, Version, VersionAction, VersionOwnerAction,
21+
Crate, NewVersionOwnerAction, Rights, Version, VersionAction, VersionOwnerAction,
2322
};
2423
use crate::rate_limiter::LimitedAction;
2524
use crate::schema::versions;
2625
use crate::util::errors::{bad_request, custom, AppResult};
27-
use crate::views::{EncodableDependency, EncodableVersion};
26+
use crate::views::EncodableVersion;
2827
use crate::worker::jobs::{SyncToGitIndex, SyncToSparseIndex, UpdateDefaultVersion};
2928

3029
use super::CrateVersionPath;
@@ -39,40 +38,6 @@ pub struct VersionUpdateRequest {
3938
version: VersionUpdate,
4039
}
4140

42-
/// Get crate version dependencies.
43-
///
44-
/// This information can also be obtained directly from the index.
45-
///
46-
/// In addition to returning cached data from the index, this returns
47-
/// fields for `id`, `version_id`, and `downloads` (which appears to always
48-
/// be 0)
49-
#[utoipa::path(
50-
get,
51-
path = "/api/v1/crates/{name}/{version}/dependencies",
52-
params(CrateVersionPath),
53-
tag = "versions",
54-
responses((status = 200, description = "Successful Response")),
55-
)]
56-
pub async fn get_version_dependencies(
57-
state: AppState,
58-
path: CrateVersionPath,
59-
) -> AppResult<ErasedJson> {
60-
let mut conn = state.db_read().await?;
61-
let version = path.load_version(&mut conn).await?;
62-
63-
let deps = Dependency::belonging_to(&version)
64-
.inner_join(crates::table)
65-
.select((Dependency::as_select(), crates::name))
66-
.order((dependencies::optional, crates::name))
67-
.load::<(Dependency, String)>(&mut conn)
68-
.await?
69-
.into_iter()
70-
.map(|(dep, crate_name)| EncodableDependency::from_dep(dep, &crate_name))
71-
.collect::<Vec<_>>();
72-
73-
Ok(json!({ "dependencies": deps }))
74-
}
75-
7641
/// Get crate version authors.
7742
///
7843
/// This endpoint was deprecated by [RFC #3052](https://github.com/rust-lang/rfcs/pull/3052)

src/router.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn build_axum_router(state: AppState) -> Router<()> {
3838
version::metadata::update_version
3939
))
4040
.routes(routes!(version::readme::get_version_readme))
41-
.routes(routes!(version::metadata::get_version_dependencies))
41+
.routes(routes!(version::dependencies::get_version_dependencies))
4242
.routes(routes!(version::downloads::get_version_downloads))
4343
.routes(routes!(version::metadata::get_version_authors))
4444
.routes(routes!(krate::downloads::get_crate_downloads))

0 commit comments

Comments
 (0)