stoptime-rs/src/models/task.rs

86 lines
2.7 KiB
Rust

use chrono::NaiveDateTime;
use diesel::prelude::*;
use juniper_codegen::{GraphQLInputObject, GraphQLObject};
use crate::models::{Customer, Invoice, TimeEntry};
use crate::schema::tasks;
use crate::DbConn;
/// The task (or project) model.
///
/// This model represents a task (or project) of a customer on which time can be registered.
/// generated.
#[derive(AsChangeset, Associations, Debug, GraphQLObject, Identifiable, Queryable)]
#[belongs_to(Customer)]
#[belongs_to(Invoice)]
#[table_name = "tasks"]
pub struct Task {
/// The unique identification number
pub id: i32,
/// The ID of the associated customer
pub customer_id: i32,
/// The fixed cost of the task (if applicable)
pub fixed_cost: Option<f64>,
/// The hourly rate of the task (if applicable)
pub hourly_rate: Option<f64>,
/// An extra comment for on the invoice
pub invoice_comment: String,
/// The associated invoice (if billed)
pub invoice_id: Option<i32>,
/// The name/description
pub name: String,
/// The VAT rate (at time of billing)
pub vat_rate: f64,
/// The time of creation
pub created_at: NaiveDateTime,
/// The time of last update
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.
///
/// This model represents a new task (or project) of a customer that can be inserted into the
/// database.
#[derive(Debug, GraphQLInputObject, Insertable)]
#[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<f64>,
/// The hourly rate of the task (if applicable)
pub hourly_rate: Option<f64>,
/// An extra comment for on the invoice
pub invoice_comment: String,
/// The associated invoice (if billed)
pub invoice_id: Option<i32>,
/// The name/description
pub name: String,
/// The VAT rate (at time of billing)
pub vat_rate: f64,
}