From b9cfbbb420a66296c42fd863fe0bd0fa6f7445c1 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Fri, 30 Aug 2024 19:26:19 +0200 Subject: Add filter for alert by id --- src/graphql/query.rs | 3 ++- src/graphql/types/alert.rs | 20 +++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/graphql/query.rs b/src/graphql/query.rs index 36883e7..0bc5682 100644 --- a/src/graphql/query.rs +++ b/src/graphql/query.rs @@ -48,9 +48,10 @@ impl Query { async fn alerts<'ctx>( &self, ctx: &Context<'ctx>, + #[graphql(desc = "Filter by ID")] id: Option, #[graphql(desc = "Limit results")] limit: Option, #[graphql(desc = "Offset results")] offset: Option, ) -> Result>, String> { - alert::get_alerts(ctx, limit, offset).await + alert::get_alerts(ctx, id, limit, offset).await } } diff --git a/src/graphql/types/alert.rs b/src/graphql/types/alert.rs index 9c21013..970864f 100644 --- a/src/graphql/types/alert.rs +++ b/src/graphql/types/alert.rs @@ -87,6 +87,9 @@ pub struct AlertInput { pub async fn get_alerts<'ctx>( ctx: &Context<'ctx>, + // Optional filter by id. + id: Option, + // Optional limit results limit: Option, @@ -99,8 +102,17 @@ pub async fn get_alerts<'ctx>( match auth { Authentication::NotLogged => Err("Unauthorized".to_string()), Authentication::Logged(_) => { - let rows = client - .query( + let rows= + match id { + Some(id) => client.query( + "SELECT id, user_id, extract(epoch from created_at)::double precision as created_at, ST_AsText(area) as area, level, reached_users + FROM alerts + WHERE id = $1", + &[&id], + ) + .await + .unwrap(), + None => client.query( "SELECT id, user_id, extract(epoch from created_at)::double precision as created_at, ST_AsText(area) as area, level, reached_users FROM alerts ORDER BY id DESC @@ -109,7 +121,9 @@ pub async fn get_alerts<'ctx>( &[&limit.unwrap_or(20), &offset.unwrap_or(0)], ) .await - .unwrap(); + .unwrap() + + }; let positions: Vec = rows .iter() -- cgit v1.2.3-18-g5258