Skip to content

Instantly share code, notes, and snippets.

@timxor
Created July 19, 2018 06:01
Show Gist options
  • Save timxor/1d4bbec9e859fb44c3532151837e390e to your computer and use it in GitHub Desktop.
Save timxor/1d4bbec9e859fb44c3532151837e390e to your computer and use it in GitHub Desktop.
#![feature(plugin)]
#[macro_use] extern crate diesel;
#[macro_use] extern crate serde_json;
#[macro_use] extern crate serde_derive;
extern crate dotenv;
//extern crate uuid;
pub mod schema;
pub mod models;
use diesel::prelude::*;
use diesel::pg::PgConnection;
use dotenv::dotenv;
use std::env;
//use uuid::Uuid;
use self::models::{User, NewUser};
pub fn establish_connection() -> PgConnection {
dotenv().ok();
let database_url = env::var("DATABASE_URL")
.expect("DATABASE_URL must be set");
PgConnection::establish(&database_url)
.expect(&format!("Error connecting to {}", database_url))
}
pub fn create_user<'a>(conn: &PgConnection,
first_name: &'a str,
last_name: &'a str,
user_name: &'a str,
cell_number: i32,
cell_verified: bool,
email: &'a str,
public_key: &'a str,
private_key: &'a str,
eth_address: &'a str, ) -> () {
use schema::users;
let new_user = NewUser {
id: None,
first_name: first_name,
last_name: last_name,
user_name: user_name,
cell_number: cell_number,
cell_verified: cell_verified,
email: email,
public_key: public_key,
private_key: private_key,
eth_address: eth_address,
};
diesel::insert_into(users::table)
.values(&new_user)
.execute(conn)
.expect("Error creating new user");
// users::table.order(users::id.desc()).first(conn).unwrap()
}
//pub fn create(user: User, connection: &PgConnection) -> User {
// use schema::users;
//
// diesel::insert_into(users::table)
// .values(&user)
// .execute(connection)
// .expect("Error creating new user");
//
// users::table.order(users::id.desc()).first(connection).unwrap()
// }
//
// pub fn read(connection: &PgConnection) -> Vec<User> {
// use schema::users;
//
// users::table.order(users::id.asc())
// .load::<User>(connection)
// .unwrap()
// }
//
// pub fn update(id: i32, user: User, connection: &PgConnection) -> bool {
// use schema::users;
//
// diesel::update(users::table.find(id)).set(&user).execute(connection).is_ok()
// }
//
// pub fn delete(id: i32, connection: &PgConnection) -> bool {
// use schema::users;
//
// diesel::delete(users::table.find(id))
// .execute(connection)
// .is_ok()
// }
//pub fn create_transfer(conn: &PgConnection, amount: &str, currency: &str,
// to_name: &str, to_number: &str, to_email: &str,
// complete: &bool, ) -> Transfer {
// use schema::transfers;
//
// let new_transfer = NewTransfer {
// amount: amount,
// currency: currency,
// to_name: to_name,
// to_number: to_number,
// to_email: to_email,
// complete: complete,
// };
//
// diesel::insert_into(transfers::table)
// .values(&new_transfer)
// .get_result(conn)
// .expect("Error saving new post")
//}
//// todo add create_user function
//pub fn create_user(conn: &PgConnection, name: String, identity: String, hometown: String, age: i32) ->
// User {
// use schema::users;
//
// let new_user = User {
// name: name,
// identity: identity,
// hometown: hometown,
// age: age,
// };
//
// diesel::insert_into(users::table)
// .values(&new_user)
// .get_result(conn)
// .expect("Error saving new post")
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment