Channel
changenotes:
* create channel now responds correctly given various parameters * insert channel now errors out properly with ? syntax * new uses added to align with sql types
This commit is contained in:
parent
df395e3b50
commit
d588128b9e
@ -9,7 +9,7 @@ use mysql_async::prelude::{params, Queryable};
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::db_types::{DbError, UBigInt, VarChar, Integer};
|
||||
use crate::db_types::{UBigInt, VarChar, Integer};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ChannelType {
|
||||
@ -52,7 +52,6 @@ impl ChannelType {
|
||||
}
|
||||
|
||||
// Primary way of interpretting sql data on our channels table
|
||||
pub type ChannelID = u64;
|
||||
pub struct Channel {
|
||||
id: u64,
|
||||
name: String,
|
||||
@ -155,68 +154,46 @@ pub async fn list_channels(pool: &Pool, response: &mut Response<Body>) {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_insert_channel_params(name_r: Option<&Value>, kind_r: Option<&Value>) ->
|
||||
Result<InsertableChannel, Error> {
|
||||
|
||||
match (name_r, kind_r) {
|
||||
(Some(name), Some(kind)) => {
|
||||
|
||||
let channel = InsertableChannel {
|
||||
name: name.as_str().unwrap().into(), // as_str removes the quotes from the output
|
||||
kind: ChannelType::from_i64_opt(kind.as_i64()),
|
||||
};
|
||||
println!("Insertable channel name: {}", channel.name);
|
||||
Ok(channel)
|
||||
}
|
||||
_ => {
|
||||
let x = Cow::from("Missing required parameters");
|
||||
Err(Error::Other(x))
|
||||
}
|
||||
}
|
||||
}
|
||||
async fn insert_channel(pool: &Pool, channel: InsertableChannel) -> Result<(), DbError> {
|
||||
if let Ok(conn) = pool.get_conn().await {
|
||||
let db_result: Result<Conn, Error> = conn.drop_exec(
|
||||
"INSERT INTO channels (name, kind) VALUES (:name, 0)",
|
||||
params!{"name" => channel.name}
|
||||
).await;
|
||||
match db_result {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => {
|
||||
match e {
|
||||
Error::Server(_) => Err(DbError::BadParam("Couldn't process input somehow")),
|
||||
_ => Err(DbError::Internal)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
Err(DbError::Connection("Could not connect to database at all"))
|
||||
}
|
||||
async fn insert_channel(pool: &Pool, name: &str, desc: &str, kind: i64) -> Result<(), Error>{
|
||||
let conn = pool.get_conn().await?;
|
||||
conn.prep_exec(
|
||||
"INSERT INTO channels (name, description, kind) VALUES (:name, :description, :kind)",
|
||||
params!{"name" => name, "kind" => kind, "description" => desc}).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn create_channel(pool: &Pool, response: &mut Response<Body>, params: Value) {
|
||||
/*
|
||||
* Create a channel base on a few parameters that may or may not be there
|
||||
*/
|
||||
let name_r = params.get("name");
|
||||
let kind_r = params.get("kind");
|
||||
|
||||
match parse_insert_channel_params(name_r, kind_r) {
|
||||
Ok(channel) => {
|
||||
match insert_channel(pool, channel).await {
|
||||
Ok(_) => *response.status_mut() = StatusCode::OK,
|
||||
Err(dbe) => {
|
||||
// Todo: add some form of loggin off the back of these values
|
||||
match dbe {
|
||||
DbError::BadParam(_msg) => *response.status_mut() = StatusCode::BAD_REQUEST,
|
||||
DbError::Connection(_msg) => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR,
|
||||
DbError::Internal => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
|
||||
}
|
||||
// Theres an extra un-needed unwrap to be cut out from this proc
|
||||
// specifically with the desc parameter
|
||||
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)) => {
|
||||
match insert_channel(pool, name, desc, kind).await {
|
||||
// Server Errors are generally _ok_ to reveal in body I suppose
|
||||
Err(Error::Server(se)) => {
|
||||
*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||
let b = format!("Server code: {}\nServer Message:{}", se.code, se.message);
|
||||
*response.body_mut() = Body::from(b);
|
||||
},
|
||||
// generic errors get a 500
|
||||
Err(_) => {
|
||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
// Nothing to do when things go right
|
||||
_ => {}
|
||||
}
|
||||
},
|
||||
Err(_) => *response.status_mut() = StatusCode::BAD_REQUEST
|
||||
// basically one of the parameter gets failed so we bail on all of this
|
||||
_ => *response.status_mut() = StatusCode::BAD_REQUEST
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user