Message
changenotes
* send_message fails when a non-existant channel is specified * send_message now works with existant channel * send_message: notifies client of bad channel parameters * send_message: sends a generic 500 on every other error
This commit is contained in:
parent
d588128b9e
commit
6416370e95
@ -10,17 +10,21 @@ use chrono::Utc;
|
|||||||
use crate::db_types::{UBigInt};
|
use crate::db_types::{UBigInt};
|
||||||
|
|
||||||
|
|
||||||
pub async fn insert_message_table(pool: &Pool, content: &Value, channel_name: &Value, author_id: UBigInt)
|
pub async fn insert_message(pool: &Pool, content: &Value, channel_name: &Value, author_id: UBigInt)
|
||||||
-> Result<(), Error>{
|
-> Result<(), Error>{
|
||||||
match (content.as_str(), channel_name.as_str()) {
|
match (content.as_str(), channel_name.as_str()) {
|
||||||
(Some(content), Some(channel)) => {
|
(Some(content), Some(channel)) => {
|
||||||
let conn = pool.get_conn().await?;
|
let conn = pool.get_conn().await?;
|
||||||
let time = Utc::now().timestamp();
|
let time = Utc::now().timestamp();
|
||||||
conn.prep_exec(r"", params!{
|
conn.prep_exec(
|
||||||
"time" => time,
|
r"INSERT INTO messages
|
||||||
"content" => content,
|
(time, content, author_id, channel_name)
|
||||||
"author" => author_id,
|
VALUES(:time, :content, :author, :channel)",
|
||||||
"channel" => channel
|
params!{
|
||||||
|
"time" => time,
|
||||||
|
"content" => content,
|
||||||
|
"author" => author_id,
|
||||||
|
"channel" => channel
|
||||||
}).await?;
|
}).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -37,16 +41,32 @@ pub async fn send_message(pool: &Pool, response: &mut Response<Body>, params: Va
|
|||||||
* @id: expecting the channel id that we're posting data to
|
* @id: expecting the channel id that we're posting data to
|
||||||
*/
|
*/
|
||||||
let content_r = params.get("content");
|
let content_r = params.get("content");
|
||||||
let channel_id_r = params.get("channel");
|
let channel_name_r = params.get("channel");
|
||||||
// auth module guarantees this will be there in the correct form
|
// auth module guarantees this will be there in the correct form
|
||||||
let author = params.get("userid")
|
let author = params.get("userid")
|
||||||
.unwrap().as_u64().unwrap();
|
.unwrap().as_u64().unwrap();
|
||||||
|
|
||||||
match (content_r, channel_id_r) {
|
match (content_r, channel_name_r) {
|
||||||
(Some(content), Some(channel_id)) => {
|
(Some(content), Some(channel_name)) => {
|
||||||
// insert the new message into our db
|
match insert_message(pool, content, channel_name, author).await {
|
||||||
if let Ok(_db_result) = insert_message_table(pool, content, channel_id, author).await {
|
Ok(_) => *response.status_mut() = StatusCode::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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
|
Loading…
Reference in New Issue
Block a user