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
+8 -9
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 {