aboutsummaryrefslogtreecommitdiffstats
path: root/src/models/user.rs
blob: 0a41e3d0031261bc41d67b24f5fcdae23e84483e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use diesel;
use diesel::prelude::*;
use schema::users;
use utils;

#[derive(AsChangeset, FromForm, Identifiable, Serialize, Queryable)]
pub struct User {
    pub id: i32,
    pub username: String,
    pub realname: Option<String>,
    pub email: Option<String>,
    pub password: String
}

#[derive(Default, FromForm, Insertable)]
#[table_name="users"]
pub struct NewUser {
    pub username; String,
    pub realname: Option<String>,
    pub email: Option<String>,
    pub password: String,
}

impl User {
    pub fn by_id(user_id: i32, conn: utils::DbConn) -> QueryResult<User> {
        use schema::users::dsl::*;
        users.filter(id.eq(user_id)).load::<User>(&*conn).map(|v| v.pop())
    }

    pub fn create(new_user: &NewUser, conn: utils::DbConn) -> QueryResult<User> {
        use ::schema::posts::dsl::*;
        diesel::insert_into(users)
            .values(new_user)
            .get_result(&*conn)
    }
}