* /join route handler now uses hashmaps among and returns more sensible http codes
This commit is contained in:
parent
98803eec26
commit
9fac3aa117
@ -1,4 +1,4 @@
|
|||||||
use serde_json::Value;
|
use serde_json::{Value, json};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
use mysql_async;
|
use mysql_async;
|
||||||
@ -16,6 +16,8 @@ use db::{UBigInt, BigInt};
|
|||||||
use db::common::FromDB;
|
use db::common::FromDB;
|
||||||
use db::member::Member;
|
use db::member::Member;
|
||||||
|
|
||||||
|
use crate::http;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Invite {
|
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
|
* Main dispatcher for dealing with an attempted join via a given invide code
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
let code = match params.get("invite-id") {
|
let code = match params.get("code") {
|
||||||
Some(val) => val.as_i64(),
|
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
|
None => None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Returns a new member if the code is used successfully
|
||||||
match use_invite(&pool, code).await {
|
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 => {
|
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>
|
* expires: Option<bool>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
let id = crate::http::extract_uid(¶ms);
|
let id = http::extract_uid(¶ms);
|
||||||
|
|
||||||
let use_count = match params.get("uses") {
|
let use_count = match params.get("uses") {
|
||||||
Some(val) => {
|
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 {
|
match insert_new_invite(&pool, &invite).await {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
// return the id of the invite
|
// 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))
|
crate::http::set_json_body(response, serde_json::json!(invite))
|
||||||
},
|
},
|
||||||
Err(mysqle) => {
|
Err(mysqle) => {
|
||||||
@ -227,21 +243,6 @@ mod invites_test {
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn create_invite_good() {
|
async fn create_invite_good() {
|
||||||
// Generation of data
|
todo!()
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user