* /join route handler now uses hashmaps among and returns more sensible http codes

This commit is contained in:
shockrah 2021-02-03 21:09:12 -08:00
parent 98803eec26
commit 9fac3aa117

View File

@ -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<BigInt>) -> Option<Member>{
}
}
pub async fn join(pool: &Pool, response: &mut Response<Body>, params: Value) {
pub async fn join(pool: &Pool, response: &mut Response<Body>, 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::<i64>() {
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<Body>, params: HashMap<
* expires: Option<bool>
*/
let id = crate::http::extract_uid(&params);
let id = http::extract_uid(&params);
let use_count = match params.get("uses") {
Some(val) => {
@ -203,7 +219,7 @@ pub async fn create(pool: &Pool, response: &mut Response<Body>, 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/<id>
// Link format from here is basically hostname.io:4536/join?code=<some-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!()
}
}