/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 // Theres an extra un-needed unwrap to be cut out from this proc
// specifically with the desc parameter // specifically with the desc parameter
use db::Response::*;
let name = params.get("name"); // str let name = params.get("name");
let kind = qs_param!(params, "type", i32);
let description = if let Some(dval) = params.get("description") { // &str let description = match params.get("description") {
dval Some(d) => d,
} else { None => "No description"
"No description"
}; };
let kind = match params.get("kind") { // 1 for vc 2 for text *response.status_mut() = if name.is_some() && kind.is_some() {
Some(value) => { let (name, kind) = (name.unwrap(), kind.unwrap());
let v = value;
if let Ok(kval) = v.to_string().parse::<i32>() {
Some(kval)
} else {
None
}
},
None => None
};
match (name, kind) { if let Ok(resp) = Channel::add(pool, name, description, kind).await {
(Some(name), Some(kind)) => { match resp {
use db::channels::{TEXT_CHANNEL, VOICE_CHANNEL}; Row(channel) => {
if (kind == VOICE_CHANNEL) ^ (kind == TEXT_CHANNEL) { set_json_body(response, json!(channel));
let db_resp = Channel::new(pool, name, description, kind).await; StatusCode::OK
use db::Response::*; },
match db_resp {
Row(channel) => set_json_body(response, json!(channel)), RestrictedInput(_) => StatusCode::UNPROCESSABLE_ENTITY,
RestrictedInput(msg) => { Other(_) => StatusCode::INTERNAL_SERVER_ERROR,
eprintln!("{}", msg); _ => StatusCode::INTERNAL_SERVER_ERROR
*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
} }
} else {
StatusCode::INTERNAL_SERVER_ERROR
} }
_ => *response.status_mut() = StatusCode::BAD_REQUEST } else {
StatusCode::BAD_REQUEST
} }
} }