From b966c61c20ac938ea6ce6b633b5ad6e55d0eb016 Mon Sep 17 00:00:00 2001 From: shockrah Date: Thu, 12 Nov 2020 13:18:43 -0800 Subject: [PATCH] Finally the change over to db::messages::Messages::send in userland code - removed old helper function which is no longer needed * cleaned up responses for empty,other, and _ in /channels/create endpoint handler --- server-api/src/channels.rs | 6 ++-- server-api/src/messages.rs | 69 +++++++++----------------------------- 2 files changed, 19 insertions(+), 56 deletions(-) diff --git a/server-api/src/channels.rs b/server-api/src/channels.rs index 2126f41..1ec69c2 100644 --- a/server-api/src/channels.rs +++ b/server-api/src/channels.rs @@ -57,9 +57,9 @@ pub async fn create_channel(pool: &Pool, response: &mut Response, params: *response.body_mut() = Body::from(to_string(&row).unwrap_or("{}".into())); }, - db::Response::Empty => {}, - db::Response::Other(msg) => {}, - _ => {*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;} + db::Response::Empty => *response.status_mut() = StatusCode::NOT_FOUND, + db::Response::Other(msg) => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR, + _ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR } }, // basically one of the parameter gets failed so we bail on all of this diff --git a/server-api/src/messages.rs b/server-api/src/messages.rs index 220f6df..a34de4e 100644 --- a/server-api/src/messages.rs +++ b/server-api/src/messages.rs @@ -10,69 +10,32 @@ use chrono::Utc; use db::UBigInt; -pub async fn insert_message(pool: &Pool, content: &Value, channel_name: &Value, author_id: UBigInt) - -> Result<(), Error>{ - match (content.as_str(), channel_name.as_str()) { - (Some(content), Some(channel)) => { - let conn = pool.get_conn().await?; - let time = Utc::now().timestamp(); - conn.prep_exec( - r"INSERT INTO messages - (time, content, author_id, channel_name) - VALUES(:time, :content, :author, :channel)", - params!{ - "time" => time, - "content" => content, - "author" => author_id, - "channel" => channel - }).await?; - Ok(()) - } - _ => { - let e = Cow::from("Required parameter missing"); - Err(Error::Other(e)) - } - } -} - pub async fn send_message(pool: &Pool, response: &mut Response, params: Value) { /* * @content: expecting string type - * @id: expecting the channel id that we're posting data to + * @channel: channel id that we're going to send a message to */ - let content_r = params.get("content"); - let channel_name_r = params.get("channel"); - // auth module guarantees this will be there in the correct form + // NOTE: auth module guarantees this will be there in the correct form let author = params.get("id") .unwrap().as_u64().unwrap(); - match (content_r, channel_name_r) { - (Some(content), Some(channel_name)) => { - match insert_message(pool, content, channel_name, author).await { - Ok(_) => *response.status_mut() = StatusCode::OK, - Err(err) => { - use mysql_async::error::Error::{Server}; - println!("\tDB Error::send_message: {:?}", err); - // doing this to avoid client confusion as some input does cause sql errors - if let Server(se) = err { - if se.code == 1452 { - *response.status_mut() = StatusCode::BAD_REQUEST; - *response.body_mut() = Body::from(format!("{} does not exist", channel_name)); - } - else { - *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; - } - } - else { - *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; - } + match (params.get("conetnt") , params.get("channel")) { + (Some(content_v), Some(channel_id_v)) => { + let (content, channel) = (content_v.as_str(), channel_id_v.as_u64()); + + if let (Some(message), Some(cid)) = (content, channel) { + // call returns empty on sucess so we don't need to do anything + if let Err(issue) = db::messages::Message::send(pool, message, cid, author).await { + *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; + // log(send) } + // if there's no issue then we don't need to do anything at all + } + else { + *response.status_mut() = StatusCode::BAD_REQUEST; } }, - _ => { - *response.status_mut() = StatusCode::BAD_REQUEST; - *response.body_mut() = Body::from("content/channel missing from json parameters"); - } + _ => *response.status_mut() = StatusCode::BAD_REQUEST } }