From fbcf16b735310f3bf09e102db976586ddb24d1aa Mon Sep 17 00:00:00 2001 From: shockrah Date: Wed, 12 May 2021 14:07:38 -0700 Subject: [PATCH] + Adding /neighbor/remove route api code and db-lib code This patch has not yet been tested but will be in the coming patches Sumamary: we're just doing a regular delete on the neighbor's table based off the url field --- json-api/db/src/neighbors.rs | 7 +++++++ json-api/src/main.rs | 1 + json-api/src/meta.rs | 13 +++++++++++++ json-api/src/routes.rs | 1 + 4 files changed, 22 insertions(+) diff --git a/json-api/db/src/neighbors.rs b/json-api/db/src/neighbors.rs index 9804251..d75c3b3 100644 --- a/json-api/db/src/neighbors.rs +++ b/json-api/db/src/neighbors.rs @@ -73,3 +73,10 @@ pub async fn update(p: &Pool, url: &str, new: Neighbor) -> SqlResult<()> { conn.exec_drop(q, sqlparams).await?; Ok(()) } + +pub async fn remove(p: &Pool, url: &str) -> SqlResult<()> { + let mut conn = p.get_conn().await?; + let q = "DELETE FROM neighbors WHERE url = :url"; + conn.exec_drop(q, params!{"url" => url}).await?; + Ok(()) +} diff --git a/json-api/src/main.rs b/json-api/src/main.rs index 2ff3e16..343b22e 100644 --- a/json-api/src/main.rs +++ b/json-api/src/main.rs @@ -92,6 +92,7 @@ async fn route_dispatcher( (GET, routes::GET_NEIGHBORS) => meta::server_neighbors(pool, resp).await, (POST, routes::ADD_NEIGHBOR) => meta::add_neighbor(pool, resp, body).await, (PUT, routes::UPDATE_NEIGHBOR) => meta::update_neighbor(pool, resp, params, body).await, + (DELETE, routes::REMOVE_NEIGHBOR) => meta::remove_neighbor(pool, resp, params).await, _ => { println!("[HTTP]\tNOT FOUND: {}: {}", meth, path); *resp.status_mut() = StatusCode::NOT_FOUND diff --git a/json-api/src/meta.rs b/json-api/src/meta.rs index 51e3f22..fb9ebd4 100644 --- a/json-api/src/meta.rs +++ b/json-api/src/meta.rs @@ -123,3 +123,16 @@ pub async fn update_neighbor(p: &Pool, response: &mut Response, params: Ha _ => *response.status_mut() = StatusCode::BAD_REQUEST } } + +pub async fn remove_neighbor(p: &Pool, response: &mut Response, params: HashMap) { + match params.get("url") { + Some(url) => { + // As usual 500 on server errors otherwise there's nothing to do + if let Err(e) = db::neighbors::remove(p, url).await { + eprintln!("[HTTP] [DB-LIB] /neighbor/remove {}", e); + *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; + } + }, + None => *response.status_mut() = StatusCode::BAD_REQUEST + } +} diff --git a/json-api/src/routes.rs b/json-api/src/routes.rs index b0670e1..8f4b17a 100644 --- a/json-api/src/routes.rs +++ b/json-api/src/routes.rs @@ -28,6 +28,7 @@ pub const SET_NEW_ADMIN: Rstr = "/owner/newadmin"; // @requiers: ow pub const GET_NEIGHBORS: Rstr = "/neighbor/list"; // @requires: none @note must be a member for this list pub const ADD_NEIGHBOR: Rstr = "/neighbor/add"; // @requires: perm::add_neighbor pub const UPDATE_NEIGHBOR: Rstr = "/neighbor/update"; // @requires perms::add_neighbor @url(unique) +pub const REMOVE_NEIGHBOR: Rstr = "/neighbor/delete"; // @requires perms::add_neighbor @url(unique) pub fn is_open(path: &str) -> bool { return path.starts_with("/join") || path.starts_with("/meta");