diff --git a/server/src/channels.rs b/server/src/channels.rs
index bed4244..c88d362 100644
--- a/server/src/channels.rs
+++ b/server/src/channels.rs
@@ -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
) {
else {
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
}
+}
+
+fn parse_insert_channel_params(name: Option<&&str>, kind: Option<&&str>, desc: Option<&&str>) ->
+ Result {
+
+ match (name, kind) {
+ (Some(name), Some(kind)) => {
+ let mut channel = InsertableChannel {
+ name: name.to_string(),
+ kind: ChannelType::Undefined,
+ description: String::new()
+ };
+ match kind.parse::() {
+ 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, 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
+ }
}
\ No newline at end of file
diff --git a/server/src/main.rs b/server/src/main.rs
index 6560fc6..ee1937f 100644
--- a/server/src/main.rs
+++ b/server/src/main.rs
@@ -58,6 +58,7 @@ async fn route_dispatcher(pool: &Pool, resp: &mut Response, 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) -> Result, 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, ¶ms).await {
+ // Deal with permissions errors at this point
match auth_result {
OpenAuth | Good => route_dispatcher(&pool, &mut response, &method, path, ¶ms).await,
LimitPassed => *response.status_mut() = StatusCode::UNAUTHORIZED,
diff --git a/server/src/routes.rs b/server/src/routes.rs
index 0158a15..eaa9732 100644
--- a/server/src/routes.rs
+++ b/server/src/routes.rs
@@ -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
\ No newline at end of file
+pub const CHANNELS_LIST: &'static str = "/channels/list"; // requires none
+pub const CHANNELS_CREATE: &'static str = "/channels/create"; // requires @name @description
\ No newline at end of file