base send_message ready to take and process params into our db

This commit is contained in:
shockrah 2020-07-05 15:06:33 -07:00
parent 112c9f34cd
commit f827cef9c5

View File

@ -1,20 +1,24 @@
use std::collections::HashMap; use std::borrow::Cow;
use mysql_async::Pool; use mysql_async::Pool;
use mysql_async::error::Error;
use hyper::{Response, Body, StatusCode}; use hyper::{Response, Body};
use serde_json::Value;
use crate::members::Member; use crate::members::Member;
use crate::channels::{Channel, ChannelID};
struct Message { struct Message {
author: Member, author: Member,
date: u64, date: u64,
content: String content: String,
channel: Channel
} }
enum MsgParam { enum MsgParam {
Good, Good,
Incomplete,
Empty Empty
} }
impl MsgParam { impl MsgParam {
@ -33,19 +37,48 @@ impl MsgParam {
} }
} }
fn parse_send_params(p: &HashMap<&str, &str>, keys: Vec<&str>) -> MsgParam { fn validate_params(p: &Value, keys: Vec<&str>) -> bool {
let mut cond = MsgParam::Good; let mut fail = false;
for k in keys.iter() { for key in keys.iter() {
if !p.contains_key(k) { if let None = p.get(key) {
cond = MsgParam::Empty; fail = true;
break;
} }
} }
cond return fail == false;
} }
pub async fn send_message(pool: &Pool, response: &Response<Body>, params: &HashMap<&str, &str>) { async fn update_messages_table(pool: &Pool, secret: &str, content: Option<&str>, id: Option<ChannelID>)
match parse_send_params(params, vec!["content", "id"]) { -> Result<(), mysql_async::error::Error> {
MsgParam::Good => {}, match (content, id) {
MsgParam::Empty => {} (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<Body>, params: Value) {
/*
* @content: expecting string type
* @id: expecting the channel id that we're posting data to
*/
if validate_params(&params, 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) => {
}
}
} }
} }