Add relation resolving methods to all models

This commit is contained in:
Paul van Tilburg 2019-07-09 12:23:31 +02:00
parent 368a3ff10e
commit f8bff689e6
5 changed files with 109 additions and 2 deletions

View File

@ -1,7 +1,9 @@
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use diesel::prelude::*;
use crate::schema::company_infos; use crate::schema::company_infos;
use crate::DbConn;
/// The company (information) model. /// The company (information) model.
/// ///
@ -57,6 +59,21 @@ pub struct CompanyInfo {
pub updated_at: NaiveDateTime, pub updated_at: NaiveDateTime,
} }
impl CompanyInfo {
/// Returns the original/previous company information model (if any).
pub fn original(&self, conn: &DbConn) -> QueryResult<Option<CompanyInfo>> {
use diesel::associations::HasTable;
match self.original_id {
Some(original_id) => CompanyInfo::table()
.find(original_id)
.first(&**conn)
.optional(),
None => Ok(None),
}
}
}
/// The new company (information) model. /// The new company (information) model.
/// ///
/// This model represents new company information that can be inserted into the database. /// This model represents new company information that can be inserted into the database.

View File

@ -1,7 +1,10 @@
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use diesel::prelude::*;
use crate::models::{Invoice, Task};
use crate::schema::customers; use crate::schema::customers;
use crate::DbConn;
/// The customer model. /// The customer model.
/// ///
@ -38,6 +41,31 @@ pub struct Customer {
pub updated_at: NaiveDateTime, pub updated_at: NaiveDateTime,
} }
impl Customer {
/// Returns the invoices billed to the customer.
pub fn invoices(&self, conn: &DbConn) -> QueryResult<Vec<Invoice>> {
Invoice::belonging_to(self).load(&**conn)
}
/// Returns the project/tasks associated with the customer.
pub fn tasks(&self, conn: &DbConn) -> QueryResult<Vec<Task>> {
Task::belonging_to(self).load(&**conn)
}
}
#[object(Context = DbConn)]
impl Customer {
/// Returns the invoices billed to the customer.
fn invoices(&self, context: &DbConn) -> FieldResult<Vec<Invoice>> {
self.invoices(context).map_err(Into::into)
}
/// Returns the project/tasks associated with the customer.
fn tasks(&self, context: &DbConn) -> FieldResult<Vec<Task>> {
self.tasks(context).map_err(Into::into)
}
}
/// The new customer model /// The new customer model
/// ///
/// This model represents a new customer that can be inserted into the database. /// This model represents a new customer that can be inserted into the database.

View File

@ -1,8 +1,10 @@
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use diesel::prelude::*;
use crate::models::{CompanyInfo, Customer}; use crate::models::{CompanyInfo, Customer, Task};
use crate::schema::invoices; use crate::schema::invoices;
use crate::DbConn;
/// The invoice model. /// The invoice model.
/// ///
@ -31,6 +33,29 @@ pub struct Invoice {
pub updated_at: NaiveDateTime, pub updated_at: NaiveDateTime,
} }
impl Invoice {
/// Returns the associated company info at the time of billing.
pub fn company_info(&self, conn: &DbConn) -> QueryResult<CompanyInfo> {
use diesel::associations::HasTable;
CompanyInfo::table()
.find(self.company_info_id)
.first(&**conn)
}
/// Returns the associated customer.
pub fn customer(&self, conn: &DbConn) -> QueryResult<Customer> {
use diesel::associations::HasTable;
Customer::table().find(self.customer_id).first(&**conn)
}
/// Returns the billed tasks included in the invoice.
pub fn tasks(&self, conn: &DbConn) -> QueryResult<Vec<Task>> {
Task::belonging_to(self).load(&**conn)
}
}
/// The new invoice model. /// The new invoice model.
/// ///
/// This model represents an new invoice for a customer that can be inserted into the database. /// This model represents an new invoice for a customer that can be inserted into the database.

View File

@ -1,8 +1,10 @@
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use diesel::prelude::*;
use crate::models::{Customer, Invoice}; use crate::models::{Customer, Invoice, TimeEntry};
use crate::schema::tasks; use crate::schema::tasks;
use crate::DbConn;
/// The task (or project) model. /// The task (or project) model.
/// ///
@ -35,6 +37,30 @@ pub struct Task {
pub updated_at: NaiveDateTime, pub updated_at: NaiveDateTime,
} }
impl Task {
/// Returns the associated customer.
pub fn customer(&self, conn: &DbConn) -> QueryResult<Customer> {
use diesel::associations::HasTable;
Customer::table().find(self.customer_id).first(&**conn)
}
/// Returns the associated invoice (if billed)
pub fn invoice(&self, conn: &DbConn) -> QueryResult<Option<Invoice>> {
use diesel::associations::HasTable;
match self.invoice_id {
Some(invoice_id) => Invoice::table().find(invoice_id).first(&**conn).optional(),
None => Ok(None),
}
}
/// Returns the registered time entries.
pub fn time_entries(&self, conn: &DbConn) -> QueryResult<Vec<TimeEntry>> {
TimeEntry::belonging_to(self).load(&**conn)
}
}
/// The new task model. /// The new task model.
/// ///
/// This model represents a new task (or project) of a customer that can be inserted into the /// This model represents a new task (or project) of a customer that can be inserted into the

View File

@ -1,8 +1,10 @@
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use diesel::prelude::*;
use crate::models::Task; use crate::models::Task;
use crate::schema::time_entries; use crate::schema::time_entries;
use crate::DbConn;
/// The time entry model. /// The time entry model.
/// ///
@ -29,6 +31,15 @@ pub struct TimeEntry {
pub updated_at: NaiveDateTime, pub updated_at: NaiveDateTime,
} }
impl TimeEntry {
/// Returns the task the entry is registered for.
pub fn customer(&self, conn: &DbConn) -> QueryResult<Task> {
use diesel::associations::HasTable;
Task::table().find(self.task_id).first(&**conn)
}
}
/// The new time entry model. /// The new time entry model.
/// ///
/// This model represents a new registered amount of time that can be inserted into the database. /// This model represents a new registered amount of time that can be inserted into the database.