about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorPjotr Prins <pjotr.public01@thebird.nl>2026-11-17 12:00:19 +0000
committerPjotr Prins <pjotr.public01@thebird.nl>2026-11-17 12:00:19 +0000
commitb055c240c909e5c3628cc341fe1a2cbae9ed07f6 (patch)
tree8061143d7b03c7927ba93116b82699d9d58613b8 /src
parenta3d67c72dfa78840eb846f5d93f6f6326ac32b32 (diff)
downloadgenenetwork4-b055c240c909e5c3628cc341fe1a2cbae9ed07f6.tar.gz
groups/{group}/strains: limit_to / ids_only / names_only, old shape by default
Second of the six, and the same three edits as /groups: the params struct gains
the three fields, the reply type becomes Rows (untagged, so no parameters still
means the array of objects), and the handler shapes the rows where it builds
the envelope, appending a note naming any limit_to value that matched nothing.

Verified on the deployed build:

  GET /groups/BXD/strains                          396 objects, keys as before
  GET /groups/BXD/strains?names_only=true          396 strings, first "B6D2F1"
  GET ...?names_only=true&limit_to=BXD1,BXD2,NOPE  ["BXD1","BXD2"] + note: nope

Read tier after deploy: 24 ok, 0 failed.
Diffstat (limited to 'src')
-rw-r--r--src/routes/groups.rs67
1 files changed, 53 insertions, 14 deletions
diff --git a/src/routes/groups.rs b/src/routes/groups.rs
index eca9574..44fd605 100644
--- a/src/routes/groups.rs
+++ b/src/routes/groups.rs
@@ -251,6 +251,16 @@ pub async fn group_by_name(
 #[derive(serde::Deserialize)]
 pub struct GroupStrainsParams {
     used_for_mapping: Option<bool>,
+    /// Keep only these ids or names, comma-separated. Values that match nothing
+    /// are named in the reply's note.
+    #[serde(default)]
+    pub limit_to: Option<String>,
+    /// Answer with one id per strain instead of objects.
+    #[serde(default)]
+    pub ids_only: Option<bool>,
+    /// Answer with one name per strain instead of objects.
+    #[serde(default)]
+    pub names_only: Option<bool>,
 }
 
 #[utoipa::path(get, path = "/groups/{group}/parents-and-f1s",
@@ -368,7 +378,8 @@ pub async fn group_parental_design(
     ("group" = String, Path, description = "Group InbredSetName, Name, FullName or numeric Id"),
     ("used_for_mapping" = Option<bool>, Query, description = "Only strains/samples used for mapping")
 ), responses(
-    (status = 200, description = "The strains/samples belonging to the group", body = Envelope<Vec<db::Strain>>),
+    (status = 200, description = "The strains/samples belonging to the group; \
+`ids_only`/`names_only` answer one value per strain instead", body = Envelope<crate::listing::Rows>),
     Unauthorized,
     (status = 404, description = "No matching group", body = ApiError),
     (status = 500, description = "Database error", body = ApiError)
@@ -379,8 +390,24 @@ pub async fn group_strains(
     Path(group): Path<String>,
     Query(params): Query<GroupStrainsParams>,
     _token: AuthToken,
-) -> Result<Json<Envelope<Vec<db::Strain>>>, ApiError> {
+) -> Result<Json<Envelope<crate::listing::Rows>>, ApiError> {
     let rid = ctx.request_info();
+    // No listing parameters means the old answer: `Rows::Objects`.
+    let listing = crate::listing::Listing::parse(&crate::listing::ListingParams {
+        limit_to: params.limit_to.clone(),
+        ids_only: params.ids_only,
+        names_only: params.names_only,
+    })
+    .map_err(|detail| {
+        ApiError::new(
+            StatusCode::UNPROCESSABLE_ENTITY,
+            "invalid_listing",
+            "Invalid listing parameters",
+            detail,
+            "group_strains",
+        )
+        .with_request_id(rid.clone())
+    })?;
     let not_found_name = group.clone();
     let used_for_mapping = params.used_for_mapping;
     tokio::task::spawn_blocking(move || {
@@ -403,18 +430,30 @@ pub async fn group_strains(
     .map_err(|e| ApiError::from_db(e, "group_strains").with_request_id(rid.clone()))?
     .map(|strains| {
         let group = path_segment(&not_found_name);
-        Json(
-            ctx.envelope(strains)
-                .with_next(
-                    &format!("/groups/{group}"),
-                    "The group these strains belong to",
-                )
-                .with_links(crate::suggest::group_links(&not_found_name))
-                .with_next(
-                    &format!("/groups/{group}/case-attributes"),
-                    "Case-attribute names and per-strain values",
-                ),
-        )
+        let rows: Vec<serde_json::Value> = strains
+            .iter()
+            .map(|strain| serde_json::to_value(strain).unwrap_or(serde_json::Value::Null))
+            .collect();
+        let (shaped, missing) = listing.shape(rows, params.limit_to.as_deref());
+        let mut envelope = ctx
+            .envelope(shaped)
+            .with_next(
+                &format!("/groups/{group}"),
+                "The group these strains belong to",
+            )
+            .with_links(crate::suggest::group_links(&not_found_name))
+            .with_next(
+                &format!("/groups/{group}/case-attributes"),
+                "Case-attribute names and per-strain values",
+            );
+        if !missing.is_empty() {
+            envelope = envelope.with_note(&format!(
+                "limit_to named {} value(s) that matched no strain: {}",
+                missing.len(),
+                missing.join(", ")
+            ));
+        }
+        Json(envelope)
     })
     .ok_or_else(|| {
         ApiError::new(