Introduce an insert! macro to avoid some code duplication

This commit is contained in:
Bat 2018-06-18 14:57:38 +01:00
parent 94af0b9a7d
commit cd1d0d9627
13 changed files with 42 additions and 94 deletions

View File

@ -19,12 +19,6 @@ pub struct NewBlogAuthor {
} }
impl BlogAuthor { impl BlogAuthor {
pub fn insert (conn: &PgConnection, new: NewBlogAuthor) -> BlogAuthor { insert!(blog_authors, NewBlogAuthor);
diesel::insert_into(blog_authors::table)
.values(new)
.get_result(conn)
.expect("Error saving new blog author")
}
get!(blog_authors); get!(blog_authors);
} }

View File

@ -22,7 +22,7 @@ use activity_pub::{
sign, sign,
webfinger::* webfinger::*
}; };
use models::instance::Instance; use models::instance::*;
use schema::blogs; use schema::blogs;
@ -56,13 +56,7 @@ pub struct NewBlog {
} }
impl Blog { impl Blog {
pub fn insert (conn: &PgConnection, new: NewBlog) -> Blog { insert!(blogs, NewBlog);
diesel::insert_into(blogs::table)
.values(new)
.get_result(conn)
.expect("Error saving new blog")
}
get!(blogs); get!(blogs);
pub fn find_for_author(conn: &PgConnection, author_id: i32) -> Vec<Blog> { pub fn find_for_author(conn: &PgConnection, author_id: i32) -> Vec<Blog> {
@ -130,7 +124,11 @@ impl Blog {
let instance = match Instance::find_by_domain(conn, inst.clone()) { let instance = match Instance::find_by_domain(conn, inst.clone()) {
Some(instance) => instance, Some(instance) => instance,
None => { None => {
Instance::insert(conn, inst.clone(), inst.clone(), false) Instance::insert(conn, NewInstance {
public_domain: inst.clone(),
name: inst.clone(),
local: false
})
} }
}; };
Blog::insert(conn, NewBlog { Blog::insert(conn, NewBlog {

View File

@ -47,13 +47,7 @@ pub struct NewComment {
} }
impl Comment { impl Comment {
pub fn insert (conn: &PgConnection, new: NewComment) -> Comment { insert!(comments, NewComment);
diesel::insert_into(comments::table)
.values(new)
.get_result(conn)
.expect("Error saving new comment")
}
get!(comments); get!(comments);
find_by!(comments, find_by_post, post_id as i32); find_by!(comments, find_by_post, post_id as i32);

View File

@ -25,13 +25,7 @@ pub struct NewFollow {
} }
impl Follow { impl Follow {
pub fn insert(conn: &PgConnection, new: NewFollow) -> Follow { insert!(follows, NewFollow);
diesel::insert_into(follows::table)
.values(new)
.get_result(conn)
.expect("Unable to insert new follow")
}
get!(follows); get!(follows);
pub fn accept_follow<A: Signer + IntoId + Clone, B: Clone + WithInbox + Actor>( pub fn accept_follow<A: Signer + IntoId + Clone, B: Clone + WithInbox + Actor>(

View File

@ -44,19 +44,8 @@ impl Instance {
Instance::get_local(conn).unwrap().id Instance::get_local(conn).unwrap().id
} }
pub fn insert<'a>(conn: &PgConnection, pub_dom: String, name: String, local: bool) -> Instance { insert!(instances, NewInstance);
diesel::insert_into(instances::table)
.values(NewInstance {
public_domain: pub_dom,
name: name,
local: local
})
.get_result(conn)
.expect("Error saving new instance")
}
get!(instances); get!(instances);
find_by!(instances, find_by_domain, public_domain as String); find_by!(instances, find_by_domain, public_domain as String);
pub fn block(&self) { pub fn block(&self) {

View File

@ -35,12 +35,9 @@ pub struct NewLike {
} }
impl Like { impl Like {
pub fn insert(conn: &PgConnection, new: NewLike) -> Like { insert!(likes, NewLike);
diesel::insert_into(likes::table) get!(likes);
.values(new) find_by!(likes, find_by_ap_url, ap_url as String);
.get_result(conn)
.expect("Unable to insert new like")
}
pub fn update_ap_url(&self, conn: &PgConnection) { pub fn update_ap_url(&self, conn: &PgConnection) {
if self.ap_url.len() == 0 { if self.ap_url.len() == 0 {
@ -50,10 +47,6 @@ impl Like {
} }
} }
get!(likes);
find_by!(likes, find_by_ap_url, ap_url as String);
pub fn find_by_user_on_post(conn: &PgConnection, user: &User, post: &Post) -> Option<Like> { pub fn find_by_user_on_post(conn: &PgConnection, user: &User, post: &Post) -> Option<Like> {
likes::table.filter(likes::post_id.eq(post.id)) likes::table.filter(likes::post_id.eq(post.id))
.filter(likes::user_id.eq(user.id)) .filter(likes::user_id.eq(user.id))

View File

@ -24,6 +24,17 @@ macro_rules! get {
}; };
} }
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")
}
};
}
pub mod blog_authors; pub mod blog_authors;
pub mod blogs; pub mod blogs;
pub mod comments; pub mod comments;

View File

@ -26,13 +26,7 @@ pub struct NewNotification {
} }
impl Notification { impl Notification {
pub fn insert(conn: &PgConnection, new: NewNotification) -> Notification { insert!(notifications, NewNotification);
diesel::insert_into(notifications::table)
.values(new)
.get_result(conn)
.expect("Couldn't save notification")
}
get!(notifications); get!(notifications);
pub fn find_for_user(conn: &PgConnection, user: &User) -> Vec<Notification> { pub fn find_for_user(conn: &PgConnection, user: &User) -> Vec<Notification> {

View File

@ -23,12 +23,6 @@ pub struct NewPostAuthor {
} }
impl PostAuthor { impl PostAuthor {
pub fn insert(conn: &PgConnection, new: NewPostAuthor) -> PostAuthor { insert!(post_authors, NewPostAuthor);
diesel::insert_into(post_authors::table)
.values(new)
.get_result(conn)
.expect("Error saving new blog author")
}
get!(post_authors); get!(post_authors);
} }

View File

@ -50,13 +50,7 @@ pub struct NewPost {
} }
impl Post { impl Post {
pub fn insert(conn: &PgConnection, new: NewPost) -> Post { insert!(posts, NewPost);
diesel::insert_into(posts::table)
.values(new)
.get_result(conn)
.expect("Error saving new post")
}
get!(posts); get!(posts);
pub fn count_local(conn: &PgConnection) -> usize { pub fn count_local(conn: &PgConnection) -> usize {

View File

@ -24,13 +24,7 @@ pub struct NewReshare {
} }
impl Reshare { impl Reshare {
pub fn insert(conn: &PgConnection, new: NewReshare) -> Reshare { insert!(reshares, NewReshare);
diesel::insert_into(reshares::table)
.values(new)
.get_result(conn)
.expect("Couldn't save reshare")
}
get!(reshares); get!(reshares);
pub fn update_ap_url(&self, conn: &PgConnection) { pub fn update_ap_url(&self, conn: &PgConnection) {

View File

@ -38,7 +38,7 @@ use models::{
blogs::Blog, blogs::Blog,
blog_authors::BlogAuthor, blog_authors::BlogAuthor,
follows::Follow, follows::Follow,
instance::Instance, instance::*,
post_authors::PostAuthor, post_authors::PostAuthor,
posts::Post posts::Post
}; };
@ -85,6 +85,8 @@ pub struct NewUser {
} }
impl User { impl User {
insert!(users, NewUser);
pub fn grant_admin_rights(&self, conn: &PgConnection) { pub fn grant_admin_rights(&self, conn: &PgConnection) {
diesel::update(self) diesel::update(self)
.set(users::is_admin.eq(true)) .set(users::is_admin.eq(true))
@ -92,13 +94,6 @@ impl User {
.expect("Couldn't grant admin rights"); .expect("Couldn't grant admin rights");
} }
pub fn insert(conn: &PgConnection, new: NewUser) -> User {
diesel::insert_into(users::table)
.values(new)
.get_result(conn)
.expect("Error saving new user")
}
pub fn update(&self, conn: &PgConnection, name: String, email: String, summary: String) -> User { pub fn update(&self, conn: &PgConnection, name: String, email: String, summary: String) -> User {
diesel::update(self) diesel::update(self)
.set(( .set((
@ -178,7 +173,11 @@ impl User {
let instance = match Instance::find_by_domain(conn, inst.clone()) { let instance = match Instance::find_by_domain(conn, inst.clone()) {
Some(instance) => instance, Some(instance) => instance,
None => { None => {
Instance::insert(conn, inst.clone(), inst.clone(), false) Instance::insert(conn, NewInstance {
name: inst.clone(),
public_domain: inst.clone(),
local: false
})
} }
}; };
User::insert(conn, NewUser { User::insert(conn, NewUser {

View File

@ -58,11 +58,11 @@ struct NewInstanceForm {
#[post("/configure", data = "<data>")] #[post("/configure", data = "<data>")]
fn post_config(conn: DbConn, data: Form<NewInstanceForm>) -> Redirect { fn post_config(conn: DbConn, data: Form<NewInstanceForm>) -> Redirect {
let form = data.get(); let form = data.get();
let inst = Instance::insert( let inst = Instance::insert(&*conn, NewInstance {
&*conn, public_domain: BASE_URL.as_str().to_string(),
BASE_URL.as_str().to_string(), name: form.name.to_string(),
form.name.to_string(), local: true
true); });
if inst.has_admin(&*conn) { if inst.has_admin(&*conn) {
Redirect::to("/") Redirect::to("/")
} else { } else {