Updated blanknewUser macro
use_invite now returns a Json<User>
This commit is contained in:
parent
d7c70afd68
commit
d9e892b1e0
@ -2,19 +2,18 @@
|
|||||||
use diesel::{self, prelude::*};
|
use diesel::{self, prelude::*};
|
||||||
use rocket_contrib::json::Json;
|
use rocket_contrib::json::Json;
|
||||||
use rand::random;
|
use rand::random;
|
||||||
use chrono::{Duration, Utc};
|
|
||||||
use crate::DBConn;
|
use crate::DBConn;
|
||||||
use crate::models::Invite;
|
use crate::models::{User, Invite};
|
||||||
use crate::schema;
|
use crate::schema;
|
||||||
use crate::payload::NewUserInfo;
|
use chrono::{Duration, Utc};
|
||||||
use crate::rand_utils::{newUserID, newKeyHash};
|
|
||||||
|
|
||||||
macro_rules! blankNewUser {
|
macro_rules! blankNewUser {
|
||||||
() => {
|
() => {
|
||||||
NewUserInfo {
|
User {
|
||||||
userid: 0,
|
userid: 0,
|
||||||
key_hash: None,
|
username: "".into(),
|
||||||
valid: false
|
key: "".into(),
|
||||||
|
date: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -51,11 +50,10 @@ pub fn generate_invite(conn: DBConn) -> Json<Invite> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[get("/<hash>")]
|
#[get("/<hash>")]
|
||||||
pub fn use_invite(hash: u64, conn: DBConn) -> Json<NewUserInfo> {
|
pub fn use_invite(hash: u64, conn: DBConn) -> Json<User> {
|
||||||
// Grab the token from our table to make sure it's even there to begin with
|
// Grab the token from our table to make sure it's even there to begin with
|
||||||
use schema::invites::dsl::*;
|
use schema::invites::dsl::*;
|
||||||
use rand::{thread_rng, Rng};
|
use schema::users::dsl::*;
|
||||||
use rand::distributions::Alphanumeric;
|
|
||||||
|
|
||||||
// NOTE: collection of 1 item from the table could be done cleaner
|
// NOTE: collection of 1 item from the table could be done cleaner
|
||||||
let data = invites.select((id, expires, uses))
|
let data = invites.select((id, expires, uses))
|
||||||
@ -63,26 +61,16 @@ pub fn use_invite(hash: u64, conn: DBConn) -> Json<NewUserInfo> {
|
|||||||
.first::<Invite>(&conn.0)
|
.first::<Invite>(&conn.0)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let _data: Result<Invite, diesel::result::Error> = invites.filter(id.eq(hash)).first(&conn.0);
|
let diesel_result: Result<Invite, diesel::result::Error> = invites.filter(id.eq(hash)).first(&conn.0);
|
||||||
// We're going to need more secure random numbers but for now this will do
|
// TODO: this is getting moved elsewhere to clean up so ignore this for now
|
||||||
match data {
|
match diesel_result {
|
||||||
Ok(d) => {
|
Ok(data) => {
|
||||||
if d.id == hash {
|
// we may have an unnecssary struct here but its not that big a deal raelly
|
||||||
// create the new user
|
let user = crate::users::createNewUser();
|
||||||
let new = NewUserInfo {
|
|
||||||
userid: newUserID(),
|
|
||||||
key_hash: newKeyHash(),
|
|
||||||
valid: true
|
|
||||||
};
|
|
||||||
// save the user info and finally return it back to the caller
|
|
||||||
diesel::insert_into(users)
|
diesel::insert_into(users)
|
||||||
.values(&new)
|
.values(&user)
|
||||||
.execute(&conn.0);
|
.execute(&conn.0);
|
||||||
Json(new)
|
Json(user)
|
||||||
}
|
|
||||||
else {
|
|
||||||
Json(blankNewUser!())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Err(_e) => {
|
Err(_e) => {
|
||||||
Json(blankNewUser!())
|
Json(blankNewUser!())
|
||||||
@ -95,7 +83,7 @@ mod invite_tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use rocket;
|
use rocket;
|
||||||
use rocket::local::Client;
|
use rocket::local::Client;
|
||||||
use rocket::http::{Status, ContentType};
|
use rocket::http::Status;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn make_invite() {
|
fn make_invite() {
|
||||||
|
Loading…
Reference in New Issue
Block a user