summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2024-08-30 15:43:40 +0200
committerSanto Cariotti <santo@dcariotti.me>2024-08-30 15:43:40 +0200
commite118a43c9ba1719be403d6e7a0f8c610319c144c (patch)
treea4a8146e66335e81603c9788935173ff8f2db5c6
parentc11d902f7e37e5bbd5565bf7353e459c793ade52 (diff)
Use timestamp for `created_at` field
-rw-r--r--src/dates.rs36
-rw-r--r--src/graphql/mutation.rs10
-rw-r--r--src/graphql/types/alert.rs9
-rw-r--r--src/graphql/types/position.rs19
-rw-r--r--src/main.rs1
5 files changed, 17 insertions, 58 deletions
diff --git a/src/dates.rs b/src/dates.rs
deleted file mode 100644
index 9c007e9..0000000
--- a/src/dates.rs
+++ /dev/null
@@ -1,36 +0,0 @@
-use async_graphql::{InputValueError, InputValueResult, Scalar, ScalarType, Value};
-use chrono::{DateTime, Utc};
-use serde::{Deserialize, Serialize};
-
-#[derive(Serialize, Deserialize, Debug, Clone)]
-/// A DateTime used as GraphQL type
-pub struct GraphQLDate(pub DateTime<Utc>);
-
-impl From<DateTime<Utc>> for GraphQLDate {
- fn from(dt: DateTime<Utc>) -> Self {
- GraphQLDate(dt)
- }
-}
-
-impl From<GraphQLDate> for DateTime<Utc> {
- fn from(my_dt: GraphQLDate) -> Self {
- my_dt.0
- }
-}
-
-#[Scalar]
-impl ScalarType for GraphQLDate {
- fn parse(value: Value) -> InputValueResult<Self> {
- if let Value::String(s) = &value {
- DateTime::parse_from_rfc3339(s)
- .map(|dt| GraphQLDate(dt.with_timezone(&Utc)))
- .map_err(|e| InputValueError::custom(format!("Invalid DateTime: {}", e)))
- } else {
- Err(InputValueError::expected_type(value))
- }
- }
-
- fn to_value(&self) -> Value {
- Value::String(self.0.to_rfc3339())
- }
-}
diff --git a/src/graphql/mutation.rs b/src/graphql/mutation.rs
index 682d9b6..d6c704a 100644
--- a/src/graphql/mutation.rs
+++ b/src/graphql/mutation.rs
@@ -1,5 +1,4 @@
use crate::{
- dates::GraphQLDate,
graphql::types::{
alert,
jwt::{self, Authentication},
@@ -9,7 +8,6 @@ use crate::{
state::AppState,
};
use async_graphql::{Context, Error, FieldResult, Object};
-use chrono::Utc;
/// Mutation struct
pub struct Mutation;
@@ -66,7 +64,7 @@ impl Mutation {
ST_SetSRID(ST_MakePoint($2, $3), 4326),
$4
)
- RETURNING id, user_id, created_at, ST_Y(location::geometry) AS latitude, ST_X(location::geometry) AS longitude, activity
+ RETURNING id, user_id, extract(epoch from created_at)::double precision as created_at, ST_Y(location::geometry) AS latitude, ST_X(location::geometry) AS longitude, activity
",
&[
&claims.user_id,
@@ -83,7 +81,7 @@ impl Mutation {
.map(|row| position::Position {
id: row.get("id"),
user_id: row.get("user_id"),
- created_at: GraphQLDate(Utc::now()),
+ created_at: row.get::<_, f64>("created_at") as i64,
latitude: row.get("latitude"),
longitude: row.get("longitude"),
moving_activity: row.get("activity"),
@@ -158,7 +156,7 @@ impl Mutation {
let query = format!("INSERT INTO alerts (user_id, area, level)
VALUES($1, {}, $2)
- RETURNING id, user_id, created_at, ST_AsText(area) as area, level, reached_users
+ RETURNING id, user_id, extract(epoch from created_at)::double precision as created_at, ST_AsText(area) as area, level, reached_users
", polygon);
match client.query(&query, &[&claims.user_id, &input.level]).await {
@@ -168,7 +166,7 @@ impl Mutation {
.map(|row| alert::Alert {
id: row.get("id"),
user_id: row.get("user_id"),
- created_at: GraphQLDate(Utc::now()),
+ created_at: row.get::<_, f64>("created_at") as i64,
area: row.get("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 cede465..9c21013 100644
--- a/src/graphql/types/alert.rs
+++ b/src/graphql/types/alert.rs
@@ -1,6 +1,5 @@
-use crate::{dates::GraphQLDate, graphql::types::jwt::Authentication, state::AppState};
+use crate::{graphql::types::jwt::Authentication, state::AppState};
use async_graphql::{Context, Enum, InputObject, SimpleObject};
-use chrono::Utc;
use serde::{Deserialize, Serialize};
use std::error::Error;
use tokio_postgres::types::{to_sql_checked, FromSql, IsNull, ToSql, Type};
@@ -65,7 +64,7 @@ pub struct PolygonValid {
pub struct Alert {
pub id: i32,
pub user_id: i32,
- pub created_at: GraphQLDate,
+ pub created_at: i64,
pub area: String,
pub level: LevelAlert,
pub reached_users: i32,
@@ -102,7 +101,7 @@ pub async fn get_alerts<'ctx>(
Authentication::Logged(_) => {
let rows = client
.query(
- "SELECT id, user_id, created_at, ST_AsText(area) as area, level, reached_users
+ "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
LIMIT $1
@@ -117,7 +116,7 @@ pub async fn get_alerts<'ctx>(
.map(|row| Alert {
id: row.get("id"),
user_id: row.get("user_id"),
- created_at: GraphQLDate(Utc::now()),
+ created_at: row.get::<_, f64>("created_at") as i64,
area: row.get("area"),
level: row.get("level"),
reached_users: row.get("reached_users"),
diff --git a/src/graphql/types/position.rs b/src/graphql/types/position.rs
index a9236a6..dfa1830 100644
--- a/src/graphql/types/position.rs
+++ b/src/graphql/types/position.rs
@@ -1,6 +1,5 @@
-use crate::{dates::GraphQLDate, graphql::types::jwt::Authentication, state::AppState};
+use crate::{graphql::types::jwt::Authentication, state::AppState};
use async_graphql::{Context, Enum, InputObject, SimpleObject};
-use chrono::Utc;
use serde::{Deserialize, Serialize};
use std::error::Error;
use tokio_postgres::types::{to_sql_checked, FromSql, IsNull, ToSql, Type};
@@ -72,7 +71,7 @@ impl ToSql for MovingActivity {
pub struct Position {
pub id: i32,
pub user_id: i32,
- pub created_at: GraphQLDate,
+ pub created_at: i64,
pub latitude: f64,
pub longitude: f64,
pub moving_activity: MovingActivity,
@@ -115,7 +114,7 @@ pub async fn get_positions<'ctx>(
match user_id {
Some(id) => {
rows = client.query("
- SELECT id, user_id, created_at, ST_Y(location::geometry) AS latitude, ST_X(location::geometry) AS longitude, activity
+ SELECT id, user_id, extract(epoch from created_at)::double precision as created_at, ST_Y(location::geometry) AS latitude, ST_X(location::geometry) AS longitude, activity
FROM positions
WHERE user_id = $1
ORDER BY id DESC
@@ -125,7 +124,7 @@ pub async fn get_positions<'ctx>(
}
None => {
rows = client.query("
- SELECT id, user_id, created_at, ST_Y(location::geometry) AS latitude, ST_X(location::geometry) AS longitude, activity
+ SELECT id, user_id, extract(epoch from created_at)::double precision as created_at, ST_Y(location::geometry) AS latitude, ST_X(location::geometry) AS longitude, activity
FROM positions
ORDER BY id DESC
LIMIT $1
@@ -135,7 +134,7 @@ pub async fn get_positions<'ctx>(
}
} else {
rows = client.query("
- SELECT id, user_id, created_at, ST_Y(location::geometry) AS latitude, ST_X(location::geometry) AS longitude, activity
+ SELECT id, user_id, extract(epoch from created_at)::double precision as created_at, ST_Y(location::geometry) AS latitude, ST_X(location::geometry) AS longitude, activity
FROM positions
WHERE user_id = $1
ORDER BY id DESC
@@ -149,7 +148,7 @@ pub async fn get_positions<'ctx>(
.map(|row| Position {
id: row.get("id"),
user_id: row.get("user_id"),
- created_at: GraphQLDate(Utc::now()),
+ created_at: row.get::<_, f64>("created_at") as i64,
latitude: row.get("latitude"),
longitude: row.get("longitude"),
moving_activity: row.get("activity"),
@@ -186,7 +185,7 @@ pub async fn last_positions<'ctx>(
let rows = client
.query(
"SELECT DISTINCT ON (user_id)
- id, user_id, created_at, ST_Y(location::geometry) AS latitude, ST_X(location::geometry) AS longitude, activity
+ id, user_id, extract(epoch from created_at)::double precision as created_at, ST_Y(location::geometry) AS latitude, ST_X(location::geometry) AS longitude, activity
FROM positions ORDER BY user_id, created_at DESC",
&[],
)
@@ -199,7 +198,7 @@ pub async fn last_positions<'ctx>(
.map(|row| Position {
id: row.get("id"),
user_id: row.get("user_id"),
- created_at: GraphQLDate(Utc::now()),
+ created_at: row.get::<_, f64>("created_at") as i64,
latitude: row.get("latitude"),
longitude: row.get("longitude"),
moving_activity: row.get("activity"),
@@ -211,7 +210,7 @@ pub async fn last_positions<'ctx>(
.map(|row| Position {
id: row.get("id"),
user_id: row.get("user_id"),
- created_at: GraphQLDate(Utc::now()),
+ created_at: row.get::<_, f64>("created_at") as i64,
latitude: row.get("latitude"),
longitude: row.get("longitude"),
moving_activity: row.get("activity"),
diff --git a/src/main.rs b/src/main.rs
index 1614e6b..c288001 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,4 @@
mod config;
-mod dates;
mod db;
mod errors;
mod graphql;