Skip to content

Commit

Permalink
Merge pull request #4829 from solgenomics/topic/sorting_studies
Browse files Browse the repository at this point in the history
enable sortBy on brapi studies
  • Loading branch information
lukasmueller authored Feb 26, 2024
2 parents 86aa110 + 12b3881 commit b2894c3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
26 changes: 26 additions & 0 deletions lib/CXGN/BrAPI/v2/Studies.pm
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,32 @@ sub _search {
my $sort_by = shift;
my $sort_order = shift;

if($sort_order){
if(lc $sort_order eq "ascending"){
$sort_order = ' ASC'
} elsif (lc $sort_order eq "descending"){
$sort_order = ' DESC';
} else{
$sort_order = undef;
return CXGN::BrAPI::JSONResponse->return_error($self->status, "sortOrder valid values are: Ascending or Descending", 400);
}
}

if($sort_by){
my %sort_by_items = (
"locationDbId" => " location.value ",
"studyDbId" => " study.project_id ",
"studyName" => " study.name",
"trialDbId" => " folder.project_id ",
"trialName" => " folder.name "
);
if ($sort_by_items{$sort_by}){
$sort_by = " ORDER BY " . $sort_by_items{$sort_by} ;
} else {
return CXGN::BrAPI::JSONResponse->return_error($self->status, "sortBy valid values are only: locationDbId, studyDbId, studyName, trialDbId or trialName at the moment.", 400);
}
}

# my $c = shift;
my $page_obj = CXGN::Page->new();
my $main_production_site_url = $page_obj->get_hostname();
Expand Down
8 changes: 6 additions & 2 deletions lib/CXGN/Trial/Search.pm
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ sub search {

my $where_clause = scalar(@where_clause)>0 ? " WHERE " . (join (" AND " , @where_clause)) : '';

if(!$sort_by) {
$sort_by = " ORDER BY study.name ";
}

my $q = "SELECT study.name, study.project_id, study.description, folder.name, folder.project_id, folder.description, trial_type_name.cvterm_id, trial_type_name.name, projectprop.value as trial_type_value, year.value, location.value, breeding_program.name, breeding_program.project_id, breeding_program.description, harvest_date.value, planting_date.value, design.value, genotyping_facility.value, genotyping_facility_submitted.value, genotyping_facility_status.value, genotyping_plate_format.value, genotyping_plate_sample_type.value, genotyping_facility_plate_id.value, sampling_facility.value, sampling_facility_sample_type.value, project_additional_info.value, count(study.project_id) OVER() AS full_count, xref.xrefjson
FROM project AS study
JOIN project_relationship AS bp_rel ON(study.project_id=bp_rel.subject_project_id AND bp_rel.type_id=$breeding_program_trial_relationship_id)
Expand Down Expand Up @@ -382,8 +386,8 @@ sub search {
$accession_join
$trait_join
$where_clause
GROUP BY(study.name, study.project_id, study.description, folder.name, folder.project_id, folder.description, trial_type_name.cvterm_id, trial_type_name.name, projectprop.value, year.value, location.value, breeding_program.name, breeding_program.project_id, breeding_program.description, harvest_date.value, planting_date.value, design.value, genotyping_facility.value, genotyping_facility_submitted.value, genotyping_facility_status.value, genotyping_plate_format.value, genotyping_plate_sample_type.value, genotyping_facility_plate_id.value, sampling_facility.value, sampling_facility_sample_type.value, project_additional_info.value, xref.xrefjson)
ORDER BY study.name;";
GROUP BY(study.name, study.project_id, study.description, folder.name, folder.project_id, folder.description, trial_type_name.cvterm_id, trial_type_name.name, projectprop.value, year.value, location.value, breeding_program.name, breeding_program.project_id, breeding_program.description, harvest_date.value, planting_date.value, design.value, genotyping_facility.value, genotyping_facility_submitted.value, genotyping_facility_status.value, genotyping_plate_format.value, genotyping_plate_sample_type.value, genotyping_facility_plate_id.value, sampling_facility.value, sampling_facility_sample_type.value, project_additional_info.value, xref.xrefjson) " .
$sort_by . $order_by . ";";

print STDERR Dumper $q;
my $h = $schema->storage->dbh()->prepare($q);
Expand Down

0 comments on commit b2894c3

Please sign in to comment.