summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2024-08-30 19:26:19 +0200
committerSanto Cariotti <santo@dcariotti.me>2024-08-30 19:26:19 +0200
commitb9cfbbb420a66296c42fd863fe0bd0fa6f7445c1 (patch)
tree25de6f3707593a342bcdbc926002703c02f665c3
parente118a43c9ba1719be403d6e7a0f8c610319c144c (diff)
Add filter for alert by id
-rw-r--r--src/graphql/query.rs3
-rw-r--r--src/graphql/types/alert.rs20
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<i32>,
#[graphql(desc = "Limit results")] limit: Option<i64>,
#[graphql(desc = "Offset results")] offset: Option<i64>,
) -> Result<Option<Vec<alert::Alert>>, 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<i32>,
+
// Optional limit results
limit: Option<i64>,
@@ -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<Alert> = rows
.iter()