database models

* introduce rust sources per db model

Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
2019-11-18 21:43:23 +01:00
parent eb11f385e1
commit ccc41383fe
8 changed files with 301 additions and 0 deletions

16
src/models.rs Normal file
View File

@@ -0,0 +1,16 @@
/*
* advotracker - Hotline tackingtool for Advocats
*
* Copyright 2019 Ralf Zerres <ralf.zerres@networkx.de>
* SPDX-License-Identifier: (0BSD or MIT)
*/
// bring models into scope
pub mod roles;
pub mod harms;
pub mod users;
pub mod user_roles;
// methods used for: INSERT
//pub mod insert;
//use insert::types::*;

26
src/models/claims.rs Normal file
View File

@@ -0,0 +1,26 @@
/*
* advotracker - Hotline tackingtool for Advocats
*
* Copyright 2019 Ralf Zerres <ralf.zerres@networkx.de>
* SPDX-License-Identifier: (0BSD or MIT)
*/
use crait::schema::claim.rs;
//#[derive(Debug, Deserialize, PartialEq, Queryable)]
#[derive(Debug, Queryable)]
pub struct Claim {
// id: the auto incremented primary index
pub id: i32,
pub user_id: i32,
pub type_: Option<String>,
pub value: Option<String>,
}
#[derive(Debug, Deserialize, Insertable)]
#[table_name = "claims"]
pub struct NewClaim<'a> {
//pub user_id: &'a i32,
pub type_: Option<&'a str>,
pub value: Option<&'a str>,
}

43
src/models/harms.rs Normal file
View File

@@ -0,0 +1,43 @@
/*
* advotracker - Hotline tackingtool for Advocats
*
* Copyright 2019 Ralf Zerres <ralf.zerres@networkx.de>
* SPDX-License-Identifier: (0BSD or MIT)
*/
use chrono::NaiveDateTime;
// in diesel, we need serde_derive to realize the needed traits to de- or serialize
use serde_derive::*;
use crate::schema::harms;
//usage: SELECT, UPDATE, DELETE
#[derive(AsChangeset, Debug, Deserialize, Identifiable, PartialEq, Queryable)]
pub struct Harm {
// id: the auto incremented primary index
pub id: i32,
pub id_harm: String,
pub id_policyholder: Option<String>,
pub id_callback: Option<String>,
pub date_fallback: Option<NaiveDateTime>,
pub date_recording: NaiveDateTime,
pub id_user: i32,
pub date_created: NaiveDateTime,
pub id_user_changed: i32,
pub date_changed: NaiveDateTime,
}
// usage: INSERT
#[derive(Debug, Deserialize, Insertable, Serialize)]
#[table_name = "harms"]
pub struct NewHarm<'a> {
pub id_harm: &'a str,
pub id_policyholder: Option<&'a str>,
pub id_callback: Option<&'a str>,
pub date_fallback: Option<NaiveDateTime>,
pub date_recording: NaiveDateTime,
pub id_user: i32,
pub date_created: Option<NaiveDateTime>,
pub id_user_changed: i32,
pub date_changed: Option<NaiveDateTime>,
}

15
src/models/insert.rs Normal file
View File

@@ -0,0 +1,15 @@
/*
* advotracker - Hotline tackingtool for Advocats
*
* Copyright 2019 Ralf Zerres <ralf.zerres@networkx.de>
* SPDX-License-Identifier: (0BSD or MIT)
*/
use crate::schema::*;
//use chrono::NaiveDateTime;
use serde_derive::*;
//use types::*;
//use crate::models::types::*;
//use serde::*;
pub mod types;

38
src/models/roles.rs Normal file
View File

@@ -0,0 +1,38 @@
/*
* advotracker - Hotline tackingtool for Advocats
*
* Copyright 2019 Ralf Zerres <ralf.zerres@networkx.de>
* SPDX-License-Identifier: (0BSD or MIT)
*/
use chrono::NaiveDateTime;
use serde_derive::*;
//use serde;
use crate::schema::roles;
#[derive(AsChangeset, Debug, Deserialize, Identifiable, PartialEq, Queryable)]
#[table_name = "roles"]
pub struct Role {
// id: the auto incremented primary index
pub id: i32,
pub name: String,
pub date_created: NaiveDateTime,
pub id_user_changed: i32,
pub date_changed: NaiveDateTime,
}
#[derive(Debug, Deserialize, Insertable, Serialize)]
#[table_name = "roles"]
pub struct NewRole<'a> {
pub name: &'a str,
pub date_created: Option<NaiveDateTime>,
pub id_user_changed: i32,
pub date_changed: Option<NaiveDateTime>,
}
// we couldn't do a RoleList implementation for a Vector
// because we don't own it, RoleList is using the newtype pattern
//#[derive(Debug, Deserialize, Serialize)]
//pub struct RoleList(pub Vec<Role>);

23
src/models/user_roles.rs Normal file
View File

@@ -0,0 +1,23 @@
/*
* advotracker - Hotline tackingtool for Advocats
*
* Copyright 2019 Ralf Zerres <ralf.zerres@networkx.de>
* SPDX-License-Identifier: (0BSD or MIT)
*/
use crate::schema::user_roles;
use serde_derive::*;
#[derive(Debug, Queryable, PartialEq)]
pub struct UserRole {
pub id_user: i32,
pub id_role: i32,
}
//#[derive(Deserialize)]
#[derive(Deserialize, Insertable)]
#[table_name = "user_roles"]
pub struct NewUserRole {
pub id_user: i32,
pub id_role: i32,
}

68
src/models/users.rs Normal file
View File

@@ -0,0 +1,68 @@
/*
* advotracker - Hotline tackingtool for Advocats
*
* Copyright 2019 Ralf Zerres <ralf.zerres@networkx.de>
* SPDX-License-Identifier: (0BSD or MIT)
*/
use chrono::NaiveDateTime;
// in diesel, we need serde_derive to realize the needed traits to de- or serialize
use serde_derive::*;
use crate::schema::users;
// usage: SELECT, UPDATE, DELETE
#[derive(AsChangeset, Debug, Deserialize, Identifiable, PartialEq, Queryable, Serialize)]
pub struct User {
// id: the auto incremented primary index
pub id: i32,
pub last_name: String,
// Option -> returns "None", if db value is null
pub first_name: Option<String>,
pub alias_name: Option<String>,
pub email: Option<String>,
pub email_confirmed: Option<i32>,
pub initials: Option<String>,
pub password_hash: Option<String>,
pub date_created: NaiveDateTime,
pub id_changed: i32,
pub date_changed: NaiveDateTime,
}
// usage: INSERT
#[derive(Debug, Deserialize, Insertable, Serialize)]
#[table_name = "users"]
pub struct NewUser<'a> {
pub last_name: &'a str,
pub first_name: Option<&'a str>,
pub alias_name: Option<&'a str>,
pub email: Option<&'a str>,
pub email_confirmed: Option<i32>,
pub initials: Option<&'a str>,
pub password_hash: &'a str,
pub date_created: Option<NaiveDateTime>,
pub id_changed: i32,
pub date_changed: Option<NaiveDateTime>,
}
#[derive(Debug, Deserialize, Queryable)]
pub struct RegisterUser<'a> {
pub last_name: &'a str,
pub first_name: Option<&'a str>,
pub alias: Option<&'a str>,
pub email: Option<&'a str>,
pub email_confirmed: Option<i32>,
pub initials: Option<&'a str>,
pub password_hash: &'a str,
pub password_confirmation: &'a str,
pub id_changed: i32,
}
// WIP
//pub mod traits;
//use types::ConfirmState;
//use crate::types::ConfirmStateVisitor;
//#[serde(skip)]
//#[derive(Serialize, Deserialize)]
//pub struct UserList(pub Vec<User>);

72
src/schema.rs Normal file
View File

@@ -0,0 +1,72 @@
table! {
claims (id) {
id -> Integer,
id_user -> Integer,
#[sql_name = "type"]
type_ -> Nullable<Text>,
value -> Nullable<Text>,
date_created -> Timestamp,
id_user_changed -> Integer,
date_changed -> Timestamp,
}
}
table! {
harms (id) {
id -> Integer,
id_harm -> Text,
id_policyholder -> Nullable<Text>,
id_callback -> Nullable<Text>,
date_fallback -> Nullable<Timestamp>,
date_recording -> Timestamp,
id_user -> Integer,
date_created -> Timestamp,
id_user_changed -> Integer,
date_changed -> Timestamp,
}
}
table! {
roles (id) {
id -> Integer,
name -> Text,
date_created -> Timestamp,
id_user_changed -> Integer,
date_changed -> Timestamp,
}
}
table! {
user_roles (id_user, id_role) {
id_user -> Integer,
id_role -> Integer,
}
}
table! {
users (id) {
id -> Integer,
last_name -> Text,
first_name -> Nullable<Text>,
alias_name -> Nullable<Text>,
email -> Nullable<Text>,
email_confirmed -> Nullable<Integer>,
initials -> Nullable<Text>,
password_hash -> Nullable<Text>,
date_created -> Timestamp,
id_changed -> Integer,
date_changed -> Timestamp,
}
}
joinable!(claims -> users (id_user));
joinable!(user_roles -> roles (id_role));
joinable!(user_roles -> users (id_user));
allow_tables_to_appear_in_same_query!(
claims,
harms,
roles,
user_roles,
users,
);