1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
|
//! Groups (InbredSet) domain endpoints: read-only access to the
//! InbredSet table and its strains.
use axum::Json;
use axum::extract::{Path, Query};
use axum::http::StatusCode;
use serde::Deserialize;
use gnauth_macros::gnauth;
use crate::auth::{self, AuthToken, Authorized};
use crate::config::Config;
use crate::db;
use crate::error::{ApiError, Unauthorized};
use crate::response::{Ctx, Envelope, path_segment};
/// Query parameters for `GET /groups`; all optional, combined with AND.
#[derive(Deserialize, utoipa::IntoParams)]
pub struct GroupsParams {
/// Keep groups whose family contains this text, case-insensitively,
/// e.g. "Reference Populations".
pub family: Option<String>,
/// Keep groups of this species, by the short name /species lists
/// (case-insensitive), e.g. "mouse".
pub species: Option<String>,
/// Keep groups whose name, display name or full name contains this
/// text, case-insensitively, e.g. "alzheimer".
pub q: Option<String>,
}
fn contains_ci(field: &Option<String>, needle: &str) -> bool {
field
.as_deref()
.is_some_and(|f| f.to_lowercase().contains(&needle.to_lowercase()))
}
#[utoipa::path(get, path = "/groups", summary = "List all groups (populations; InbredSet rows), optionally filtered", params(GroupsParams), responses(
(status = 200, description = "The groups (InbredSet rows), with family and species name", body = Envelope<Vec<db::Group>>),
Unauthorized,
(status = 500, description = "Database error", body = ApiError)
))]
#[gnauth(token_only)]
pub async fn groups(
ctx: Ctx,
_token: AuthToken,
Query(params): Query<GroupsParams>,
) -> Result<Json<Envelope<Vec<db::Group>>>, ApiError> {
let rid = ctx.request_info();
tokio::task::spawn_blocking(db::get_all_groups)
.await
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"internal_error",
"Internal error",
e.to_string(),
"groups",
)
.with_request_id(rid.clone())
})?
.map(|groups| {
let groups: Vec<db::Group> = groups
.into_iter()
.filter(|g| params.family.as_ref().is_none_or(|f| contains_ci(&g.family, f)))
.filter(|g| {
params
.species
.as_ref()
.is_none_or(|s| g.species.as_deref().is_some_and(|n| n.eq_ignore_ascii_case(s)))
})
.filter(|g| {
params.q.as_ref().is_none_or(|q| {
contains_ci(&Some(g.name.clone()), q)
|| contains_ci(&g.display_name, q)
|| contains_ci(&g.full_name, q)
})
})
.collect();
Json(ctx.envelope(groups).with_next(
"/species",
"The species the groups belong to (filter groups with ?species=<name>)",
))
})
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"database_error",
"Database error",
e.to_string(),
"groups",
)
.with_request_id(rid)
})
}
#[utoipa::path(get, path = "/groups/{group}", summary = "Get one group's full record by name or numeric id", params(
("group" = String, Path, description = "Group InbredSetName, Name, FullName or numeric Id")
), responses(
(status = 200, description = "The matching row of the InbredSet table", body = Envelope<db::GroupDetail>),
Unauthorized,
(status = 404, description = "No matching group", body = ApiError),
(status = 500, description = "Database error", body = ApiError)
))]
#[gnauth(token_only)]
pub async fn group_by_name(
ctx: Ctx,
Path(group): Path<String>,
_token: AuthToken,
) -> Result<Json<Envelope<db::GroupDetail>>, ApiError> {
let rid = ctx.request_info();
let not_found_name = group.clone();
tokio::task::spawn_blocking(move || db::get_group_by_name(&group))
.await
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"internal_error",
"Internal error",
e.to_string(),
"group_by_name",
)
.with_request_id(rid.clone())
})?
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"database_error",
"Database error",
e.to_string(),
"group_by_name",
)
.with_request_id(rid.clone())
})?
.map(|detail| {
let name = path_segment(&detail.name);
Json(
ctx.envelope(detail)
.with_next(&format!("/groups/{name}/strains"), "The group's strains/samples")
.with_next(
&format!("/groups/{name}/case-attributes"),
"Case-attribute names and per-strain values",
)
.with_next(
&format!("/groups/{name}/datasets"),
"Expression (ProbeSet) datasets of the group",
)
.with_next(
&format!("/groups/{name}/traits"),
"Published (phenotype) traits of the group (can be very large)",
),
)
})
.ok_or_else(|| {
ApiError::new(
StatusCode::NOT_FOUND,
"group_not_found",
"Group not found",
format!("No group matching '{not_found_name}'."),
"group_by_name",
)
.with_request_id(rid)
.with_next("/groups")
})
}
#[derive(serde::Deserialize)]
pub struct GroupStrainsParams {
used_for_mapping: Option<bool>,
}
#[utoipa::path(get, path = "/groups/{group}/strains", summary = "List the strains/samples belonging to a group", params(
("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>>),
Unauthorized,
(status = 404, description = "No matching group", body = ApiError),
(status = 500, description = "Database error", body = ApiError)
))]
#[gnauth(token_only)]
pub async fn group_strains(
ctx: Ctx,
Path(group): Path<String>,
Query(params): Query<GroupStrainsParams>,
_token: AuthToken,
) -> Result<Json<Envelope<Vec<db::Strain>>>, ApiError> {
let rid = ctx.request_info();
let not_found_name = group.clone();
let used_for_mapping = params.used_for_mapping;
tokio::task::spawn_blocking(move || {
db::get_group_by_name(&group).and_then(|group| match group {
Some(group) => db::get_group_strains(group.id, used_for_mapping).map(Some),
None => Ok(None),
})
})
.await
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"internal_error",
"Internal error",
e.to_string(),
"group_strains",
)
.with_request_id(rid.clone())
})?
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"database_error",
"Database error",
e.to_string(),
"group_strains",
)
.with_request_id(rid.clone())
})?
.map(|strains| {
let group = path_segment(¬_found_name);
Json(
ctx.envelope(strains)
.with_next(&format!("/groups/{group}"), "The group these strains belong to")
.with_next(
&format!("/groups/{group}/case-attributes"),
"Case-attribute names and per-strain values",
),
)
})
.ok_or_else(|| {
ApiError::new(
StatusCode::NOT_FOUND,
"group_not_found",
"Group not found",
format!("No group matching '{not_found_name}'."),
"group_strains",
)
.with_request_id(rid)
.with_next("/groups")
})
}
/// What looking up a trait within a group found, shared by
/// group_trait_values and group_trait_info so each can tell a missing
/// group from a missing trait and return the right 404. `Forbidden`
/// mirrors src/routes/traits.rs and src/routes/datasets.rs: the same
/// published (phenotype) data is reachable there via
/// /traits/{dataset}/... with a gn-auth check, so it needs one here
/// too (this was a known gap -- see docs/gn-auth.md).
enum GroupTraitLookup<T> {
GroupNotFound,
TraitNotFound,
Forbidden,
Found(Authorized, T),
}
/// Either a database error, or a failure to reach/parse gn-auth's
/// answer while checking authorization for a non-public group.
enum GroupAuthError {
Db(mysql::Error),
Auth(String),
}
impl From<mysql::Error> for GroupAuthError {
fn from(e: mysql::Error) -> Self {
GroupAuthError::Db(e)
}
}
/// Resolve `group`'s PublishFreeze row and run the same
/// public/confidentiality/create_time-gated gn-auth check
/// src/routes/traits.rs applies when the same data is reached via
/// /traits/{dataset}/... When a group has published traits
/// (PublishXRef rows) with no PublishFreeze row to check -- not
/// expected, but PublishXRef/PublishFreeze aren't tied by a foreign
/// key -- this denies rather than treating ungated data as public.
fn check_group_trait_authorized(
group: &db::GroupDetail,
trait_name: Option<&str>,
auth_server_url: &str,
token: &AuthToken,
) -> Result<Option<Authorized>, GroupAuthError> {
let Some(pf) = db::get_publish_freeze_by_name(&group.name)? else {
return Ok(None);
};
auth::check(
auth_server_url,
token,
&pf.name,
trait_name,
pf.public,
pf.confidentiality,
&pf.create_time,
)
.map_err(GroupAuthError::Auth)
}
fn get_group_trait_values_result(
group: &str,
trait_id: u32,
auth_server_url: &str,
token: &AuthToken,
) -> Result<GroupTraitLookup<Vec<db::TraitValue>>, GroupAuthError> {
let Some(group) = db::get_group_by_name(group)? else {
return Ok(GroupTraitLookup::GroupNotFound);
};
let values = db::get_group_trait_values(group.id, trait_id)?;
if values.is_empty() {
return Ok(GroupTraitLookup::TraitNotFound);
}
let trait_id_string = trait_id.to_string();
let Some(auth) =
check_group_trait_authorized(&group, Some(&trait_id_string), auth_server_url, token)?
else {
return Ok(GroupTraitLookup::Forbidden);
};
Ok(GroupTraitLookup::Found(auth, values))
}
#[utoipa::path(get, path = "/groups/{group}/traits/{trait_id}/values", summary = "Get one published trait's value in each of a group's strains", params(
("group" = String, Path, description = "Group InbredSetName, Name, FullName or numeric Id"),
("trait_id" = u32, Path, description = "PublishXRef.Id of a published (phenotype) trait")
), responses(
(status = 200, description = "The group's per-strain values for the trait", body = Envelope<Vec<db::TraitValue>>),
Unauthorized,
(status = 403, description = "The trait's dataset isn't public, and the caller isn't authorized for it", body = ApiError),
(status = 404, description = "No matching group, or no matching trait in that group", body = ApiError),
(status = 500, description = "Database error", body = ApiError),
(status = 502, description = "Could not reach gn-auth to check authorization", body = ApiError)
))]
#[gnauth(secured)]
pub async fn group_trait_values(
ctx: Ctx,
Path((group, trait_id)): Path<(String, u32)>,
token: AuthToken,
) -> Result<Json<Envelope<Vec<db::TraitValue>>>, ApiError> {
let rid = ctx.request_info();
let not_found_group = group.clone();
let auth_server_url = Config::load().auth_server_url.clone();
tokio::task::spawn_blocking(move || {
get_group_trait_values_result(&group, trait_id, &auth_server_url, &token)
})
.await
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"internal_error",
"Internal error",
e.to_string(),
"group_trait_values",
)
.with_request_id(rid.clone())
})?
.map_err(|e| match e {
GroupAuthError::Db(e) => ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"database_error",
"Database error",
e.to_string(),
"group_trait_values",
)
.with_request_id(rid.clone()),
GroupAuthError::Auth(e) => ApiError::new(
StatusCode::BAD_GATEWAY,
"auth_server_error",
"Could not check authorization",
e,
"group_trait_values",
)
.with_request_id(rid.clone()),
})
.and_then(|lookup| match lookup {
GroupTraitLookup::Found(authorized, values) => {
let _: Authorized = authorized;
let group = path_segment(¬_found_group);
Ok(Json(ctx.envelope(values).with_next(
&format!("/groups/{group}/traits/{trait_id}"),
"The trait's metadata",
)))
}
GroupTraitLookup::Forbidden => Err(ApiError::new(
StatusCode::FORBIDDEN,
"forbidden",
"Not authorized",
format!(
"Trait {trait_id} in group '{not_found_group}' is not public, \
and you are not authorized to view it."
),
"group_trait_values",
)
.with_request_id(rid.clone())),
GroupTraitLookup::GroupNotFound => Err(ApiError::new(
StatusCode::NOT_FOUND,
"group_not_found",
"Group not found",
format!("No group matching '{not_found_group}'."),
"group_trait_values",
)
.with_request_id(rid.clone())
.with_next("/groups")),
GroupTraitLookup::TraitNotFound => Err(ApiError::new(
StatusCode::NOT_FOUND,
"trait_not_found",
"Trait not found",
format!("No trait {trait_id} in group '{not_found_group}'."),
"group_trait_values",
)
.with_request_id(rid)),
})
}
fn get_group_trait_info_result(
group: &str,
trait_id: u32,
auth_server_url: &str,
token: &AuthToken,
) -> Result<GroupTraitLookup<db::TraitInfo>, GroupAuthError> {
let Some(group) = db::get_group_by_name(group)? else {
return Ok(GroupTraitLookup::GroupNotFound);
};
let Some(info) = db::get_group_trait_info(group.id, trait_id)? else {
return Ok(GroupTraitLookup::TraitNotFound);
};
let trait_id_string = trait_id.to_string();
let Some(auth) =
check_group_trait_authorized(&group, Some(&trait_id_string), auth_server_url, token)?
else {
return Ok(GroupTraitLookup::Forbidden);
};
Ok(GroupTraitLookup::Found(auth, info))
}
#[utoipa::path(get, path = "/groups/{group}/traits/{trait_id}", summary = "Get one published trait's PublishXRef/Phenotype/Publication metadata", params(
("group" = String, Path, description = "Group InbredSetName, Name, FullName or numeric Id"),
("trait_id" = u32, Path, description = "PublishXRef.Id of a published (phenotype) trait")
), responses(
(status = 200, description = "The trait's PublishXRef/Phenotype/Publication metadata", body = Envelope<db::TraitInfo>),
Unauthorized,
(status = 403, description = "The trait's dataset isn't public, and the caller isn't authorized for it", body = ApiError),
(status = 404, description = "No matching group, or no matching trait in that group", body = ApiError),
(status = 500, description = "Database error", body = ApiError),
(status = 502, description = "Could not reach gn-auth to check authorization", body = ApiError)
))]
pub async fn group_trait_info(
ctx: Ctx,
Path((group, trait_id)): Path<(String, u32)>,
token: AuthToken,
) -> Result<Json<Envelope<db::TraitInfo>>, ApiError> {
let rid = ctx.request_info();
let not_found_group = group.clone();
let auth_server_url = Config::load().auth_server_url.clone();
tokio::task::spawn_blocking(move || {
get_group_trait_info_result(&group, trait_id, &auth_server_url, &token)
})
.await
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"internal_error",
"Internal error",
e.to_string(),
"group_trait_info",
)
.with_request_id(rid.clone())
})?
.map_err(|e| match e {
GroupAuthError::Db(e) => ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"database_error",
"Database error",
e.to_string(),
"group_trait_info",
)
.with_request_id(rid.clone()),
GroupAuthError::Auth(e) => ApiError::new(
StatusCode::BAD_GATEWAY,
"auth_server_error",
"Could not check authorization",
e,
"group_trait_info",
)
.with_request_id(rid.clone()),
})
.and_then(|lookup| match lookup {
GroupTraitLookup::Found(_authorized, info) => {
let group = path_segment(¬_found_group);
let pubmed_id = info.pubmed_id;
let mut envelope = ctx
.envelope(info)
.with_note(db::LOD_NOTE)
.with_next(
&format!("/groups/{group}/traits/{trait_id}/values"),
"The trait's value in each strain",
)
.with_next(&format!("/groups/{group}"), "The group this trait belongs to");
if let Some(pubmed_id) = pubmed_id {
envelope = envelope.with_next(
&format!("/publications/{pubmed_id}/traits"),
"Every trait reported by this trait's publication",
);
}
Ok(Json(envelope))
}
GroupTraitLookup::Forbidden => Err(ApiError::new(
StatusCode::FORBIDDEN,
"forbidden",
"Not authorized",
format!(
"Trait {trait_id} in group '{not_found_group}' is not public, \
and you are not authorized to view it."
),
"group_trait_info",
)
.with_request_id(rid.clone())),
GroupTraitLookup::GroupNotFound => Err(ApiError::new(
StatusCode::NOT_FOUND,
"group_not_found",
"Group not found",
format!("No group matching '{not_found_group}'."),
"group_trait_info",
)
.with_request_id(rid.clone())
.with_next("/groups")),
GroupTraitLookup::TraitNotFound => Err(ApiError::new(
StatusCode::NOT_FOUND,
"trait_not_found",
"Trait not found",
format!("No trait {trait_id} in group '{not_found_group}'."),
"group_trait_info",
)
.with_request_id(rid)),
})
}
/// Query parameters for `GET /groups/{group}/traits`.
#[derive(Deserialize, utoipa::IntoParams)]
pub struct GroupTraitsParams {
/// Only the traits with this short name (abbreviation), e.g.
/// "5HT_MB_M" or "3NP_Tox".
pub abbreviation: Option<String>,
}
enum GroupTraitsLookup {
GroupNotFound,
Forbidden,
Found(Vec<db::TraitInfo>),
}
fn get_group_traits_result(
group: &str,
abbreviation: Option<&str>,
auth_server_url: &str,
token: &AuthToken,
) -> Result<GroupTraitsLookup, GroupAuthError> {
let Some(group) = db::get_group_by_name(group)? else {
return Ok(GroupTraitsLookup::GroupNotFound);
};
let traits = match abbreviation {
Some(abbreviation) => db::get_group_traits_by_abbreviation(group.id, abbreviation)?,
None => db::get_group_traits(group.id)?,
};
// Most groups have no published (phenotype) dataset at all --
// nothing here to protect, so don't spend a gn-auth round trip
// (or deny) on an empty list.
if traits.is_empty() {
return Ok(GroupTraitsLookup::Found(traits));
}
let Some(_auth) = check_group_trait_authorized(&group, None, auth_server_url, token)? else {
return Ok(GroupTraitsLookup::Forbidden);
};
Ok(GroupTraitsLookup::Found(traits))
}
#[utoipa::path(get, path = "/groups/{group}/traits", summary = "List the published (phenotype) traits belonging to a group", params(
("group" = String, Path, description = "Group InbredSetName, Name, FullName or numeric Id"),
GroupTraitsParams
), responses(
(status = 200, description = "The published traits belonging to the group", body = Envelope<Vec<db::TraitInfo>>),
Unauthorized,
(status = 403, description = "The group's published data isn't public, and the caller isn't authorized for it", body = ApiError),
(status = 404, description = "No matching group", body = ApiError),
(status = 500, description = "Database error", body = ApiError),
(status = 502, description = "Could not reach gn-auth to check authorization", body = ApiError)
))]
pub async fn group_traits(
ctx: Ctx,
Path(group): Path<String>,
token: AuthToken,
Query(params): Query<GroupTraitsParams>,
) -> Result<Json<Envelope<Vec<db::TraitInfo>>>, ApiError> {
let rid = ctx.request_info();
let not_found_name = group.clone();
let auth_server_url = Config::load().auth_server_url.clone();
tokio::task::spawn_blocking(move || {
get_group_traits_result(&group, params.abbreviation.as_deref(), &auth_server_url, &token)
})
.await
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"internal_error",
"Internal error",
e.to_string(),
"group_traits",
)
.with_request_id(rid.clone())
})?
.map_err(|e| match e {
GroupAuthError::Db(e) => ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"database_error",
"Database error",
e.to_string(),
"group_traits",
)
.with_request_id(rid.clone()),
GroupAuthError::Auth(e) => ApiError::new(
StatusCode::BAD_GATEWAY,
"auth_server_error",
"Could not check authorization",
e,
"group_traits",
)
.with_request_id(rid.clone()),
})
.and_then(|lookup| match lookup {
GroupTraitsLookup::Found(traits) => {
let group = path_segment(¬_found_name);
Ok(Json(
ctx.envelope(traits)
.with_note(db::LOD_NOTE)
.with_next(&format!("/groups/{group}"), "The group these traits belong to"),
))
}
GroupTraitsLookup::Forbidden => Err(ApiError::new(
StatusCode::FORBIDDEN,
"forbidden",
"Not authorized",
format!(
"Group '{not_found_name}' published data is not public, \
and you are not authorized to view it."
),
"group_traits",
)
.with_request_id(rid.clone())),
GroupTraitsLookup::GroupNotFound => Err(ApiError::new(
StatusCode::NOT_FOUND,
"group_not_found",
"Group not found",
format!("No group matching '{not_found_name}'."),
"group_traits",
)
.with_request_id(rid)
.with_next("/groups")),
})
}
#[utoipa::path(get, path = "/groups/{group}/case-attributes", summary = "Get a group's case attributes: defined attribute names and every strain's values", params(
("group" = String, Path, description = "Group InbredSetName, Name, FullName or numeric Id")
), responses(
(status = 200, description = "The group's case-attribute names and per-strain values", body = Envelope<db::CaseAttributes>),
Unauthorized,
(status = 404, description = "No matching group", body = ApiError),
(status = 500, description = "Database error", body = ApiError)
))]
#[gnauth(token_only)]
pub async fn group_case_attributes(
ctx: Ctx,
Path(group): Path<String>,
_token: AuthToken,
) -> Result<Json<Envelope<db::CaseAttributes>>, ApiError> {
let rid = ctx.request_info();
let not_found_name = group.clone();
tokio::task::spawn_blocking(move || {
db::get_group_by_name(&group).and_then(|group| match group {
Some(group) => db::get_group_case_attributes(group.id).map(Some),
None => Ok(None),
})
})
.await
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"internal_error",
"Internal error",
e.to_string(),
"group_case_attributes",
)
.with_request_id(rid.clone())
})?
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"database_error",
"Database error",
e.to_string(),
"group_case_attributes",
)
.with_request_id(rid.clone())
})?
.map(|case_attributes| {
Json(ctx.envelope(case_attributes).with_next(
&format!("/groups/{}/strains", path_segment(¬_found_name)),
"The group's strains/samples",
))
})
.ok_or_else(|| {
ApiError::new(
StatusCode::NOT_FOUND,
"group_not_found",
"Group not found",
format!("No group matching '{not_found_name}'."),
"group_case_attributes",
)
.with_request_id(rid)
.with_next("/groups")
})
}
#[utoipa::path(get, path = "/groups/{group}/datasets", summary = "List the expression (ProbeSet) datasets belonging to a group", params(
("group" = String, Path, description = "Group InbredSetName, Name, FullName or numeric Id")
), responses(
(status = 200, description = "The expression (ProbeSet) datasets belonging to the group", body = Envelope<Vec<db::Dataset>>),
Unauthorized,
(status = 404, description = "No matching group", body = ApiError),
(status = 500, description = "Database error", body = ApiError)
))]
#[gnauth(token_only)]
pub async fn group_datasets(
ctx: Ctx,
Path(group): Path<String>,
_token: AuthToken,
) -> Result<Json<Envelope<Vec<db::Dataset>>>, ApiError> {
let rid = ctx.request_info();
let not_found_name = group.clone();
tokio::task::spawn_blocking(move || {
db::get_group_by_name(&group).and_then(|group| match group {
Some(group) => db::get_group_datasets(group.id).map(Some),
None => Ok(None),
})
})
.await
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"internal_error",
"Internal error",
e.to_string(),
"group_datasets",
)
.with_request_id(rid.clone())
})?
.map_err(|e| {
ApiError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"database_error",
"Database error",
e.to_string(),
"group_datasets",
)
.with_request_id(rid.clone())
})?
.map(|datasets| {
Json(ctx.envelope(datasets).with_next(
&format!("/groups/{}", path_segment(¬_found_name)),
"The group these datasets belong to",
))
})
.ok_or_else(|| {
ApiError::new(
StatusCode::NOT_FOUND,
"group_not_found",
"Group not found",
format!("No group matching '{not_found_name}'."),
"group_datasets",
)
.with_request_id(rid)
.with_next("/groups")
})
}
|