list channels and create channels behaving properly based on inputs, delete_channel not working just yet
This commit is contained in:
parent
c7922d4249
commit
351a9ba30c
@ -9,8 +9,9 @@ use mysql_async::prelude::{params, Queryable};
|
|||||||
|
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use crate::db_types::{UBigInt, VarChar, Integer};
|
use crate::db_types::{DbError, UBigInt, VarChar, Integer};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum ChannelType {
|
pub enum ChannelType {
|
||||||
Voice,
|
Voice,
|
||||||
Text,
|
Text,
|
||||||
@ -59,9 +60,9 @@ pub struct Channel {
|
|||||||
kind: ChannelType
|
kind: ChannelType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
struct InsertableChannel {
|
struct InsertableChannel {
|
||||||
name: String,
|
name: String,
|
||||||
description: String,
|
|
||||||
kind: ChannelType
|
kind: ChannelType
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,11 +71,16 @@ impl Channel {
|
|||||||
* When our sql library queries things we generally get back tuples rather reasily
|
* When our sql library queries things we generally get back tuples rather reasily
|
||||||
* we can use this method to get something that makes more sense
|
* we can use this method to get something that makes more sense
|
||||||
*/
|
*/
|
||||||
fn from_tup(tup: (u64, String, String, i32)) -> Channel {
|
fn from_tup(tup: (UBigInt, VarChar, Option<VarChar>, Integer)) -> Channel {
|
||||||
|
let desc = match tup.2 {
|
||||||
|
Some(val) => val,
|
||||||
|
None => "None".into()
|
||||||
|
};
|
||||||
|
|
||||||
Channel {
|
Channel {
|
||||||
id: tup.0,
|
id: tup.0,
|
||||||
name: tup.1,
|
name: tup.1,
|
||||||
description: tup.2,
|
description: desc,
|
||||||
kind: ChannelType::from_i32(tup.3)
|
kind: ChannelType::from_i32(tup.3)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -84,7 +90,7 @@ impl Channel {
|
|||||||
* {
|
* {
|
||||||
* "id": id<i32>,
|
* "id": id<i32>,
|
||||||
* "name": "<some name here>",
|
* "name": "<some name here>",
|
||||||
* "description": "<description here>",
|
* "description": Option<"<description here>">,
|
||||||
* "kind": kind<i32>
|
* "kind": kind<i32>
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
@ -106,8 +112,7 @@ impl Channel {
|
|||||||
async fn get_channels_vec(conn: Conn) -> Result<Vec<Channel>, Error> {
|
async fn get_channels_vec(conn: Conn) -> Result<Vec<Channel>, Error> {
|
||||||
let rows_db = conn.prep_exec(r"SELECT * FROM channels", ()).await?;
|
let rows_db = conn.prep_exec(r"SELECT * FROM channels", ()).await?;
|
||||||
let (_, rows) = rows_db.map_and_drop(|row| {
|
let (_, rows) = rows_db.map_and_drop(|row| {
|
||||||
let (id, name, desc, kind): (UBigInt, VarChar, VarChar, Integer) = mysql_async::from_row(row);
|
let (id, name, desc, kind): (UBigInt, VarChar, Option<VarChar>, Integer) = mysql_async::from_row(row);
|
||||||
println!("{}, {}, {}, {}", id, name, desc, kind);
|
|
||||||
Channel::from_tup((id, name, desc, kind))
|
Channel::from_tup((id, name, desc, kind))
|
||||||
}).await?;
|
}).await?;
|
||||||
|
|
||||||
@ -150,19 +155,15 @@ pub async fn list_channels(pool: &Pool, response: &mut Response<Body>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_insert_channel_params(name_r: Option<&Value>, kind_r: Option<&Value>, desc_r: Option<&Value>) ->
|
fn parse_insert_channel_params(name_r: Option<&Value>, kind_r: Option<&Value>) ->
|
||||||
Result<InsertableChannel, Error> {
|
Result<InsertableChannel, Error> {
|
||||||
|
|
||||||
match (name_r, kind_r) {
|
match (name_r, kind_r) {
|
||||||
(Some(name), Some(kind)) => {
|
(Some(name), Some(kind)) => {
|
||||||
let desc: String = match desc_r {
|
|
||||||
Some(d) => d.as_str().unwrap().to_string(), // if this fails burn the server
|
|
||||||
None => "".into()
|
|
||||||
};
|
|
||||||
let channel = InsertableChannel {
|
let channel = InsertableChannel {
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
kind: ChannelType::from_i64_opt(kind.as_i64()),
|
kind: ChannelType::from_i64_opt(kind.as_i64()),
|
||||||
description: desc
|
|
||||||
};
|
};
|
||||||
Ok(channel)
|
Ok(channel)
|
||||||
}
|
}
|
||||||
@ -172,16 +173,25 @@ fn parse_insert_channel_params(name_r: Option<&Value>, kind_r: Option<&Value>, d
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async fn insert_channel(pool: &Pool, channel: InsertableChannel) -> Result<(), Error> {
|
async fn insert_channel(pool: &Pool, channel: InsertableChannel) -> Result<(), DbError> {
|
||||||
let conn = pool.get_conn().await?;
|
if let Ok(conn) = pool.get_conn().await {
|
||||||
conn.batch_exec(
|
let db_result: Result<Conn, Error> = conn.drop_exec(
|
||||||
r"INSERT INTO channels (name, kind, description) VALUES (:name, :kind, :description)",
|
"INSERT INTO channels (name, kind) VALUES (:name, 0)",
|
||||||
params!{
|
mysql_async::params!{ "name" => channel.name }
|
||||||
"name" => channel.name,
|
).await;
|
||||||
"kind" => channel.kind.as_i32(),
|
match db_result {
|
||||||
"description" => channel.description,
|
Ok(_) => Ok(()),
|
||||||
}).await?;
|
Err(e) => {
|
||||||
Ok(())
|
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) {
|
||||||
@ -189,16 +199,44 @@ pub async fn create_channel(pool: &Pool, response: &mut Response<Body>, params:
|
|||||||
* 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");
|
let name_r = params.get("name");
|
||||||
let desc_r = params.get("description");
|
|
||||||
let kind_r = params.get("kind");
|
let kind_r = params.get("kind");
|
||||||
|
|
||||||
match parse_insert_channel_params(name_r, kind_r, desc_r) {
|
match parse_insert_channel_params(name_r, kind_r) {
|
||||||
Ok(channel) => {
|
Ok(channel) => {
|
||||||
match insert_channel(pool, channel).await {
|
match insert_channel(pool, channel).await {
|
||||||
Ok(_) => *response.status_mut() = StatusCode::OK,
|
Ok(_) => *response.status_mut() = StatusCode::OK,
|
||||||
Err(_) => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(_) => *response.status_mut() = StatusCode::BAD_REQUEST
|
Err(_) => *response.status_mut() = StatusCode::BAD_REQUEST
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn db_delete_channel(pool: &Pool, name: &Value) -> Result<(), Error> {
|
||||||
|
let conn = pool.get_conn().await?;
|
||||||
|
let q = "DELETE FROM channels WHERE name = :name";
|
||||||
|
let p = params!{ "name" => name.as_str().unwrap() };
|
||||||
|
conn.batch_exec(q, p).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub async fn delete_channel(pool: &Pool, response: &mut Response<Body>, params: Value) {
|
||||||
|
// make sure we have the right parameters provided
|
||||||
|
if let Some(name) = params.get("name") {
|
||||||
|
match db_delete_channel(pool, name).await {
|
||||||
|
Ok(_) => *response.status_mut() = StatusCode::OK,
|
||||||
|
Err(e) => {
|
||||||
|
println!("delete_chanel sql error :\n{}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -5,3 +5,9 @@ pub type UBigInt = u64;
|
|||||||
pub type BigInt = i64;
|
pub type BigInt = i64;
|
||||||
|
|
||||||
pub type VarChar = String;
|
pub type VarChar = String;
|
||||||
|
|
||||||
|
pub enum DbError {
|
||||||
|
BadParam(&'static str),
|
||||||
|
Connection(&'static str),
|
||||||
|
Internal
|
||||||
|
}
|
||||||
|
@ -47,6 +47,7 @@ async fn route_dispatcher(pool: &Pool, resp: &mut Response<Body>, meth: &Method,
|
|||||||
},
|
},
|
||||||
(&Method::GET, routes::CHANNELS_LIST) => channels::list_channels(pool, resp).await,
|
(&Method::GET, routes::CHANNELS_LIST) => channels::list_channels(pool, resp).await,
|
||||||
(&Method::POST, routes::CHANNELS_CREATE) => channels::create_channel(pool, resp, params).await,
|
(&Method::POST, routes::CHANNELS_CREATE) => channels::create_channel(pool, resp, params).await,
|
||||||
|
(&Method::POST, routes::CHANNELS_DELETE) => channels::delete_channel(pool, resp, params).await,
|
||||||
|
|
||||||
(&Method::POST, routes::MESSAGE_SEND) => messages::send_message(pool, resp, params).await,
|
(&Method::POST, routes::MESSAGE_SEND) => messages::send_message(pool, resp, params).await,
|
||||||
_ => {
|
_ => {
|
||||||
@ -63,6 +64,7 @@ async fn route_dispatcher(pool: &Pool, resp: &mut Response<Body>, meth: &Method,
|
|||||||
println!("Dynamic part: {}", route.dynamic);
|
println!("Dynamic part: {}", route.dynamic);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
println!("NOT FOUND: {}: {}", meth, path);
|
||||||
*resp.status_mut() = StatusCode::NOT_FOUND
|
*resp.status_mut() = StatusCode::NOT_FOUND
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -96,6 +98,7 @@ async fn main_responder(request: Request<Body>) -> Result<Response<Body>, hyper:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
println!("PARSER: Parameter parsing failed");
|
||||||
*response.status_mut() = StatusCode::BAD_REQUEST;
|
*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||||
}
|
}
|
||||||
Ok(response)
|
Ok(response)
|
||||||
|
Loading…
Reference in New Issue
Block a user