Plume/src/models/mod.rs

49 lines
1.3 KiB
Rust
Raw Normal View History

// TODO: support multiple columns (see Like::find_by_user_on_post)
macro_rules! find_by {
($table:ident, $fn:ident, $col:ident as $type:ident) => {
/// Try to find a $table with a given $col
pub fn $fn(conn: &PgConnection, val: $type) -> Option<Self> {
$table::table.filter($table::$col.eq(val))
.limit(1)
.load::<Self>(conn)
.expect("Error loading $table by $col")
.into_iter().nth(0)
}
};
}
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)
}
};
}
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 13:27:27 +02:00
pub mod blog_authors;
2018-04-23 12:29:27 +02:00
pub mod blogs;
2018-05-09 22:35:02 +02:00
pub mod comments;
2018-05-01 15:06:31 +02:00
pub mod follows;
pub mod instance;
2018-05-10 17:54:35 +02:00
pub mod likes;
2018-05-13 14:44:18 +02:00
pub mod notifications;
2018-04-23 16:37:49 +02:00
pub mod post_authors;
2018-04-23 17:19:28 +02:00
pub mod posts;
2018-05-19 11:23:02 +02:00
pub mod reshares;
2018-04-23 17:19:28 +02:00
pub mod users;