use chrono::datetime::DateTime;
use chrono::offset::utc::UTC;
use gen::column;
use gen::schema;
use gen::table;
use rustc_serialize::json::Json;
use rustc_serialize::json::ToJson;
use rustorm::dao::Dao;
use rustorm::dao::IsDao;
use rustorm::dao::Type;
use rustorm::dao::Value;
use rustorm::query::Operand;
use rustorm::table::Column;
use rustorm::table::Foreign;
use rustorm::table::IsTable;
use rustorm::table::Table;
use uuid::Uuid;
use gen::bazaar::Users;
#[derive(RustcEncodable)]
#[derive(Debug, Clone)]
pub struct ApiKey {
pub api_key_id: Uuid,
pub api_key: String,
pub user_id: Uuid,
pub valid_starting: Option<DateTime<UTC>>,
pub valid_until: Option<DateTime<UTC>>,
pub active: bool,
pub client_id: Option<Uuid>,
pub created: DateTime<UTC>,
pub created_by: Option<Uuid>,
pub description: Option<String>,
pub help: Option<String>,
pub name: Option<String>,
pub organization_id: Option<Uuid>,
pub priority: Option<f64>,
pub updated: DateTime<UTC>,
pub updated_by: Option<Uuid>,
pub user: Option<Users>,
}
impl IsDao for ApiKey {
fn from_dao(dao: &Dao) -> Self {
ApiKey {
organization_id: dao.get_opt(column::organization_id),
client_id: dao.get_opt(column::client_id),
created: dao.get(column::created),
created_by: dao.get_opt(column::created_by),
updated: dao.get(column::updated),
updated_by: dao.get_opt(column::updated_by),
priority: dao.get_opt(column::priority),
name: dao.get_opt(column::name),
description: dao.get_opt(column::description),
help: dao.get_opt(column::help),
active: dao.get(column::active),
api_key_id: dao.get(column::api_key_id),
api_key: dao.get(column::api_key),
user_id: dao.get(column::user_id),
valid_starting: dao.get_opt(column::valid_starting),
valid_until: dao.get_opt(column::valid_until),
user: None,
}
}
fn to_dao(&self) -> Dao {
let mut dao = Dao::new();
match self.organization_id {
Some(ref _value) => dao.set(column::organization_id, _value),
None => dao.set_null(column::organization_id)
}
match self.client_id {
Some(ref _value) => dao.set(column::client_id, _value),
None => dao.set_null(column::client_id)
}
dao.set(column::created, &self.created);
match self.created_by {
Some(ref _value) => dao.set(column::created_by, _value),
None => dao.set_null(column::created_by)
}
dao.set(column::updated, &self.updated);
match self.updated_by {
Some(ref _value) => dao.set(column::updated_by, _value),
None => dao.set_null(column::updated_by)
}
match self.priority {
Some(ref _value) => dao.set(column::priority, _value),
None => dao.set_null(column::priority)
}
match self.name {
Some(ref _value) => dao.set(column::name, _value),
None => dao.set_null(column::name)
}
match self.description {
Some(ref _value) => dao.set(column::description, _value),
None => dao.set_null(column::description)
}
match self.help {
Some(ref _value) => dao.set(column::help, _value),
None => dao.set_null(column::help)
}
dao.set(column::active, &self.active);
dao.set(column::api_key_id, &self.api_key_id);
dao.set(column::api_key, &self.api_key);
dao.set(column::user_id, &self.user_id);
match self.valid_starting {
Some(ref _value) => dao.set(column::valid_starting, _value),
None => dao.set_null(column::valid_starting)
}
match self.valid_until {
Some(ref _value) => dao.set(column::valid_until, _value),
None => dao.set_null(column::valid_until)
}
dao
}
}
impl ToJson for ApiKey {
fn to_json(&self) -> Json {
self.to_dao().to_json()
}
}
impl Default for ApiKey {
fn default() -> Self {
ApiKey{
organization_id: Default::default(),
client_id: Default::default(),
created: UTC::now(),
created_by: Default::default(),
updated: UTC::now(),
updated_by: Default::default(),
priority: Default::default(),
name: Default::default(),
description: Default::default(),
help: Default::default(),
active: Default::default(),
api_key_id: Default::default(),
api_key: Default::default(),
user_id: Default::default(),
valid_starting: Default::default(),
valid_until: Default::default(),
user: Default::default(),
}
}
}
impl IsTable for ApiKey {
fn table() -> Table {
Table {
schema: Some(schema::bazaar.to_owned()),
name: table::api_key.to_owned(),
parent_table: Some(table::record.to_owned()),
sub_table: vec![],
comment: None,
columns: vec![
Column {
name: column::organization_id.to_owned(),
data_type: Type::Uuid,
db_data_type: "uuid".to_owned(),
is_primary: false, is_unique: false, not_null: false, is_inherited: true,
default: None,
comment: None,
foreign: None,
},
Column {
name: column::client_id.to_owned(),
data_type: Type::Uuid,
db_data_type: "uuid".to_owned(),
is_primary: false, is_unique: false, not_null: false, is_inherited: true,
default: None,
comment: None,
foreign: None,
},
Column {
name: column::created.to_owned(),
data_type: Type::DateTime,
db_data_type: "timestamp with time zone".to_owned(),
is_primary: false, is_unique: false, not_null: true, is_inherited: true,
default: Some(Operand::Value(Value::String("'now()'".to_owned()))),
comment: None,
foreign: None,
},
Column {
name: column::created_by.to_owned(),
data_type: Type::Uuid,
db_data_type: "uuid".to_owned(),
is_primary: false, is_unique: false, not_null: false, is_inherited: true,
default: None,
comment: None,
foreign: None,
},
Column {
name: column::updated.to_owned(),
data_type: Type::DateTime,
db_data_type: "timestamp with time zone".to_owned(),
is_primary: false, is_unique: false, not_null: true, is_inherited: true,
default: Some(Operand::Value(Value::String("'now()'".to_owned()))),
comment: None,
foreign: None,
},
Column {
name: column::updated_by.to_owned(),
data_type: Type::Uuid,
db_data_type: "uuid".to_owned(),
is_primary: false, is_unique: false, not_null: false, is_inherited: true,
default: None,
comment: None,
foreign: None,
},
Column {
name: column::priority.to_owned(),
data_type: Type::F64,
db_data_type: "double precision".to_owned(),
is_primary: false, is_unique: false, not_null: false, is_inherited: true,
default: None,
comment: None,
foreign: None,
},
Column {
name: column::name.to_owned(),
data_type: Type::String,
db_data_type: "character varying".to_owned(),
is_primary: false, is_unique: false, not_null: false, is_inherited: true,
default: None,
comment: None,
foreign: None,
},
Column {
name: column::description.to_owned(),
data_type: Type::String,
db_data_type: "character varying".to_owned(),
is_primary: false, is_unique: false, not_null: false, is_inherited: true,
default: None,
comment: None,
foreign: None,
},
Column {
name: column::help.to_owned(),
data_type: Type::String,
db_data_type: "text".to_owned(),
is_primary: false, is_unique: false, not_null: false, is_inherited: true,
default: None,
comment: None,
foreign: None,
},
Column {
name: column::active.to_owned(),
data_type: Type::Bool,
db_data_type: "boolean".to_owned(),
is_primary: false, is_unique: false, not_null: true, is_inherited: true,
default: Some(Operand::Value(Value::String("'true'".to_owned()))),
comment: None,
foreign: None,
},
Column {
name: column::api_key_id.to_owned(),
data_type: Type::Uuid,
db_data_type: "uuid".to_owned(),
is_primary: true, is_unique: false, not_null: true, is_inherited: false,
default: Some(Operand::Value(Value::String("'uuid_generate_v4()'".to_owned()))),
comment: None,
foreign: None,
},
Column {
name: column::api_key.to_owned(),
data_type: Type::String,
db_data_type: "character varying".to_owned(),
is_primary: false, is_unique: false, not_null: true, is_inherited: false,
default: None,
comment: None,
foreign: None,
},
Column {
name: column::user_id.to_owned(),
data_type: Type::Uuid,
db_data_type: "uuid".to_owned(),
is_primary: false, is_unique: false, not_null: true, is_inherited: false,
default: None,
comment: None,
foreign: Some(
Foreign {
schema: Some("bazaar".to_owned()),
table: "users".to_owned(),
column: "user_id".to_owned(),
}),
},
Column {
name: column::valid_starting.to_owned(),
data_type: Type::DateTime,
db_data_type: "timestamp with time zone".to_owned(),
is_primary: false, is_unique: false, not_null: false, is_inherited: false,
default: None,
comment: None,
foreign: None,
},
Column {
name: column::valid_until.to_owned(),
data_type: Type::DateTime,
db_data_type: "timestamp with time zone".to_owned(),
is_primary: false, is_unique: false, not_null: false, is_inherited: false,
default: None,
comment: None,
foreign: None,
},
],
is_view: false,
}
}
}
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub static organization_id: &'static str = "api_key.organization_id";
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub static client_id: &'static str = "api_key.client_id";
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub static created: &'static str = "api_key.created";
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub static created_by: &'static str = "api_key.created_by";
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub static updated: &'static str = "api_key.updated";
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub static updated_by: &'static str = "api_key.updated_by";
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub static priority: &'static str = "api_key.priority";
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub static name: &'static str = "api_key.name";
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub static description: &'static str = "api_key.description";
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub static help: &'static str = "api_key.help";
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub static active: &'static str = "api_key.active";
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub static api_key_id: &'static str = "api_key.api_key_id";
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub static api_key: &'static str = "api_key.api_key";
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub static user_id: &'static str = "api_key.user_id";
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub static valid_starting: &'static str = "api_key.valid_starting";
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub static valid_until: &'static str = "api_key.valid_until";