Skip to content

Commit

Permalink
added filters to source endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Goran Sterjov committed Jan 11, 2024
1 parent d149319 commit 0c3f57b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
25 changes: 17 additions & 8 deletions server/src/database/sources.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use arga_core::models::Taxon;
use arga_core::models::FilteredTaxon;
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use uuid::Uuid;

use crate::database::Error;
use crate::database::extensions::filters::{with_filters, Filter};

use super::extensions::Paginate;
use super::{schema, PgPool, PageResult};
use super::{schema, schema_gnl, PgPool, PageResult};
use super::models::{Source, Dataset};


Expand Down Expand Up @@ -73,8 +74,9 @@ impl SourceProvider {
Ok(records)
}

pub async fn species(&self, source: &Source, page: i64, page_size: i64) -> PageResult<Taxon> {
use schema::{datasets, taxa, names, name_attributes as attrs};
pub async fn species(&self, source: &Source, filters: &Vec<Filter>, page: i64, page_size: i64) -> PageResult<FilteredTaxon> {
use schema::{datasets, names, name_attributes as attrs};
use schema_gnl::taxa_filter;
let mut conn = self.pool.get().await?;

let with_source = names::table
Expand All @@ -85,12 +87,19 @@ impl SourceProvider {
.group_by(names::id)
.into_boxed();

let species = taxa::table
.filter(taxa::name_id.eq_any(with_source))
.order_by(taxa::scientific_name)
let mut species = taxa_filter::table
.filter(taxa_filter::name_id.eq_any(with_source))
.into_boxed();

if let Some(filters) = with_filters(&filters) {
species = species.filter(filters);
}

let species = species
.order_by(taxa_filter::scientific_name)
.paginate(page)
.per_page(page_size)
.load::<(Taxon, i64)>(&mut conn)
.load::<(FilteredTaxon, i64)>(&mut conn)
.await?;

Ok(species.into())
Expand Down
4 changes: 2 additions & 2 deletions server/src/http/graphql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ impl Query {
Source::all(&state.database).await
}

async fn source(&self, ctx: &Context<'_>, by: source::SourceBy) -> Result<Source, Error> {
async fn source(&self, ctx: &Context<'_>, by: source::SourceBy, filters: Option<Vec<FilterItem>>) -> Result<Source, Error> {
let state = ctx.data::<State>().unwrap();
Source::new(&state.database, &by).await
Source::new(&state.database, &by, filters.unwrap_or_default()).await
}

async fn dataset(&self, ctx: &Context<'_>, by: dataset::DatasetBy) -> Result<Dataset, Error> {
Expand Down
12 changes: 7 additions & 5 deletions server/src/http/graphql/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use serde::Deserialize;
use serde::Serialize;
use uuid::Uuid;

use crate::database::extensions::filters::Filter;
use crate::http::Error;
use crate::http::Context as State;
use crate::database::Database;

use super::common::{Page, SpeciesCard};
use super::common::{Page, SpeciesCard, FilterItem, convert_filters};
use super::dataset::DatasetDetails;
use super::helpers::SpeciesHelper;

Expand All @@ -24,13 +25,13 @@ pub enum SourceBy {
pub struct Source(SourceDetails, SourceQuery);

impl Source {
pub async fn new(db: &Database, by: &SourceBy) -> Result<Source, Error> {
pub async fn new(db: &Database, by: &SourceBy, filters: Vec<FilterItem>) -> Result<Source, Error> {
let source = match by {
SourceBy::Id(id) => db.sources.find_by_id(id).await?,
SourceBy::Name(name) => db.sources.find_by_name(name).await?,
};
let details = source.clone().into();
let query = SourceQuery { source };
let query = SourceQuery { source, filters: convert_filters(filters)? };
Ok(Source(details, query))
}

Expand All @@ -44,6 +45,7 @@ impl Source {

pub struct SourceQuery {
source: models::Source,
filters: Vec<Filter>,
}

#[Object]
Expand All @@ -59,8 +61,8 @@ impl SourceQuery {
let state = ctx.data::<State>().unwrap();
let helper = SpeciesHelper::new(&state.database);

let page = state.database.sources.species(&self.source, page, page_size).await?;
let cards = helper.cards(page.records).await?;
let page = state.database.sources.species(&self.source, &self.filters, page, page_size).await?;
let cards = helper.filtered_cards(page.records).await?;

Ok(Page {
records: cards,
Expand Down

0 comments on commit 0c3f57b

Please sign in to comment.