From ceaef9ca6814222c49f950ac7cea5df4ba8bbb95 Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Mon, 20 May 2024 00:01:33 +0200 Subject: [PATCH] Add basic geojson serialization capabilities --- Cargo.lock | 3 +++ Cargo.toml | 4 +++- src/models.rs | 11 ++++++----- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d694017..89060cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -847,6 +847,7 @@ checksum = "d69a45c13765c7c6dce8e0fc3d976e669d4de545529c769f44e55b59a4d76c69" dependencies = [ "byteorder", "diesel", + "serde", ] [[package]] @@ -1039,6 +1040,8 @@ dependencies = [ "dotenvy", "http", "postgis_diesel", + "serde", + "serde_json", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index 623d729..17ceebd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,5 +16,7 @@ diesel = { version = "2.1.6", features = ["postgres"] } diesel-derive-enum = { version = "2.1.0", features = ["postgres"] } dotenvy = "0.15.7" http = "1.1.0" -postgis_diesel = "2.3.1" +postgis_diesel = { version = "2.3.1", features = ["serde_geojson"] } +serde = { version = "1.0.202", features = ["derive"] } +serde_json = "1.0.117" tokio = {version = "1.37.0", features = ["full"]} diff --git a/src/models.rs b/src/models.rs index d8ec79e..e5b9dc9 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,15 +1,16 @@ use crate::schema::{nodes, restaurants}; use diesel::prelude::*; use postgis_diesel::types::Point; +use serde::{Deserialize, Serialize}; -#[derive(diesel_derive_enum::DbEnum, Debug)] +#[derive(diesel_derive_enum::DbEnum, Debug, Serialize, Deserialize)] #[ExistingTypePath = "crate::schema::sql_types::NodeType"] pub enum NodeType { Marker, Restaurant, } -#[derive(Insertable)] +#[derive(Insertable, Serialize, Deserialize)] #[diesel(table_name = nodes)] pub struct NewNode<'a> { pub name: &'a str, @@ -17,7 +18,7 @@ pub struct NewNode<'a> { pub coordinates: Point, } -#[derive(Debug, Queryable, AsChangeset)] +#[derive(Debug, Queryable, AsChangeset, Deserialize)] pub struct Node { pub id: i32, pub name: String, @@ -25,7 +26,7 @@ pub struct Node { pub coordinates: Point, } -#[derive(Insertable)] +#[derive(Insertable, Deserialize)] #[diesel(table_name = restaurants)] pub struct NewRestaurant<'a> { pub node_id: i32, @@ -33,7 +34,7 @@ pub struct NewRestaurant<'a> { pub price: i16, } -#[derive(Debug, Queryable, AsChangeset)] +#[derive(Debug, Queryable, AsChangeset, Deserialize)] pub struct Restaurant { pub id: i32, pub node_id: i32,