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()));
|
||||
},
|
||||
db::Response::Empty => {},
|
||||
db::Response::Other(msg) => {},
|
||||
_ => {*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;}
|
||||
db::Response::Empty => *response.status_mut() = StatusCode::NOT_FOUND,
|
||||
db::Response::Other(msg) => *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
|
||||
|
@ -10,69 +10,32 @@ use chrono::Utc;
|
||||
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) {
|
||||
/*
|
||||
* @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");
|
||||
let channel_name_r = params.get("channel");
|
||||
// auth module guarantees this will be there in the correct form
|
||||
// NOTE: auth module guarantees this will be there in the correct form
|
||||
let author = params.get("id")
|
||||
.unwrap().as_u64().unwrap();
|
||||
|
||||
match (content_r, channel_name_r) {
|
||||
(Some(content), Some(channel_name)) => {
|
||||
match insert_message(pool, content, channel_name, author).await {
|
||||
Ok(_) => *response.status_mut() = StatusCode::OK,
|
||||
Err(err) => {
|
||||
use mysql_async::error::Error::{Server};
|
||||
println!("\tDB Error::send_message: {:?}", err);
|
||||
// doing this to avoid client confusion as some input does cause sql errors
|
||||
if let Server(se) = err {
|
||||
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;
|
||||
}
|
||||
match (params.get("conetnt") , params.get("channel")) {
|
||||
(Some(content_v), Some(channel_id_v)) => {
|
||||
let (content, channel) = (content_v.as_str(), channel_id_v.as_u64());
|
||||
|
||||
if let (Some(message), Some(cid)) = (content, channel) {
|
||||
// call returns empty on sucess so we don't need to do anything
|
||||
if let Err(issue) = db::messages::Message::send(pool, message, cid, author).await {
|
||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
// log(send)
|
||||
}
|
||||
// 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.body_mut() = Body::from("content/channel missing from json parameters");
|
||||
}
|
||||
_ => *response.status_mut() = StatusCode::BAD_REQUEST
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user