aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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)
+ }
+}