From 7a093c1fbc08491aa1682d2cbeeca85cad38b6a1 Mon Sep 17 00:00:00 2001 From: shockrah Date: Mon, 2 Nov 2020 00:12:16 -0800 Subject: [PATCH] new delete_channel endpoint handler * to be tested --- server-api/src/channels.rs | 43 +++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/server-api/src/channels.rs b/server-api/src/channels.rs index fdb537d..36a8870 100644 --- a/server-api/src/channels.rs +++ b/server-api/src/channels.rs @@ -7,8 +7,11 @@ use mysql_async::prelude::{params, Queryable}; use serde_json::Value; -use crate::db_types::{UBigInt, VarChar, Integer}; -use crate::common; +use db::{ + self, + UBigInt, VarChar, Integer, + common::FromDB +}; #[derive(Debug)] pub enum ChannelType { @@ -163,18 +166,10 @@ pub async fn create_channel(pool: &Pool, response: &mut Response, params: match req_params { (Some(name), Some(desc), Some(kind)) => { match insert_channel(pool, name, desc, kind).await { - // Server Errors are generally _ok_ to reveal in body I suppose - Err(Error::Server(se)) => { - common::db_err_response_body(response, se); - //*response.status_mut() = StatusCode::BAD_REQUEST; - //let b = format!("Server code: {}\nServer Message:{}", se.code, se.message); - //*response.body_mut() = Body::from(b); - }, - // generic errors get a 500 - Err(_) => { + Err(e) => { + println!("SERVER ERROR: {}", e); *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; - } - // Nothing to do when things go right + }, _ => {} } }, @@ -183,21 +178,21 @@ pub async fn create_channel(pool: &Pool, response: &mut Response, params: } } -async fn db_delete_channel(pool: &Pool, name: &Value) -> Result<(), Error> { - let conn = pool.get_conn().await?; - conn.prep_exec(r"DELETE FROM channels WHERE name = :name", params!{"name" => name.as_str().unwrap()}).await?; - Ok(()) -} - pub async fn delete_channel(pool: &Pool, response: &mut Response, params: Value) { // make sure we have the right parameters provided - if let Some(name) = params.get("name") { - match db_delete_channel(pool, name).await { - Ok(_) => *response.status_mut() = StatusCode::OK, - Err(e) => { - *response.body_mut() = Body::from(format!("delete_chanel sql error :\n{}", e)); + use db::channels::Channel; + if let Some(name) = params.get("channel_id") { + if let Some(id) = name.as_u64() { + // TODO: something more intelligent with the logging im ngl + match Channel::delete(pool, id).await { + db::Response::Success => {}, + db::Response::Other(data) => println!("\t{}", data), + _ => {} } } + else { + *response.status_mut() = StatusCode::BAD_REQUEST; + } } else { *response.status_mut() = StatusCode::BAD_REQUEST;