summaryrefslogtreecommitdiff
path: root/src/graphql
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2024-09-01 19:02:31 +0200
committerSanto Cariotti <santo@dcariotti.me>2024-09-01 19:02:31 +0200
commit98931e63f71a088328ffdbf7db34a2ecfacf3da8 (patch)
tree2ae628ad0597c06363d52480915b414b19add81c /src/graphql
parentb9cfbbb420a66296c42fd863fe0bd0fa6f7445c1 (diff)
Add extended area field for alerts
Diffstat (limited to 'src/graphql')
-rw-r--r--src/graphql/mutation.rs26
-rw-r--r--src/graphql/types/alert.rs64
2 files changed, 72 insertions, 18 deletions
diff --git a/src/graphql/mutation.rs b/src/graphql/mutation.rs
index d6c704a..1440ced 100644
--- a/src/graphql/mutation.rs
+++ b/src/graphql/mutation.rs
@@ -154,10 +154,29 @@ impl Mutation {
Err(e) => return Err(e.into()),
};
- let query = format!("INSERT INTO alerts (user_id, area, level)
+ let query = format!(
+ "INSERT INTO alerts (user_id, area, level)
VALUES($1, {}, $2)
- RETURNING id, user_id, extract(epoch from created_at)::double precision as created_at, ST_AsText(area) as area, level, reached_users
- ", polygon);
+ RETURNING
+ id,
+ user_id,
+ extract(epoch from created_at)::double precision as created_at,
+ ST_AsText(area) as area,
+ ST_AsText(
+ ST_Buffer(
+ area::geography,
+ CASE
+ WHEN level = 'One' THEN 0
+ WHEN level = 'Two' THEN 1000
+ WHEN level = 'Three' THEN 2000
+ ELSE 0
+ END
+ )
+ ) as extended_area,
+ level,
+ reached_users",
+ polygon
+ );
match client.query(&query, &[&claims.user_id, &input.level]).await {
Ok(rows) => {
@@ -168,6 +187,7 @@ impl Mutation {
user_id: row.get("user_id"),
created_at: row.get::<_, f64>("created_at") as i64,
area: row.get("area"),
+ extended_area: row.get("extended_area"),
level: row.get("level"),
reached_users: row.get("reached_users"),
})
diff --git a/src/graphql/types/alert.rs b/src/graphql/types/alert.rs
index 970864f..9e2add5 100644
--- a/src/graphql/types/alert.rs
+++ b/src/graphql/types/alert.rs
@@ -66,6 +66,7 @@ pub struct Alert {
pub user_id: i32,
pub created_at: i64,
pub area: String,
+ pub extended_area: String,
pub level: LevelAlert,
pub reached_users: i32,
}
@@ -102,27 +103,59 @@ pub async fn get_alerts<'ctx>(
match auth {
Authentication::NotLogged => Err("Unauthorized".to_string()),
Authentication::Logged(_) => {
- 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
+ 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,
+ ST_AsText(
+ ST_Buffer(
+ area::geography,
+ CASE
+ WHEN level = 'One' THEN 0
+ WHEN level = 'Two' THEN 1000
+ WHEN level = 'Three' THEN 2000
+ ELSE 0
+ END
+ )
+ ) as extended_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
+ &[&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,
+ ST_AsText(
+ ST_Buffer(
+ area::geography,
+ CASE
+ WHEN level = 'One' THEN 0
+ WHEN level = 'Two' THEN 1000
+ WHEN level = 'Three' THEN 2000
+ ELSE 0
+ END
+ )
+ ) as extended_area,
+ level,
+ reached_users
FROM alerts
ORDER BY id DESC
LIMIT $1
OFFSET $2",
- &[&limit.unwrap_or(20), &offset.unwrap_or(0)],
- )
- .await
- .unwrap()
-
+ &[&limit.unwrap_or(20), &offset.unwrap_or(0)],
+ )
+ .await
+ .unwrap(),
};
let positions: Vec<Alert> = rows
@@ -132,6 +165,7 @@ pub async fn get_alerts<'ctx>(
user_id: row.get("user_id"),
created_at: row.get::<_, f64>("created_at") as i64,
area: row.get("area"),
+ extended_area: row.get("extended_area"),
level: row.get("level"),
reached_users: row.get("reached_users"),
})