From 1559936d4b2fd2c3375426f5ef26366a8cf51b67 Mon Sep 17 00:00:00 2001 From: Paul van Tilburg Date: Sun, 9 Jun 2019 18:14:32 +0200 Subject: [PATCH] Add new (insertable) models and associations to models Also make models updateable (by deriving `AsChangeset`), debuggable (by deriving `Debug`) and identifiable (by deriving `Identifiable`). Note that the `CompanyInfo` is not updateable as a new revision should be created. Leaving this derive out ensure we cannot accidentally update it. --- src/models.rs | 10 +++---- src/models/company_info.rs | 58 +++++++++++++++++++++++++++++++++++--- src/models/customer.rs | 35 +++++++++++++++++++++-- src/models/invoice.rs | 28 ++++++++++++++++-- src/models/task.rs | 33 ++++++++++++++++++++-- src/models/time_entry.rs | 27 ++++++++++++++++-- 6 files changed, 174 insertions(+), 17 deletions(-) diff --git a/src/models.rs b/src/models.rs index 1092c7b..1397ac5 100644 --- a/src/models.rs +++ b/src/models.rs @@ -6,8 +6,8 @@ mod invoice; mod task; mod time_entry; -pub use company_info::CompanyInfo; -pub use customer::Customer; -pub use invoice::Invoice; -pub use task::Task; -pub use time_entry::TimeEntry; +pub use company_info::{CompanyInfo, NewCompanyInfo}; +pub use customer::{Customer, NewCustomer}; +pub use invoice::{Invoice, NewInvoice}; +pub use task::{NewTask, Task}; +pub use time_entry::{NewTimeEntry, TimeEntry}; diff --git a/src/models/company_info.rs b/src/models/company_info.rs index 8cf2d53..2d6da7a 100644 --- a/src/models/company_info.rs +++ b/src/models/company_info.rs @@ -1,11 +1,15 @@ use chrono::NaiveDateTime; use serde_derive::{Deserialize, Serialize}; -/// The company (information) model +use crate::schema::company_infos; + +/// The company (information) model. /// /// This model represents information about the company or sole proprietorship of the user of /// StopTime. -#[derive(Deserialize, Queryable, Serialize)] +#[derive(Associations, Debug, Identifiable, Deserialize, Queryable, Serialize)] +#[belongs_to(CompanyInfo, foreign_key = "original_id")] +#[table_name = "company_infos"] pub struct CompanyInfo { /// The unique identification number pub id: i32, @@ -27,7 +31,7 @@ pub struct CompanyInfo { pub bank_name: String, /// The cellular phone number pub cell: String, - // The chamber of commerce ID number + /// The chamber of commerce ID number pub chamber: String, /// The personal contact name pub contact_name: String, @@ -41,7 +45,7 @@ pub struct CompanyInfo { pub name: String, /// The ID of the previous company information model (if any) pub original_id: Option, - // The phone number + /// The phone number pub phone: String, /// The VAT number pub vatno: String, @@ -52,3 +56,49 @@ pub struct CompanyInfo { /// The time of last update pub updated_at: NaiveDateTime, } + +/// The new company (information) model. +/// +/// This model represents new company information that can be inserted into the database. +#[derive(Debug, Deserialize, Insertable, Serialize)] +#[table_name = "company_infos"] +pub struct NewCompanyInfo { + /// The international bank account number + pub accountiban: String, + /// The name of the bank account holder + pub accountname: String, + /// The number of the bank account + pub accountno: String, + /// The city part of the address + pub address_city: String, + /// The postal code part of the address + pub address_postal_code: String, + /// The street part of the address + pub address_street: String, + /// The bank identification code (or: SWIFT code) + pub bank_bic: String, + /// The name of the bank + pub bank_name: String, + /// The cellular phone number + pub cell: String, + /// The chamber of commerce ID number + pub chamber: String, + /// The personal contact name + pub contact_name: String, + /// The country of residence + pub country: String, + /// The two letter country code + pub country_code: String, + /// The email address + pub email: String, + /// The official company name + pub name: String, + /// The ID of the previous company information model (if any) + pub original_id: Option, + /// The phone number + pub phone: String, + /// The VAT number + pub vatno: String, + /// The website + pub website: String, +} diff --git a/src/models/customer.rs b/src/models/customer.rs index 71d0cda..1024f93 100644 --- a/src/models/customer.rs +++ b/src/models/customer.rs @@ -1,11 +1,14 @@ use chrono::NaiveDateTime; use serde_derive::{Deserialize, Serialize}; -/// The customer model +use crate::schema::customers; + +/// The customer model. /// /// This model represents a customer that has projects/tasks for which invoices need to be /// generated. -#[derive(Deserialize, Queryable, Serialize)] +#[derive(AsChangeset, Debug, Deserialize, Identifiable, Queryable, Serialize)] +#[table_name = "customers"] pub struct Customer { /// The unique identification number pub id: i32, @@ -34,3 +37,31 @@ pub struct Customer { /// The time of last update pub updated_at: NaiveDateTime, } + +/// The new customer model +/// +/// This model represents a new customer that can be inserted into the database. +#[derive(Default, Deserialize, Insertable, Serialize)] +#[table_name = "customers"] +pub struct NewCustomer { + /// The city part of the address + pub address_city: String, + /// The postal code part of the address + pub address_postal_code: String, + /// The street part of the address + pub address_street: String, + /// The email address + pub email: String, + /// The name of the financial contact person/department + pub financial_contact: String, + /// The default hourly rate (if applicable) + pub hourly_rate: Option, + /// The official (long) name + pub name: String, + /// The phone number + pub phone: String, + /// The abbreviated name + pub short_name: String, + /// Flag whether the customer requires time specificaions + pub time_specification: bool, +} diff --git a/src/models/invoice.rs b/src/models/invoice.rs index aef19bc..7915d75 100644 --- a/src/models/invoice.rs +++ b/src/models/invoice.rs @@ -1,11 +1,17 @@ use chrono::NaiveDateTime; use serde_derive::{Deserialize, Serialize}; -/// The invoice model +use crate::models::{CompanyInfo, Customer}; +use crate::schema::invoices; + +/// The invoice model. /// /// This model represents an invoice for a customer that contains billed tasks and through the /// tasks the registered time. -#[derive(Deserialize, Queryable, Serialize)] +#[derive(AsChangeset, Associations, Debug, Deserialize, Identifiable, Queryable, Serialize)] +#[belongs_to(CompanyInfo)] +#[belongs_to(Customer)] +#[table_name = "invoices"] pub struct Invoice { /// The unique identification number pub id: i32, @@ -24,3 +30,21 @@ pub struct Invoice { /// The time of last update pub updated_at: NaiveDateTime, } + +/// The new invoice model. +/// +/// This model represents an new invoice for a customer that can be inserted into the database. +#[derive(Debug, Deserialize, Insertable, Serialize)] +#[table_name = "invoices"] +pub struct NewInvoice { + /// The ID of the company info at the time of billing + pub company_info_id: i32, + /// The ID of associated customer + pub customer_id: i32, + /// Flag whether the invoice should include a time specification + pub include_specification: bool, + /// The invoice number + pub number: i32, + /// Flag whether the invoice has been paid + pub paid: bool, +} diff --git a/src/models/task.rs b/src/models/task.rs index 0f46d5c..1c1f35e 100644 --- a/src/models/task.rs +++ b/src/models/task.rs @@ -1,11 +1,17 @@ use chrono::NaiveDateTime; use serde_derive::{Deserialize, Serialize}; -/// The task model +use crate::models::{Customer, Invoice}; +use crate::schema::tasks; + +/// The task (or project) model. /// /// This model represents a task (or project) of a customer on which time can be registered. /// generated. -#[derive(Deserialize, Queryable, Serialize)] +#[derive(AsChangeset, Associations, Debug, Deserialize, Identifiable, Queryable, Serialize)] +#[belongs_to(Customer)] +#[belongs_to(Invoice)] +#[table_name = "tasks"] pub struct Task { /// The unique identification number pub id: i32, @@ -28,3 +34,26 @@ pub struct Task { /// The time of last update pub updated_at: NaiveDateTime, } + +/// The new task model. +/// +/// This model represents a new task (or project) of a customer that can be inserted into the +/// database. +#[derive(Debug, Deserialize, Insertable, Serialize)] +#[table_name = "tasks"] +pub struct NewTask { + /// The ID of the associated customer + pub customer_id: i32, + /// The fixed cost of the task (if applicable) + pub fixed_cost: Option, + /// The hourly rate of the task (if applicable) + pub hourly_rate: Option, + /// An extra comment for on the invoice + pub invoice_comment: String, + /// The associated invoice (if billed) + pub invoice_id: Option, + /// The name/description + pub name: String, + /// The VAT rate (at time of billing) + pub vat_rate: f32, +} diff --git a/src/models/time_entry.rs b/src/models/time_entry.rs index cb2eef3..a00b8bd 100644 --- a/src/models/time_entry.rs +++ b/src/models/time_entry.rs @@ -1,10 +1,15 @@ use chrono::NaiveDateTime; use serde_derive::{Deserialize, Serialize}; -/// The time entry model +use crate::models::Task; +use crate::schema::time_entries; + +/// The time entry model. /// /// This model represents an amount of time that is registered for a certain task. -#[derive(Deserialize, Queryable, Serialize)] +#[derive(AsChangeset, Associations, Debug, Deserialize, Identifiable, Queryable, Serialize)] +#[belongs_to(Task)] +#[table_name = "time_entries"] pub struct TimeEntry { /// The unique identification number pub id: i32, @@ -23,3 +28,21 @@ pub struct TimeEntry { /// The time of last update pub updated_at: NaiveDateTime, } + +/// The new time entry model. +/// +/// This model represents a new registered amount of time that can be inserted into the database. +#[derive(Debug, Deserialize, Insertable, Serialize)] +#[table_name = "time_entries"] +pub struct NewTimeEntry { + /// Flag whether to bill or not + pub bill: bool, + /// An additional comment + pub comment: String, + /// The finish/end time of the entry + pub end: NaiveDateTime, + /// The start time of the entry + pub start: NaiveDateTime, + /// The ID of the task the entry is registered for + pub task_id: i32, +}