2019-02-28 06:02:55 +00:00
|
|
|
extern crate diesel;
|
|
|
|
use schema::user_;
|
|
|
|
use diesel::*;
|
|
|
|
use diesel::result::Error;
|
|
|
|
use schema::user_::dsl::*;
|
|
|
|
use Crud;
|
|
|
|
|
2019-03-04 16:39:07 +00:00
|
|
|
#[derive(Queryable, Identifiable, PartialEq, Debug)]
|
|
|
|
#[table_name="user_"]
|
2019-02-28 06:02:55 +00:00
|
|
|
pub struct User_ {
|
|
|
|
pub id: i32,
|
|
|
|
pub name: String,
|
2019-03-04 16:39:07 +00:00
|
|
|
pub preferred_username: Option<String>,
|
2019-02-28 06:02:55 +00:00
|
|
|
pub password_encrypted: String,
|
|
|
|
pub email: Option<String>,
|
|
|
|
pub icon: Option<Vec<u8>>,
|
2019-03-05 03:52:09 +00:00
|
|
|
pub published: chrono::NaiveDateTime,
|
|
|
|
pub updated: Option<chrono::NaiveDateTime>
|
2019-02-28 06:02:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 16:39:07 +00:00
|
|
|
#[derive(Insertable, AsChangeset, Clone, Copy)]
|
2019-02-28 06:02:55 +00:00
|
|
|
#[table_name="user_"]
|
2019-03-04 16:39:07 +00:00
|
|
|
pub struct UserForm<'a> {
|
2019-02-28 06:02:55 +00:00
|
|
|
pub name: &'a str,
|
2019-03-04 16:39:07 +00:00
|
|
|
pub preferred_username: Option<&'a str>,
|
2019-02-28 06:02:55 +00:00
|
|
|
pub password_encrypted: &'a str,
|
|
|
|
pub email: Option<&'a str>,
|
2019-03-05 03:52:09 +00:00
|
|
|
pub updated: Option<&'a chrono::NaiveDateTime>
|
2019-02-28 06:02:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 16:39:07 +00:00
|
|
|
impl<'a> Crud<UserForm<'a>> for User_ {
|
2019-02-28 06:02:55 +00:00
|
|
|
fn read(conn: &PgConnection, user_id: i32) -> User_ {
|
|
|
|
user_.find(user_id)
|
|
|
|
.first::<User_>(conn)
|
|
|
|
.expect("Error in query")
|
|
|
|
}
|
2019-03-04 16:39:07 +00:00
|
|
|
fn delete(conn: &PgConnection, user_id: i32) -> usize {
|
2019-02-28 06:02:55 +00:00
|
|
|
diesel::delete(user_.find(user_id))
|
|
|
|
.execute(conn)
|
|
|
|
.expect("Error deleting.")
|
|
|
|
}
|
2019-03-04 16:39:07 +00:00
|
|
|
fn create(conn: &PgConnection, form: UserForm) -> Result<User_, Error> {
|
|
|
|
let mut edited_user = form.clone();
|
|
|
|
// Add the rust crypt
|
|
|
|
edited_user.password_encrypted = "here";
|
|
|
|
// edited_user.password_encrypted;
|
|
|
|
insert_into(user_)
|
|
|
|
.values(edited_user)
|
|
|
|
.get_result::<User_>(conn)
|
|
|
|
}
|
|
|
|
fn update(conn: &PgConnection, user_id: i32, form: UserForm) -> User_ {
|
|
|
|
let mut edited_user = form.clone();
|
|
|
|
edited_user.password_encrypted = "here";
|
|
|
|
diesel::update(user_.find(user_id))
|
|
|
|
.set(edited_user)
|
|
|
|
.get_result::<User_>(conn)
|
|
|
|
.expect(&format!("Unable to find user {}", user_id))
|
|
|
|
}
|
2019-02-28 06:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use establish_connection;
|
2019-03-04 16:39:07 +00:00
|
|
|
use super::{User_, UserForm};
|
|
|
|
use Crud;
|
2019-02-28 06:02:55 +00:00
|
|
|
#[test]
|
|
|
|
fn test_crud() {
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
2019-03-04 16:39:07 +00:00
|
|
|
let new_user = UserForm {
|
2019-02-28 06:02:55 +00:00
|
|
|
name: "thom".into(),
|
2019-03-04 16:39:07 +00:00
|
|
|
preferred_username: None,
|
2019-02-28 06:02:55 +00:00
|
|
|
password_encrypted: "nope".into(),
|
2019-03-05 03:52:09 +00:00
|
|
|
email: None,
|
|
|
|
updated: None
|
2019-02-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
|
2019-03-04 16:39:07 +00:00
|
|
|
let inserted_user = User_::create(&conn, new_user).unwrap();
|
2019-02-28 06:02:55 +00:00
|
|
|
|
|
|
|
let expected_user = User_ {
|
|
|
|
id: inserted_user.id,
|
|
|
|
name: "thom".into(),
|
2019-03-04 16:39:07 +00:00
|
|
|
preferred_username: None,
|
2019-02-28 06:02:55 +00:00
|
|
|
password_encrypted: "here".into(),
|
|
|
|
email: None,
|
|
|
|
icon: None,
|
2019-03-05 03:52:09 +00:00
|
|
|
published: inserted_user.published,
|
|
|
|
updated: None
|
2019-02-28 06:02:55 +00:00
|
|
|
};
|
2019-03-04 16:39:07 +00:00
|
|
|
|
2019-02-28 06:02:55 +00:00
|
|
|
let read_user = User_::read(&conn, inserted_user.id);
|
2019-03-04 16:39:07 +00:00
|
|
|
let updated_user = User_::update(&conn, inserted_user.id, new_user);
|
2019-02-28 06:02:55 +00:00
|
|
|
let num_deleted = User_::delete(&conn, inserted_user.id);
|
2019-03-04 16:39:07 +00:00
|
|
|
|
2019-02-28 06:02:55 +00:00
|
|
|
assert_eq!(expected_user, read_user);
|
|
|
|
assert_eq!(expected_user, inserted_user);
|
2019-03-04 16:39:07 +00:00
|
|
|
assert_eq!(expected_user, updated_user);
|
2019-02-28 06:02:55 +00:00
|
|
|
assert_eq!(1, num_deleted);
|
|
|
|
}
|
|
|
|
}
|