handling result from diesel
generally:
fixed pub/private issue in modles module
renamed rand_utils functions to be snake case
This commit is contained in:
shockrah 2020-03-08 20:07:26 -07:00
parent 89836449a1
commit e4f03b951e
4 changed files with 19 additions and 23 deletions

View File

@ -55,22 +55,18 @@ pub fn use_invite(hash: u64, conn: DBConn) -> Json<User> {
use schema::invites::dsl::*;
use schema::users::dsl::*;
// NOTE: collection of 1 item from the table could be done cleaner
let data = invites.select((id, expires, uses))
.filter(id.eq(hash))
.first::<Invite>(&conn.0)
.unwrap();
let diesel_result: Result<Invite, diesel::result::Error> = invites.filter(id.eq(hash)).first(&conn.0);
// TODO: this is getting moved elsewhere to clean up so ignore this for now
match diesel_result {
Ok(data) => {
Ok(_data) => {
// we may have an unnecssary struct here but its not that big a deal raelly
let user = crate::users::createNewUser();
diesel::insert_into(users)
.values(&user)
.execute(&conn.0);
Json(user)
let user = crate::users::create_new_user();
if let Ok(_v) = diesel::insert_into(users).values(&user).execute(&conn.0) {
Json(user)
}
else {
Json(blankNewUser!())
}
}
Err(_e) => {
Json(blankNewUser!())

View File

@ -1,4 +1,4 @@
use crate::schema::{invites, new_users};
use crate::schema::{invites, users};
#[derive(Insertable, Serialize, Deserialize, Queryable, Debug)]
#[table_name = "invites"]
pub struct Invite {
@ -10,8 +10,8 @@ pub struct Invite {
#[derive(Serialize, Deserialize, Queryable, Insertable, Debug)]
#[table_name = "users"]
pub struct User {
userid: u64,
username: String,
key: String,
date: u64,
pub userid: u64,
pub username: String,
pub key: String,
pub date: u64,
}

View File

@ -1,7 +1,7 @@
// This modules mainly deals with creating various types of random data
use getrandom::getrandom;
pub fn newUserID() -> u64 {
pub fn new_user_id() -> u64 {
let mut buf = [0u8; 8];
match getrandom::getrandom(&mut buf) { // honestly if this fails idk wtf you want
Ok(_val) => {u64::from_ne_bytes(buf)}
@ -9,7 +9,7 @@ pub fn newUserID() -> u64 {
}
}
pub fn newKey() -> String { // Returns a random string which we later hash with bcrypt
pub fn new_key() -> String { // Returns a random string which we later hash with bcrypt
let mut raw_slice = [0u8; 32];
if let Ok(_ignored) = getrandom(&mut raw_slice) {
let mut buf = String::new();

View File

@ -1,12 +1,12 @@
use chrono::Utc;
use crate::rand_utils::{newUserID, newKey};
use crate::rand_utils::{new_user_id, new_key};
use crate::models::User;
// Returns a struct of payload::NewUserInfo
pub fn createNewUser() -> User {
let uid = newUserID();
pub fn create_new_user() -> User {
let uid = new_user_id();
let uname = format!("User:#{}", uid);
let rstring = newKey();
let rstring = new_key();
User {
userid: uid,
username: uname,