use hyper::{ StatusCode, Response, Body, header::HeaderValue }; use mysql_async::Pool; use serde_json::{json, Value, to_string}; use db::{ self, common::FromDB, channels::Channel }; pub async fn list_channels(pool: &Pool, response: &mut Response) { /* * @user-params -> for now none as i don't feel like dealing with it * @TODO: add in a let var that actually */ return match db::channels::Channel::filter(pool, 0).await { db::Response::Set(channels) => { response.headers_mut().insert("Content-Type", HeaderValue::from_static("application/json")); *response.body_mut() = Body::from(to_string(&channels).unwrap_or("{}".into())) }, db::Response::Other(_msg) => *response.status_mut() = hyper::StatusCode::INTERNAL_SERVER_ERROR, _ => *response.status_mut() = hyper::StatusCode::INTERNAL_SERVER_ERROR, }; } pub async fn create_channel(pool: &Pool, response: &mut Response, params: Value) { /* * Create a channel base on a few parameters that may or may not be there * @responds with the data of the newly created channel */ // 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) = 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)) => { 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())); }, // user error that the db doesn't deal with so we just blame the user db::Response::RestrictedInput(msg) => { *response.status_mut() = StatusCode::BAD_REQUEST; response.headers_mut().insert("Content-Type", HeaderValue::from_static("application/json")); let bjson = json!({"error": msg}); *response.body_mut() = Body::from(to_string(&bjson).unwrap_or("{}".into())); }, // inserted but could not fetch db::Response::Empty => *response.status_mut() = StatusCode::NOT_FOUND, //like legit issues past here db::Response::Other(msg) => { *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; // conn issue probably eprintln!("\t[ Channels ] {}", msg); }, _ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR // ngmi } } }, // basically one of the parameter gets failed so we bail on all of this _ => *response.status_mut() = StatusCode::BAD_REQUEST } } pub async fn delete_channel(pool: &Pool, response: &mut Response, params: Value) { // make sure we have the right parameters provided if let Some(name) = params.get("channel_id") { if let Some(id) = name.as_u64() { // TODO: something more intelligent with the logging im ngl match Channel::delete(pool, id).await { db::Response::Success => {}, db::Response::Other(data) => { eprintln!("\t{}", data); *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; } _ => { eprintln!("\tBro like restart the server"); *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; } } } else { *response.status_mut() = StatusCode::BAD_REQUEST; } } else { *response.status_mut() = StatusCode::BAD_REQUEST; } }