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 {
pub fn insert (conn: &PgConnection, new: NewBlogAuthor) -> BlogAuthor {
diesel::insert_into(blog_authors::table)
.values(new)
.get_result(conn)
.expect("Error saving new blog author")
}
insert!(blog_authors, NewBlogAuthor);
get!(blog_authors);
}

View file

@ -22,7 +22,7 @@ use activity_pub::{
sign,
webfinger::*
};
use models::instance::Instance;
use models::instance::*;
use schema::blogs;
@ -56,13 +56,7 @@ pub struct NewBlog {
}
impl Blog {
pub fn insert (conn: &PgConnection, new: NewBlog) -> Blog {
diesel::insert_into(blogs::table)
.values(new)
.get_result(conn)
.expect("Error saving new blog")
}
insert!(blogs, NewBlog);
get!(blogs);
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()) {
Some(instance) => instance,
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 {

View file

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

View file

@ -25,13 +25,7 @@ pub struct NewFollow {
}
impl Follow {
pub fn insert(conn: &PgConnection, new: NewFollow) -> Follow {
diesel::insert_into(follows::table)
.values(new)
.get_result(conn)
.expect("Unable to insert new follow")
}
insert!(follows, NewFollow);
get!(follows);
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
}
pub fn insert<'a>(conn: &PgConnection, pub_dom: String, name: String, local: bool) -> Instance {
diesel::insert_into(instances::table)
.values(NewInstance {
public_domain: pub_dom,
name: name,
local: local
})
.get_result(conn)
.expect("Error saving new instance")
}
insert!(instances, NewInstance);
get!(instances);
find_by!(instances, find_by_domain, public_domain as String);
pub fn block(&self) {

View file

@ -35,12 +35,9 @@ pub struct NewLike {
}
impl Like {
pub fn insert(conn: &PgConnection, new: NewLike) -> Like {
diesel::insert_into(likes::table)
.values(new)
.get_result(conn)
.expect("Unable to insert new like")
}
insert!(likes, NewLike);
get!(likes);
find_by!(likes, find_by_ap_url, ap_url as String);
pub fn update_ap_url(&self, conn: &PgConnection) {
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> {
likes::table.filter(likes::post_id.eq(post.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 blogs;
pub mod comments;

View file

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

View file

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

View file

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

View file

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

View file

@ -38,7 +38,7 @@ use models::{
blogs::Blog,
blog_authors::BlogAuthor,
follows::Follow,
instance::Instance,
instance::*,
post_authors::PostAuthor,
posts::Post
};
@ -85,6 +85,8 @@ pub struct NewUser {
}
impl User {
insert!(users, NewUser);
pub fn grant_admin_rights(&self, conn: &PgConnection) {
diesel::update(self)
.set(users::is_admin.eq(true))
@ -92,13 +94,6 @@ impl User {
.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 {
diesel::update(self)
.set((
@ -178,7 +173,11 @@ impl User {
let instance = match Instance::find_by_domain(conn, inst.clone()) {
Some(instance) => instance,
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 {

View file

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