aboutsummaryrefslogtreecommitdiffstats
path: root/src/models
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2018-01-18 22:38:11 +0100
committerHarald Eilertsen <haraldei@anduin.net>2018-01-18 22:38:11 +0100
commitb57e94dec2a1954baf93a60749cddf3cab18a067 (patch)
treeb65866a96eb97f61710a86c340163725d6c66cf4 /src/models
parentd2eea77842189c6964b51bc75ca5a62172b77dcd (diff)
downloadrocket-blog-b57e94dec2a1954baf93a60749cddf3cab18a067.tar.gz
rocket-blog-b57e94dec2a1954baf93a60749cddf3cab18a067.tar.bz2
rocket-blog-b57e94dec2a1954baf93a60749cddf3cab18a067.zip
Begin implementing user model.
Diffstat (limited to 'src/models')
-rw-r--r--src/models/user.rs36
1 files changed, 36 insertions, 0 deletions
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<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)
+ }
+}