diff --git a/server/src/invites.rs b/server/src/invites.rs index 52dc15b..967fec1 100644 --- a/server/src/invites.rs +++ b/server/src/invites.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use serde_json::Value; use mysql_async; use mysql_async::{Conn, Pool}; @@ -86,25 +86,24 @@ async fn record_invite_usage(pool: &Pool, data: &InviteRow) -> Result<(), Error> Ok(()) } -pub async fn join_invite_code(pool: &Pool, response: &mut Response
, params: &HashMap<&str, &str>) -> Result<(), Error> { +pub async fn route_join_invite_code(pool: &Pool, response: &mut Response, params: Value) -> Result<(), Error> { // First check that the code is there - match params.get("code") { - Some(p) => { - if let Some(row) = get_invite_by_code(pool, Some(*p)).await? { - // since we have a row make sure the invite is valid - let now = Utc::now().timestamp() as u64; - // usable and expires in the future - if row.uses > 0 && row.expires > now { - record_invite_usage(pool, &row).await?; - // TODO: assign some actual data to the body - *response.status_mut() = StatusCode::OK; - } + if let Some(code) = params.get("code") { + if let Some(row) = get_invite_by_code(pool, code.as_str()).await? { + // since we have a row make sure the invite is valid + let now = Utc::now().timestamp() as u64; + // usable and expires in the future + if row.uses > 0 && row.expires > now { + record_invite_usage(pool, &row).await?; + // TODO: assign some actual data to the body + *response.status_mut() = StatusCode::OK; } - }, - None => { - *response.status_mut() = StatusCode::BAD_REQUEST; } } + else{ + *response.status_mut() = StatusCode::BAD_REQUEST; + } + Ok(()) } diff --git a/server/src/main.rs b/server/src/main.rs index 38407e4..e222398 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -31,11 +31,11 @@ mod badges; mod http_params; -async fn route_dispatcher(pool: &Pool, resp: &mut Response, meth: &Method, path: &str, params: &HashMap<&str, &str>) { +async fn route_dispatcher(pool: &Pool, resp: &mut Response, meth: &Method, path: &str, params: serde_json::Value) { // At some point we should have some way of hiding this obnoxious complexity match (meth, path) { (&Method::GET, routes::INVITE_JOIN) => { - if let Err(_) = invites::join_invite_code(pool, resp, params).await { + if let Err(_) = invites::route_join_invite_code(pool, resp, params).await { *resp.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; } }, @@ -55,23 +55,29 @@ async fn route_dispatcher(pool: &Pool, resp: &mut Response, meth: &Method, async fn main_responder(request: Request) -> Result