diff --git a/json-api/src/channels.rs b/json-api/src/channels.rs
index f89437c..5ab96e6 100644
--- a/json-api/src/channels.rs
+++ b/json-api/src/channels.rs
@@ -4,6 +4,8 @@ use mysql_async::Pool;
use serde_json::{json, Value};
+use std::collections::HashMap;
+
use db::{
self,
common::FromDB,
@@ -27,53 +29,57 @@ pub async fn list_channels(pool: &Pool, response: &mut Response
) {
};
}
-pub async fn create_channel(pool: &Pool, response: &mut Response, params: Value) {
+pub async fn create_channel(pool: &Pool, response: &mut Response, params: HashMap<&str, &str>) {
/*
* Create a channel base on a few parameters that may or may not be there
* @responds with the data of the newly created channel
*/
// Theres an extra un-needed unwrap to be cut out from this proc
// specifically with the desc parameter
- use std::convert::TryInto;
- let req_params: (Option<&str>, Option<&str>, Option) =
- match (params.get("name"), params.get("description"), params.get("kind")) {
- (Some(name), Some(desc), Some(kind)) => (name.as_str(), desc.as_str(), kind.as_i64()),
- (Some(name), None, Some(kind)) => (name.as_str(), Some("No Description"), kind.as_i64()),
- _ => (None, None, None)
+ let name = params.get("name"); // str
+
+ let description = if let Some(dval) = params.get("description") { // &str
+ dval
+ } else {
+ "No description"
};
- match req_params {
- (Some(name), Some(desc), Some(kind)) => {
- use db::channels::{TEXT_CHANNEL, VOICE_CHANNEL};
- if kind < VOICE_CHANNEL as i64 || kind > TEXT_CHANNEL as i64 {
- *response.status_mut() = StatusCode::BAD_REQUEST; // restriciting to 1|2 for valid channel kinds
+ let kind = match params.get("kind") { // 1 for vc 2 for text
+ Some(value) => {
+ let v = *value;
+ if let Ok(kval) = v.to_string().parse::() {
+ Some(kval)
+ } else {
+ None
}
- else {
- // Send the data up to the db, then return the new channel back to the user(?)
- match db::channels::Channel::add(pool, name, desc, kind.try_into().unwrap()).await {
- db::Response::Row(row) => {
- set_json_body(response, json!(row));
- },
- // user error that the db doesn't deal with so we just blame the user
- db::Response::RestrictedInput(msg) => {
+ },
+ None => None
+ };
+
+ match (name, kind) {
+ (Some(name), Some(kind)) => {
+ use db::channels::{TEXT_CHANNEL, VOICE_CHANNEL};
+ if (kind == VOICE_CHANNEL) ^ (kind == TEXT_CHANNEL) {
+ let db_resp = Channel::new(pool, name, description, kind).await;
+ use db::Response::*;
+ match db_resp {
+ Row(channel) => set_json_body(response, json!(channel)),
+ RestrictedInput(msg) => {
+ eprintln!("{}", msg);
*response.status_mut() = StatusCode::BAD_REQUEST;
- set_json_body(response, json!({"error": msg}));
},
-
- // inserted but could not fetch
- db::Response::Empty => *response.status_mut() = StatusCode::NOT_FOUND,
-
- //like legit issues past here
- db::Response::Other(msg) => {
- *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; // conn issue probably
+ Empty => *response.status_mut() = StatusCode::NOT_FOUND,
+ Other(msg) => {
+ *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
eprintln!("\t[ Channels ] {}", msg);
},
_ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR // ngmi
- }
+ };
+ } else {
+ *response.status_mut() = StatusCode::BAD_REQUEST; // restriciting to 1|2 for valid channel kinds
}
- },
- // basically one of the parameter gets failed so we bail on all of this
+ }
_ => *response.status_mut() = StatusCode::BAD_REQUEST
}
}