User API changes to now use the new behavior for adding channels

This commit is contained in:
shockrah 2020-11-07 18:27:14 -08:00
parent 4668ce7d0f
commit c8a6aa204d

View File

@ -38,14 +38,29 @@ 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 std::convert::TryInto;
let req_params: (Option<&str>, Option<&str>, Option<i64>) =
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)
};
match req_params {
(Some(name), Some(desc), Some(kind)) => {
// 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) => {
response.headers_mut().insert("Content type",
HeaderValue::from_static("json/application"));
*response.body_mut() = Body::from(to_string(&row).unwrap_or("{}".into()));
},
db::Response::Empty => {},
db::Response::Other(msg) => {},
_ => {*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;}
}
},
// basically one of the parameter gets failed so we bail on all of this
_ => *response.status_mut() = StatusCode::BAD_REQUEST