Last active
May 1, 2019 15:08
-
-
Save Wassasin/cc987c8945ce5f63c9604a1f3cd584c4 to your computer and use it in GitHub Desktop.
Real API's code examples
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
| derive_crud!(Message; NewMessage; messages); |
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
| fn try_find( | |
| id: Id<$struct_name>, | |
| connection: &PgConnection, | |
| ) -> Result<Option<$struct_name>, ::error::Error> { | |
| let result = $schema::table | |
| .filter($schema::id.eq(&id)) | |
| .first::<$struct_name>(connection); | |
| match result { | |
| Ok(obj) => Ok(Some(obj)), | |
| Err(diesel::result::Error::NotFound) => Ok(None), | |
| Err(e) => Err(e) | |
| .context(format_err!( | |
| "Was not able to read {}", | |
| stringify!($struct_name) | |
| )) | |
| .context(::error::ErrorKind::DatabaseError) | |
| .map_err(|err| err.into()), | |
| } | |
| } |
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
| pub trait CRUD: Sized { | |
| type NewEntity; | |
| fn create(obj: &Self::NewEntity, connection: &PgConnection) -> Result<Id<Self>, Error>; | |
| fn create_promote(obj: Self::NewEntity, connection: &PgConnection) -> Result<Self, Error>; | |
| fn try_find(id: Id<Self>, connection: &PgConnection) -> Result<Option<Self>, Error>; | |
| fn find(id: Id<Self>, connection: &PgConnection) -> Result<Self, Error>; | |
| fn read(connection: &PgConnection) -> Result<Vec<Self>, Error>; | |
| fn update(obj: &Self, connection: &PgConnection) -> Result<usize, Error>; | |
| fn delete(id: &Id<Self>, connection: &PgConnection) -> Result<usize, Error>; | |
| } |
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
| #[derive(FromSqlRow, AsExpression, PartialEq, Eq)] | |
| #[sql_type = "Integer"] | |
| pub struct Id<T>(i32, PhantomData<T>); | |
| #[derive(Queryable, AsChangeset, Debug, Clone, PartialEq, Serialize, Deserialize, TypeName)] | |
| #[table_name=messages] | |
| pub struct Message { | |
| id: Id<Message>, | |
| timestamp: NaiveDateTime, | |
| tag: String, | |
| blob: JsonValue | |
| } | |
| #[derive(Queryable, Insertable, Debug, Clone, PartialEq, Serialize, Deserialize)] | |
| #[table_name=messages] | |
| pub struct NewMessage { | |
| timestamp: NaiveDateTime, | |
| tag: String, | |
| blob: JsonValue | |
| } |
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
| build_model!(Message; NewMessage; "messages" => { | |
| timestamp: NaiveDateTime, | |
| tag: String, | |
| blob: JsonValue | |
| }); |
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
| table! { | |
| use diesel::sql_types::{Serial, Nullable, Integer, Varchar, Timestamp}; | |
| events (id) { | |
| id -> Serial, | |
| source_id -> Nullable<Integer>, | |
| timestamp -> Timestamp, | |
| subject -> Varchar, | |
| origin -> Varchar, | |
| concerns -> Varchar, | |
| summary -> Varchar, | |
| url -> Nullable<Varchar>, | |
| } | |
| } | |
| joinable!(events -> messages (source_id)); |
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
| table! { | |
| use diesel::sql_types::{Serial, Varchar, Timestamp, Json}; | |
| messages (id) { | |
| id -> Serial, | |
| timestamp -> Timestamp, | |
| tag -> Varchar, | |
| blob -> Json, | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment