Introduce an insert! macro to avoid some code duplication
This commit is contained in:
+8
-9
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user