From 666894af0e920c978ef49fd2aae45aaf4cb12b2c Mon Sep 17 00:00:00 2001 From: shockrah Date: Tue, 11 May 2021 17:18:17 -0700 Subject: [PATCH] + Debug to Neighbor struct * add_neighbor now takes a moved Neighbor param instead of the fields individually This should make it a easier for both front & backend to parse what's happening. It also let's us leveraege serde_json a bit more --- json-api/db/src/lib.rs | 2 +- json-api/db/src/neighbors.rs | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/json-api/db/src/lib.rs b/json-api/db/src/lib.rs index e8848ec..0de94d3 100644 --- a/json-api/db/src/lib.rs +++ b/json-api/db/src/lib.rs @@ -92,7 +92,7 @@ pub struct PublicMember { pub permissions: UBigInt, } -#[derive(Deserialize, Serialize)] +#[derive(Debug, Deserialize, Serialize)] pub struct Neighbor { pub name: String, pub description: Option, diff --git a/json-api/db/src/neighbors.rs b/json-api/db/src/neighbors.rs index 4dd111e..9804251 100644 --- a/json-api/db/src/neighbors.rs +++ b/json-api/db/src/neighbors.rs @@ -16,17 +16,18 @@ pub async fn get_all(p: &Pool) -> SqlResult> { Ok(set) } -pub async fn add_neighbor(p: &Pool, url: &str, wsurl: &str, name: &str, desc: &str, tags:&str) -> SqlResult<()> { +pub async fn add_neighbor(p: &Pool, new: Neighbor) -> SqlResult<()> { // Note we assume that the tags field has been verified as proper valid json prior to // writing it to the db let mut conn = p.get_conn().await?; let q = "INSERT INTO neighbors(url, wsurl, name, description, tags) VALUES(:url, :wsurl, :name, :desc, :tags)"; + let tags = serde_json::to_string(&new.tags).unwrap_or("[]".to_string()); let sqlparams = params!{ - "url"=> url, - "wsurl"=>wsurl, - "name"=>name, - "desc"=>desc, + "url"=> new.url, + "wsurl"=>new.wsurl, + "name"=>new.name, + "desc"=>new.description, "tags"=>tags }; conn.exec_drop(q, sqlparams).await?;