diff options
| author | Santo Cariotti <santo@dcariotti.me> | 2024-09-05 10:29:49 +0000 |
|---|---|---|
| committer | Santo Cariotti <santo@dcariotti.me> | 2024-09-05 10:29:49 +0000 |
| commit | b592d19f1a74a32df0aaed7dbf7484d0ef5ad35a (patch) | |
| tree | 51d1452393cc282421da923dafa33f751138dac7 /src/graphql/types | |
| parent | 7e9dbd60f55f02ab065c764f8230aabaa6778eed (diff) | |
Create notifications from a new alert
Diffstat (limited to 'src/graphql/types')
| -rw-r--r-- | src/graphql/types/notification.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/graphql/types/notification.rs b/src/graphql/types/notification.rs index 67fb42f..a8c327e 100644 --- a/src/graphql/types/notification.rs +++ b/src/graphql/types/notification.rs @@ -1,9 +1,11 @@ use crate::{ + errors::AppError, graphql::types::{alert::Alert, jwt::Authentication, position::Position, user::find_user}, state::AppState, }; use async_graphql::{Context, SimpleObject}; use serde::{Deserialize, Serialize}; +use tokio_postgres::Client; #[derive(SimpleObject, Clone, Debug, Serialize, Deserialize)] /// Notification struct @@ -14,6 +16,30 @@ pub struct Notification { pub seen: bool, pub created_at: i64, } + +impl Notification { + /// Create a new notification into the database from an alert_id and a position_id. + /// Returns the new ID. + pub async fn new(client: &Client, alert_id: i32, position_id: i32) -> Result<i32, AppError> { + match client + .query( + "INSERT INTO notifications(alert_id, position_id) + VALUES($1, $2) + RETURNING id + ", + &[&alert_id, &position_id], + ) + .await + { + Ok(rows) => { + let row = rows[0].clone(); + Ok(row.get("id")) + } + Err(_) => Err(AppError::Database), + } + } +} + /// Get notifications from the database pub async fn get_notifications<'ctx>( ctx: &Context<'ctx>, |
