+ ADD_NEIGHBOR route now supported in API backend

! Requiring a special event in the RTC server docs for this change
This commit is contained in:
shockrah
2021-05-09 23:14:02 -07:00
parent c443b9bb07
commit adc5b261e8
4 changed files with 69 additions and 26 deletions

View File

@@ -1,19 +1,34 @@
use crate::Neighbor;
use mysql_async::Result as SqlResult;
use mysql_async::{Pool, prelude::Queryable};
use mysql_async::{params, Pool, prelude::Queryable};
use serde_json;
impl Neighbor {
pub async fn get_all(p: &Pool) -> SqlResult<Vec<Neighbor>> {
let mut conn = p.get_conn().await?;
let q = "SELECT name, description, tags, url, wsurl FROM neighbors";
type Types = (String, Option<String>, String, String, Option<String>);
let set: Vec<Neighbor> = conn.exec_map(q, (), |(name, description, tags, url, wsurl): Types| {
let json: Vec<String> = serde_json::from_str(tags.as_str()).unwrap_or(vec![]);
Neighbor {
name, description, tags: json, url, wsurl
}
}).await?;
Ok(set)
}
pub async fn get_all(p: &Pool) -> SqlResult<Vec<Neighbor>> {
let mut conn = p.get_conn().await?;
let q = "SELECT name, description, tags, url, wsurl FROM neighbors";
type Types = (String, Option<String>, String, String, Option<String>);
let set: Vec<Neighbor> = conn.exec_map(q, (), |(name, description, tags, url, wsurl): Types| {
let json: Vec<String> = serde_json::from_str(tags.as_str()).unwrap_or(vec![]);
Neighbor {
name, description, tags: json, url, wsurl
}
}).await?;
Ok(set)
}
pub async fn add_neighbor(p: &Pool, url: &str, wsurl: &str, name: &str, desc: &str, tags:&str) -> 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, :description, :tags)";
let sqlparams = params!{
"url"=> url,
"wsurl"=>wsurl,
"name"=>name,
"desc"=>desc,
"tags"=>tags
};
conn.exec_drop(q, sqlparams).await?;
Ok(())
}