* /channels/create handler uses hashmaps
* cleaned up wall of parameter parsing to be less cancerous(read diff to die instantly) - Removed repeated db::Response in response mutator match Simplified how parameters are parsed by making them actually readable
This commit is contained in:
parent
c1ef4c6336
commit
44b51133d2
@ -4,6 +4,8 @@ use mysql_async::Pool;
|
|||||||
|
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use db::{
|
use db::{
|
||||||
self,
|
self,
|
||||||
common::FromDB,
|
common::FromDB,
|
||||||
@ -27,53 +29,57 @@ pub async fn list_channels(pool: &Pool, response: &mut Response<Body>) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_channel(pool: &Pool, response: &mut Response<Body>, params: Value) {
|
pub async fn create_channel(pool: &Pool, response: &mut Response<Body>, params: HashMap<&str, &str>) {
|
||||||
/*
|
/*
|
||||||
* Create a channel base on a few parameters that may or may not be there
|
* Create a channel base on a few parameters that may or may not be there
|
||||||
* @responds with the data of the newly created channel
|
* @responds with the data of the newly created channel
|
||||||
*/
|
*/
|
||||||
// Theres an extra un-needed unwrap to be cut out from this proc
|
// Theres an extra un-needed unwrap to be cut out from this proc
|
||||||
// specifically with the desc parameter
|
// specifically with the desc parameter
|
||||||
use std::convert::TryInto;
|
|
||||||
|
|
||||||
let req_params: (Option<&str>, Option<&str>, Option<i64>) =
|
let name = params.get("name"); // str
|
||||||
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()),
|
let description = if let Some(dval) = params.get("description") { // &str
|
||||||
(Some(name), None, Some(kind)) => (name.as_str(), Some("No Description"), kind.as_i64()),
|
dval
|
||||||
_ => (None, None, None)
|
} else {
|
||||||
|
"No description"
|
||||||
};
|
};
|
||||||
|
|
||||||
match req_params {
|
let kind = match params.get("kind") { // 1 for vc 2 for text
|
||||||
(Some(name), Some(desc), Some(kind)) => {
|
Some(value) => {
|
||||||
use db::channels::{TEXT_CHANNEL, VOICE_CHANNEL};
|
let v = *value;
|
||||||
if kind < VOICE_CHANNEL as i64 || kind > TEXT_CHANNEL as i64 {
|
if let Ok(kval) = v.to_string().parse::<i32>() {
|
||||||
*response.status_mut() = StatusCode::BAD_REQUEST; // restriciting to 1|2 for valid channel kinds
|
Some(kval)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
else {
|
},
|
||||||
// Send the data up to the db, then return the new channel back to the user(?)
|
None => None
|
||||||
match db::channels::Channel::add(pool, name, desc, kind.try_into().unwrap()).await {
|
};
|
||||||
db::Response::Row(row) => {
|
|
||||||
set_json_body(response, json!(row));
|
match (name, kind) {
|
||||||
},
|
(Some(name), Some(kind)) => {
|
||||||
// user error that the db doesn't deal with so we just blame the user
|
use db::channels::{TEXT_CHANNEL, VOICE_CHANNEL};
|
||||||
db::Response::RestrictedInput(msg) => {
|
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;
|
*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||||
set_json_body(response, json!({"error": msg}));
|
|
||||||
},
|
},
|
||||||
|
Empty => *response.status_mut() = StatusCode::NOT_FOUND,
|
||||||
// inserted but could not fetch
|
Other(msg) => {
|
||||||
db::Response::Empty => *response.status_mut() = StatusCode::NOT_FOUND,
|
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||||
|
|
||||||
//like legit issues past here
|
|
||||||
db::Response::Other(msg) => {
|
|
||||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; // conn issue probably
|
|
||||||
eprintln!("\t[ Channels ] {}", msg);
|
eprintln!("\t[ Channels ] {}", msg);
|
||||||
},
|
},
|
||||||
_ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR // ngmi
|
_ => *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
|
_ => *response.status_mut() = StatusCode::BAD_REQUEST
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user