/channels/create refactored to use the new db-lib api

 Reduced branching a shit ton

Testing was ass but it should pass client tests now, back to normal
This commit is contained in:
shockrah 2021-03-07 17:47:24 -08:00
parent 7c3537e4f6
commit 5fc3e2a553

View File

@ -42,51 +42,34 @@ pub async fn create_channel(pool: &Pool, response: &mut Response<Body>, params:
*/
// Theres an extra un-needed unwrap to be cut out from this proc
// specifically with the desc parameter
use db::Response::*;
let name = params.get("name"); // str
let description = if let Some(dval) = params.get("description") { // &str
dval
} else {
"No description"
let name = params.get("name");
let kind = qs_param!(params, "type", i32);
let description = match params.get("description") {
Some(d) => d,
None => "No description"
};
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::<i32>() {
Some(kval)
} else {
None
}
},
None => None
};
*response.status_mut() = if name.is_some() && kind.is_some() {
let (name, kind) = (name.unwrap(), kind.unwrap());
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;
},
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
if let Ok(resp) = Channel::add(pool, name, description, kind).await {
match resp {
Row(channel) => {
set_json_body(response, json!(channel));
StatusCode::OK
},
RestrictedInput(_) => StatusCode::UNPROCESSABLE_ENTITY,
Other(_) => StatusCode::INTERNAL_SERVER_ERROR,
_ => StatusCode::INTERNAL_SERVER_ERROR
}
} else {
StatusCode::INTERNAL_SERVER_ERROR
}
_ => *response.status_mut() = StatusCode::BAD_REQUEST
} else {
StatusCode::BAD_REQUEST
}
}