+ 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
This commit is contained in:
shockrah 2021-05-12 14:07:38 -07:00
parent e94c720332
commit fbcf16b735
4 changed files with 22 additions and 0 deletions

View File

@ -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(())
}

View File

@ -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

View File

@ -123,3 +123,16 @@ pub async fn update_neighbor(p: &Pool, response: &mut Response<Body>, params: Ha
_ => *response.status_mut() = StatusCode::BAD_REQUEST
}
}
pub async fn remove_neighbor(p: &Pool, response: &mut Response<Body>, params: HashMap<String, String>) {
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
}
}

View File

@ -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");