From 9fac3aa1176567280e32fce220b4e55a2e239887 Mon Sep 17 00:00:00 2001 From: shockrah Date: Wed, 3 Feb 2021 21:09:12 -0800 Subject: [PATCH] * /join route handler now uses hashmaps among and returns more sensible http codes --- json-api/src/invites.rs | 47 +++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/json-api/src/invites.rs b/json-api/src/invites.rs index fb64a5a..0f7bc66 100644 --- a/json-api/src/invites.rs +++ b/json-api/src/invites.rs @@ -1,4 +1,4 @@ -use serde_json::Value; +use serde_json::{Value, json}; use serde::Serialize; use mysql_async; @@ -16,6 +16,8 @@ use db::{UBigInt, BigInt}; use db::common::FromDB; use db::member::Member; +use crate::http; + #[derive(Serialize)] struct Invite { @@ -96,17 +98,31 @@ async fn use_invite(pool: &Pool, code: Option) -> Option{ } } -pub async fn join(pool: &Pool, response: &mut Response, params: Value) { +pub async fn join(pool: &Pool, response: &mut Response, params: HashMap<&str, &str>) { /* * Main dispatcher for dealing with an attempted join via a given invide code + * */ - let code = match params.get("invite-id") { - Some(val) => val.as_i64(), + let code = match params.get("code") { + Some(val) => { + // i64 because we're using unix timestamps and basically the database is dumb + if let Ok(num) = (*val).to_string().parse::() { + Some(num) + } else { + None + } + + }, None => None }; + + // Returns a new member if the code is used successfully match use_invite(&pool, code).await { - Some(new_account) => *response.body_mut() = Body::from(serde_json::to_string(&new_account).unwrap()), + Some(new_account) => { + http::set_json_body(response, json!(new_account)) + }, None => { + *response.status_mut() = StatusCode::NOT_FOUND; } } } @@ -166,7 +182,7 @@ pub async fn create(pool: &Pool, response: &mut Response, params: HashMap< * expires: Option */ - let id = crate::http::extract_uid(¶ms); + let id = http::extract_uid(¶ms); let use_count = match params.get("uses") { Some(val) => { @@ -203,7 +219,7 @@ pub async fn create(pool: &Pool, response: &mut Response, params: HashMap< match insert_new_invite(&pool, &invite).await { Ok(_) => { // return the id of the invite - // Link format from here is basically hostname.io:4536/join/ + // Link format from here is basically hostname.io:4536/join?code= crate::http::set_json_body(response, serde_json::json!(invite)) }, Err(mysqle) => { @@ -227,21 +243,6 @@ mod invites_test { #[tokio::test] async fn create_invite_good() { - // Generation of data - let p = get_pool(); - let mut resp = hyper_resp(); - // expected params - let params: Value = serde_json::from_str(r#" - { - "uses": 3, - "expire": null - } - "#).unwrap(); - - // Collection - super::join(&p, &mut resp, params).await; - let _ = p.disconnect().await; - - assert_eq!(StatusCode::OK, resp.status()); + todo!() } }