Checking for valid channel.kind on /channels/create

This commit is contained in:
shockrah 2020-12-12 13:51:43 -08:00
parent 8e2b52d1c9
commit 201297f4c1

View File

@ -49,21 +49,27 @@ pub async fn create_channel(pool: &Pool, response: &mut Response<Body>, params:
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("application/json"));
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
}
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) => {
response.headers_mut().insert("Content-Type",
HeaderValue::from_static("application/json"));
*response.body_mut() = Body::from(to_string(&row).unwrap_or("{}".into()));
},
db::Response::Empty => *response.status_mut() = StatusCode::NOT_FOUND,
// TODO: loggin
db::Response::Other(msg) => {
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
eprintln!("\t{}", msg);
*response.body_mut() = Body::from(to_string(&row).unwrap_or("{}".into()));
},
db::Response::Empty => *response.status_mut() = StatusCode::NOT_FOUND,
// TODO: loggin
db::Response::Other(msg) => {
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
eprintln!("\t{}", msg);
}
_ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
}
_ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
}
},
// basically one of the parameter gets failed so we bail on all of this