2018-06-18 13:37:49 +00:00
|
|
|
macro_rules! find_by {
|
2018-06-18 15:13:09 +00:00
|
|
|
($table:ident, $fn:ident, $($col:ident as $type:ident),+) => {
|
2018-06-18 13:37:49 +00:00
|
|
|
/// Try to find a $table with a given $col
|
2018-06-18 15:13:09 +00:00
|
|
|
pub fn $fn(conn: &PgConnection, $($col: $type),+) -> Option<Self> {
|
|
|
|
$table::table
|
|
|
|
$(.filter($table::$col.eq($col)))+
|
2018-06-18 13:37:49 +00:00
|
|
|
.limit(1)
|
|
|
|
.load::<Self>(conn)
|
|
|
|
.expect("Error loading $table by $col")
|
|
|
|
.into_iter().nth(0)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-18 13:44:23 +00:00
|
|
|
macro_rules! get {
|
|
|
|
($table:ident) => {
|
|
|
|
pub fn get(conn: &PgConnection, id: i32) -> Option<Self> {
|
|
|
|
$table::table.filter($table::id.eq(id))
|
|
|
|
.limit(1)
|
|
|
|
.load::<Self>(conn)
|
|
|
|
.expect("Error loading $table by id")
|
|
|
|
.into_iter().nth(0)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-18 13:57:38 +00:00
|
|
|
macro_rules! insert {
|
|
|
|
($table:ident, $from:ident) => {
|
|
|
|
pub fn insert(conn: &PgConnection, new: $from) -> Self {
|
|
|
|
diesel::insert_into($table::table)
|
|
|
|
.values(new)
|
|
|
|
.get_result(conn)
|
|
|
|
.expect("Error saving new $table")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-23 11:27:27 +00:00
|
|
|
pub mod blog_authors;
|
2018-04-23 10:29:27 +00:00
|
|
|
pub mod blogs;
|
2018-05-09 20:35:02 +00:00
|
|
|
pub mod comments;
|
2018-05-01 13:06:31 +00:00
|
|
|
pub mod follows;
|
2018-04-22 13:35:37 +00:00
|
|
|
pub mod instance;
|
2018-05-10 15:54:35 +00:00
|
|
|
pub mod likes;
|
2018-05-13 12:44:18 +00:00
|
|
|
pub mod notifications;
|
2018-04-23 14:37:49 +00:00
|
|
|
pub mod post_authors;
|
2018-04-23 15:19:28 +00:00
|
|
|
pub mod posts;
|
2018-05-19 09:23:02 +00:00
|
|
|
pub mod reshares;
|
2018-04-23 15:19:28 +00:00
|
|
|
pub mod users;
|