Last active
September 11, 2020 19:28
-
-
Save danjenson/e51c2d54badd37fd7d64f40141be0d68 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use crate::db::Conn; | |
| use crate::schema::users; | |
| use chrono::{DateTime, Utc}; | |
| use diesel::{ | |
| associations::{HasTable, Identifiable}, | |
| delete, insert_into, | |
| result::QueryResult, | |
| ExpressionMethods, QueryDsl, RunQueryDsl, | |
| }; | |
| use serde::{Deserialize, Serialize}; | |
| use uuid::Uuid; | |
| #[derive( | |
| Debug, | |
| Eq, | |
| PartialEq, | |
| Insertable, | |
| Identifiable, | |
| Queryable, | |
| Serialize, | |
| Deserialize, | |
| )] | |
| #[primary_key(uuid)] | |
| pub struct User { | |
| pub created_at: DateTime<Utc>, | |
| pub updated_at: DateTime<Utc>, | |
| pub uuid: Uuid, | |
| pub display_name: String, | |
| pub name: String, | |
| pub email: String, | |
| } | |
| impl User { | |
| pub fn create( | |
| conn: &Conn, | |
| display_name: &str, | |
| name: &str, | |
| email: &str, | |
| ) -> QueryResult<Self> { | |
| insert_into(users::table) | |
| .values(( | |
| users::display_name.eq(display_name), | |
| users::name.eq(name), | |
| users::email.eq(email), | |
| )) | |
| .get_result(conn) | |
| } | |
| pub fn get<'ident>( | |
| conn: &Conn, | |
| pk: <&'ident Self as Identifiable>::Id, | |
| ) -> QueryResult<Self> { | |
| <Self as HasTable>::table().find(pk).first(conn) | |
| } | |
| pub fn delete(&self, conn: &Conn) -> QueryResult<usize> { | |
| delete(self).execute(conn) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment