From b57e94dec2a1954baf93a60749cddf3cab18a067 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 18 Jan 2018 22:38:11 +0100 Subject: Begin implementing user model. --- src/models/user.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/models/user.rs diff --git a/src/models/user.rs b/src/models/user.rs new file mode 100644 index 0000000..0a41e3d --- /dev/null +++ b/src/models/user.rs @@ -0,0 +1,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, + pub email: Option, + pub password: String +} + +#[derive(Default, FromForm, Insertable)] +#[table_name="users"] +pub struct NewUser { + pub username; String, + pub realname: Option, + pub email: Option, + pub password: String, +} + +impl User { + pub fn by_id(user_id: i32, conn: utils::DbConn) -> QueryResult { + use schema::users::dsl::*; + users.filter(id.eq(user_id)).load::(&*conn).map(|v| v.pop()) + } + + pub fn create(new_user: &NewUser, conn: utils::DbConn) -> QueryResult { + use ::schema::posts::dsl::*; + diesel::insert_into(users) + .values(new_user) + .get_result(&*conn) + } +} -- cgit v1.2.3