Finally the change over to db::messages::Messages::send in userland code
- removed old helper function which is no longer needed * cleaned up responses for empty,other, and _ in /channels/create endpoint handler
This commit is contained in:
parent
d615a41c0c
commit
b966c61c20
@ -57,9 +57,9 @@ pub async fn create_channel(pool: &Pool, response: &mut Response<Body>, params:
|
|||||||
|
|
||||||
*response.body_mut() = Body::from(to_string(&row).unwrap_or("{}".into()));
|
*response.body_mut() = Body::from(to_string(&row).unwrap_or("{}".into()));
|
||||||
},
|
},
|
||||||
db::Response::Empty => {},
|
db::Response::Empty => *response.status_mut() = StatusCode::NOT_FOUND,
|
||||||
db::Response::Other(msg) => {},
|
db::Response::Other(msg) => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
_ => {*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
|
// basically one of the parameter gets failed so we bail on all of this
|
||||||
|
@ -10,69 +10,32 @@ use chrono::Utc;
|
|||||||
use db::UBigInt;
|
use db::UBigInt;
|
||||||
|
|
||||||
|
|
||||||
pub async fn insert_message(pool: &Pool, content: &Value, channel_name: &Value, author_id: UBigInt)
|
|
||||||
-> Result<(), Error>{
|
|
||||||
match (content.as_str(), channel_name.as_str()) {
|
|
||||||
(Some(content), Some(channel)) => {
|
|
||||||
let conn = pool.get_conn().await?;
|
|
||||||
let time = Utc::now().timestamp();
|
|
||||||
conn.prep_exec(
|
|
||||||
r"INSERT INTO messages
|
|
||||||
(time, content, author_id, channel_name)
|
|
||||||
VALUES(:time, :content, :author, :channel)",
|
|
||||||
params!{
|
|
||||||
"time" => time,
|
|
||||||
"content" => content,
|
|
||||||
"author" => author_id,
|
|
||||||
"channel" => channel
|
|
||||||
}).await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
let e = Cow::from("Required parameter missing");
|
|
||||||
Err(Error::Other(e))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn send_message(pool: &Pool, response: &mut Response<Body>, params: Value) {
|
pub async fn send_message(pool: &Pool, response: &mut Response<Body>, params: Value) {
|
||||||
/*
|
/*
|
||||||
* @content: expecting string type
|
* @content: expecting string type
|
||||||
* @id: expecting the channel id that we're posting data to
|
* @channel: channel id that we're going to send a message to
|
||||||
*/
|
*/
|
||||||
let content_r = params.get("content");
|
// NOTE: auth module guarantees this will be there in the correct form
|
||||||
let channel_name_r = params.get("channel");
|
|
||||||
// auth module guarantees this will be there in the correct form
|
|
||||||
let author = params.get("id")
|
let author = params.get("id")
|
||||||
.unwrap().as_u64().unwrap();
|
.unwrap().as_u64().unwrap();
|
||||||
|
|
||||||
match (content_r, channel_name_r) {
|
match (params.get("conetnt") , params.get("channel")) {
|
||||||
(Some(content), Some(channel_name)) => {
|
(Some(content_v), Some(channel_id_v)) => {
|
||||||
match insert_message(pool, content, channel_name, author).await {
|
let (content, channel) = (content_v.as_str(), channel_id_v.as_u64());
|
||||||
Ok(_) => *response.status_mut() = StatusCode::OK,
|
|
||||||
Err(err) => {
|
if let (Some(message), Some(cid)) = (content, channel) {
|
||||||
use mysql_async::error::Error::{Server};
|
// call returns empty on sucess so we don't need to do anything
|
||||||
println!("\tDB Error::send_message: {:?}", err);
|
if let Err(issue) = db::messages::Message::send(pool, message, cid, author).await {
|
||||||
// doing this to avoid client confusion as some input does cause sql errors
|
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||||
if let Server(se) = err {
|
// log(send)
|
||||||
if se.code == 1452 {
|
|
||||||
*response.status_mut() = StatusCode::BAD_REQUEST;
|
|
||||||
*response.body_mut() = Body::from(format!("{} does not exist", channel_name));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// if there's no issue then we don't need to do anything at all
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => {
|
_ => *response.status_mut() = StatusCode::BAD_REQUEST
|
||||||
*response.status_mut() = StatusCode::BAD_REQUEST;
|
|
||||||
*response.body_mut() = Body::from("content/channel missing from json parameters");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user