primary dispatcher to insert channels now built

parameter parsing functino was built for the primary dispatcher
New InsterableChannel structure added: mysql controls the id field for us
This commit is contained in:
shockrah 2020-06-03 00:08:35 -07:00
parent 9e6f32e36e
commit 7fa103b1dc
3 changed files with 75 additions and 2 deletions

View File

@ -1,8 +1,11 @@
use std::collections::HashMap;
use std::borrow::Cow;
use hyper::{StatusCode, Response, Body};
use hyper::header::HeaderValue;
use mysql_async::{Conn, Pool};
use mysql_async::error::Error;
use mysql_async::prelude::Queryable;
use mysql_async::prelude::{params, Queryable};
enum ChannelType {
Voice,
@ -37,6 +40,12 @@ struct Channel {
kind: ChannelType
}
struct InsertableChannel {
name: String,
description: String,
kind: ChannelType
}
impl Channel {
/*
* When our sql library queries things we generally get back tuples rather reasily
@ -119,4 +128,65 @@ pub async fn list_channels(pool: &Pool, response: &mut Response<Body>) {
else {
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
}
}
fn parse_insert_channel_params(name: Option<&&str>, kind: Option<&&str>, desc: Option<&&str>) ->
Result<InsertableChannel, Error> {
match (name, kind) {
(Some(name), Some(kind)) => {
let mut channel = InsertableChannel {
name: name.to_string(),
kind: ChannelType::Undefined,
description: String::new()
};
match kind.parse::<i32>() {
Ok(val) => {
channel.kind = ChannelType::from_i32(val);
if let Some(d) = desc {
channel.description = d.to_string();
}
Ok(channel)
},
Err(_) => {
let x = Cow::from("Parse error with kind parameter");
Err(Error::Other(x))
}
}
}
_ => {
let x = Cow::from("Missing required parameters");
Err(Error::Other(x))
}
}
}
async fn insert_channel(pool: &Pool, channel: InsertableChannel) -> Result<(), Error> {
let conn = pool.get_conn().await?;
let conn = conn.batch_exec(
r"INSERT INTO channels (name, kind, description) VALUES (:name, :kind, :description)",
params!{
"name" => channel.name,
"kind" => channel.kind.as_i32(),
"description" => channel.description,
}).await?;
Ok(())
}
pub async fn create_channel(pool: &Pool, response: &Response<Body>, params: HashMap<&str, &str>) {
/*
* Create a channel base on a few parameters that may or may not be there
*/
let name_r = params.get("name");
let desc_r = params.get("description");
let kind_r = params.get("kind");
match parse_insert_channel_params(name_r, kind_r, desc_r) {
Ok(channel) => {
match insert_channel(pool, channel).await {
Ok(_) => *response.status_mut() = StatusCode::OK,
Err(_) => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
}
},
Err(e) => *response.status_mut() = StatusCode::BAD_REQUEST
}
}

View File

@ -58,6 +58,7 @@ async fn route_dispatcher(pool: &Pool, resp: &mut Response<Body>, meth: &Method,
}
},
(&Method::GET, routes::CHANNELS_LIST) => channels::list_channels(pool, resp).await,
(&Method::POST, routes::CHANNELS_CREATE) => channels::create_channel(pool, resp, params).await,
_ => *resp.status_mut() = StatusCode::NOT_FOUND
}
}
@ -72,6 +73,7 @@ async fn main_responder(request: Request<Body>) -> Result<Response<Body>, hyper:
let pool = Pool::new(&env::var("DATABASE_URL").unwrap());
// some more information in the response would be great right about here
if let Ok(auth_result) = auth::wall_entry(path, &pool, &params).await {
// Deal with permissions errors at this point
match auth_result {
OpenAuth | Good => route_dispatcher(&pool, &mut response, &method, path, &params).await,
LimitPassed => *response.status_mut() = StatusCode::UNAUTHORIZED,

View File

@ -1,4 +1,5 @@
pub const INVITE_JOIN: &'static str = "/invite/join"; // requires @code
pub const INVITE_CREATE: &'static str = "/invite/create"; // requires none
pub const CHANNELS_LIST: &'static str = "/channels/list"; // requires none
pub const CHANNELS_LIST: &'static str = "/channels/list"; // requires none
pub const CHANNELS_CREATE: &'static str = "/channels/create"; // requires @name @description