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 serde_json::Value;
|
||||||
|
|
||||||
use crate::db_types::{DbError, UBigInt, VarChar, Integer};
|
use crate::db_types::{UBigInt, VarChar, Integer};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ChannelType {
|
pub enum ChannelType {
|
||||||
@ -52,7 +52,6 @@ impl ChannelType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Primary way of interpretting sql data on our channels table
|
// Primary way of interpretting sql data on our channels table
|
||||||
pub type ChannelID = u64;
|
|
||||||
pub struct Channel {
|
pub struct Channel {
|
||||||
id: u64,
|
id: u64,
|
||||||
name: String,
|
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) {
|
async fn insert_channel(pool: &Pool, name: &str, desc: &str, kind: i64) -> Result<(), Error>{
|
||||||
(Some(name), Some(kind)) => {
|
let conn = pool.get_conn().await?;
|
||||||
|
conn.prep_exec(
|
||||||
let channel = InsertableChannel {
|
"INSERT INTO channels (name, description, kind) VALUES (:name, :description, :kind)",
|
||||||
name: name.as_str().unwrap().into(), // as_str removes the quotes from the output
|
params!{"name" => name, "kind" => kind, "description" => desc}).await?;
|
||||||
kind: ChannelType::from_i64_opt(kind.as_i64()),
|
Ok(())
|
||||||
};
|
|
||||||
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"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_channel(pool: &Pool, response: &mut Response<Body>, params: Value) {
|
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
|
* Create a channel base on a few parameters that may or may not be there
|
||||||
*/
|
*/
|
||||||
let name_r = params.get("name");
|
// Theres an extra un-needed unwrap to be cut out from this proc
|
||||||
let kind_r = params.get("kind");
|
// specifically with the desc parameter
|
||||||
|
let req_params: (Option<&str>, Option<&str>, Option<i64>) =
|
||||||
match parse_insert_channel_params(name_r, kind_r) {
|
match (params.get("name"), params.get("description"), params.get("kind")) {
|
||||||
Ok(channel) => {
|
(Some(name), Some(desc), Some(kind)) => (name.as_str(), desc.as_str(), kind.as_i64()),
|
||||||
match insert_channel(pool, channel).await {
|
(Some(name), None, Some(kind)) => (name.as_str(), Some("No Description"), kind.as_i64()),
|
||||||
Ok(_) => *response.status_mut() = StatusCode::OK,
|
_ => (None, None, None)
|
||||||
Err(dbe) => {
|
};
|
||||||
// Todo: add some form of loggin off the back of these values
|
match req_params {
|
||||||
match dbe {
|
(Some(name), Some(desc), Some(kind)) => {
|
||||||
DbError::BadParam(_msg) => *response.status_mut() = StatusCode::BAD_REQUEST,
|
match insert_channel(pool, name, desc, kind).await {
|
||||||
DbError::Connection(_msg) => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR,
|
// Server Errors are generally _ok_ to reveal in body I suppose
|
||||||
DbError::Internal => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
|
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