+ db::neighbors::get and db::neighbors::update
It should be noted at this point we're considering url's as the "primary key" even though its kinda weird to use them that way. Even though a varchar primary key doesn't really scale well I'm considering it fine for now as the neighbors table is controlled by admins(by hand) and hopefully won't need to scale.
This commit is contained in:
parent
ee5d9fb248
commit
f1b1581588
@ -1,4 +1,4 @@
|
||||
use serde::Serialize;
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
pub mod member;
|
||||
pub mod common;
|
||||
@ -92,7 +92,7 @@ pub struct PublicMember {
|
||||
pub permissions: UBigInt,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Neighbor {
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
|
@ -21,7 +21,7 @@ pub async fn add_neighbor(p: &Pool, url: &str, wsurl: &str, name: &str, desc: &s
|
||||
// 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)";
|
||||
VALUES(:url, :wsurl, :name, :desc, :tags)";
|
||||
let sqlparams = params!{
|
||||
"url"=> url,
|
||||
"wsurl"=>wsurl,
|
||||
@ -32,3 +32,43 @@ pub async fn add_neighbor(p: &Pool, url: &str, wsurl: &str, name: &str, desc: &s
|
||||
conn.exec_drop(q, sqlparams).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get(p: &Pool, url: &str) -> SqlResult<Option<Neighbor>> {
|
||||
let mut conn = p.get_conn().await?;
|
||||
let q = "SELECT wsurl, name, description, tags FROM neighbors
|
||||
WHERE url = :url";
|
||||
|
||||
type Fields = (Option<String>, String, Option<String>, String);
|
||||
let row: Option<Fields> = conn.exec_first(q, params!{"url" => url}).await?;
|
||||
if let Some(fields) = row {
|
||||
let tags: Vec<String> = serde_json::from_str(fields.3.as_str()).unwrap_or(vec![]);
|
||||
let n = Neighbor {
|
||||
name: fields.1,
|
||||
wsurl: fields.0,
|
||||
description: fields.2,
|
||||
tags,
|
||||
url: url.into(),
|
||||
};
|
||||
return Ok(Some(n));
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub async fn update(p: &Pool, url: &str, new: Neighbor) -> SqlResult<()> {
|
||||
let mut conn = p.get_conn().await?;
|
||||
let q = "UPDATE neighbors
|
||||
SET name = :nm, description = :desc, tags = :tags, wsurl = :ws, url = :newurl
|
||||
WHERE url = :url";
|
||||
|
||||
let sqlparams = params!{
|
||||
"url" => url,
|
||||
"nm" => new.name,
|
||||
"desc" => new.description,
|
||||
"tags" => serde_json::to_string(&new.tags).unwrap(),
|
||||
"ws" => new.wsurl,
|
||||
"newurl" => new.url
|
||||
};
|
||||
|
||||
conn.exec_drop(q, sqlparams).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user