diff --git a/server/src/messages.rs b/server/src/messages.rs index bfdea37..00f0060 100644 --- a/server/src/messages.rs +++ b/server/src/messages.rs @@ -1,20 +1,24 @@ -use std::collections::HashMap; +use std::borrow::Cow; use mysql_async::Pool; - -use hyper::{Response, Body, StatusCode}; +use mysql_async::error::Error; +use hyper::{Response, Body}; +use serde_json::Value; use crate::members::Member; +use crate::channels::{Channel, ChannelID}; struct Message { author: Member, date: u64, - content: String + content: String, + channel: Channel } enum MsgParam { Good, + Incomplete, Empty } impl MsgParam { @@ -33,19 +37,48 @@ impl MsgParam { } } -fn parse_send_params(p: &HashMap<&str, &str>, keys: Vec<&str>) -> MsgParam { - let mut cond = MsgParam::Good; - for k in keys.iter() { - if !p.contains_key(k) { - cond = MsgParam::Empty; +fn validate_params(p: &Value, keys: Vec<&str>) -> bool { + let mut fail = false; + for key in keys.iter() { + if let None = p.get(key) { + fail = true; + break; } } - cond + return fail == false; } -pub async fn send_message(pool: &Pool, response: &Response, params: &HashMap<&str, &str>) { - match parse_send_params(params, vec!["content", "id"]) { - MsgParam::Good => {}, - MsgParam::Empty => {} +async fn update_messages_table(pool: &Pool, secret: &str, content: Option<&str>, id: Option) + -> Result<(), mysql_async::error::Error> { + match (content, id) { + (Some(content), Some(id)) => { + let conn = pool.get_conn().await?; + // insert the thing into our db + Ok(()) + } + _ => { + let x = Cow::from("Missing required parameters to create message"); + Err(Error::Other(x)) + } + } +} + +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 + */ + if validate_params(¶ms, vec!["content", "id"]) { + // now we prepare some arguments for our db and send them down the wire + let content = params.get("content").unwrap().as_str(); + let id = params.get("id").unwrap().as_u64(); + let secret: &str = params.get("secret").unwrap().as_str().unwrap(); //auth sucess guarantees this param is fine + match update_messages_table(pool, secret, content, id).await { + Ok(_) => { + + }, + Err(err) => { + } + } } } \ No newline at end of file