Plume/src/models/instance.rs

67 lines
1.9 KiB
Rust
Raw Normal View History

2018-04-30 19:46:27 +02:00
use chrono::NaiveDateTime;
2018-04-24 11:21:39 +02:00
use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection};
use std::iter::Iterator;
2018-04-24 11:21:39 +02:00
2018-04-23 17:19:28 +02:00
use models::users::User;
2018-04-24 11:21:39 +02:00
use schema::{instances, users};
#[derive(Identifiable, Queryable)]
pub struct Instance {
pub id: i32,
pub local_domain: String,
pub public_domain: String,
pub name: String,
pub local: bool,
2018-04-30 19:46:27 +02:00
pub blocked: bool,
pub creation_date: NaiveDateTime
}
#[derive(Insertable)]
#[table_name = "instances"]
pub struct NewInstance {
pub local_domain: String,
pub public_domain: String,
pub name: String,
pub local: bool
}
impl Instance {
pub fn get_local(conn: &PgConnection) -> Option<Instance> {
instances::table.filter(instances::local.eq(true))
.limit(1)
.load::<Instance>(conn)
.expect("Error loading local instance infos")
.into_iter().nth(0)
}
pub fn insert<'a>(conn: &PgConnection, loc_dom: String, pub_dom: String, name: String, local: bool) -> Instance {
diesel::insert_into(instances::table)
.values(NewInstance {
local_domain: loc_dom,
public_domain: pub_dom,
name: name,
local: local
})
.get_result(conn)
.expect("Error saving new instance")
}
pub fn get(conn: &PgConnection, id: i32) -> Option<Instance> {
instances::table.filter(instances::id.eq(id))
.limit(1)
.load::<Instance>(conn)
.expect("Error loading local instance infos")
.into_iter().nth(0)
}
pub fn block(&self) {}
pub fn has_admin(&self, conn: &PgConnection) -> bool {
users::table.filter(users::instance_id.eq(self.id))
.filter(users::is_admin.eq(true))
.load::<User>(conn)
.expect("Couldn't load admins")
.len() > 0
}
}