feat: ability to view tasks in different categories
This commit is contained in:
@ -1,10 +1,12 @@
|
||||
use crate::schema::tasks;
|
||||
use chrono::{Duration, NaiveDate, NaiveTime};
|
||||
use diesel::deserialize::FromSql;
|
||||
use diesel::pg::{Pg, PgValue};
|
||||
use diesel::serialize::{Output, ToSql};
|
||||
use diesel::sql_types::Jsonb;
|
||||
use diesel::{AsExpression, FromSqlRow};
|
||||
use diesel::sql_types::{Bool, Jsonb};
|
||||
use diesel::{AsExpression, BoxableExpression, FromSqlRow, PgJsonbExpressionMethods};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use serde_with::DurationSeconds;
|
||||
use std::io::Write;
|
||||
|
||||
@ -18,8 +20,7 @@ pub enum Category {
|
||||
NextSteps,
|
||||
Calendar {
|
||||
date: NaiveDate,
|
||||
#[serde_as(as = "Option<DurationSeconds<i64>>")]
|
||||
reoccurance_interval: Option<Duration>,
|
||||
reoccurrence: Option<Reoccurrence>,
|
||||
time: Option<CalendarTime>,
|
||||
},
|
||||
LongTerm,
|
||||
@ -27,17 +28,26 @@ pub enum Category {
|
||||
Trash,
|
||||
}
|
||||
|
||||
#[serde_with::serde_as]
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct CalendarTime {
|
||||
time: NaiveTime,
|
||||
#[serde_as(as = "Option<DurationSeconds<i64>>")]
|
||||
reminder_offset: Option<Duration>,
|
||||
impl Category {
|
||||
pub fn eq_sql_predicate(&self) -> Box<dyn BoxableExpression<tasks::table, Pg, SqlType=Bool>> {
|
||||
use crate::schema::tasks::dsl::*;
|
||||
|
||||
match self {
|
||||
Category::Inbox => Box::new(category.contains(json!("Inbox"))),
|
||||
Category::SomedayMaybe => Box::new(category.contains(json!("SomedayMaybe"))),
|
||||
Category::WaitingFor(_) => Box::new(category.has_key("WaitingFor")),
|
||||
Category::NextSteps => Box::new(category.contains(json!("NextSteps"))),
|
||||
Category::Calendar { .. } => Box::new(category.has_key("Calendar")),
|
||||
Category::LongTerm => Box::new(category.contains(json!("LongTerm"))),
|
||||
Category::Done => Box::new(category.contains(json!("Done"))),
|
||||
Category::Trash => Box::new(category.contains(json!("Trash"))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CalendarTime {
|
||||
pub fn new(time: NaiveTime, reminder_offset: Option<Duration>) -> Self {
|
||||
Self { time, reminder_offset }
|
||||
impl PartialEq for Category {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
std::mem::discriminant(self) == std::mem::discriminant(other)
|
||||
}
|
||||
}
|
||||
|
||||
@ -63,3 +73,53 @@ impl FromSql<Jsonb, Pg> for Category {
|
||||
serde_json::from_str(str).map_err(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub enum ReoccurrenceInterval {
|
||||
Day,
|
||||
Month,
|
||||
Year,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct Reoccurrence {
|
||||
start_date: NaiveDate,
|
||||
interval: ReoccurrenceInterval,
|
||||
length: u32,
|
||||
}
|
||||
|
||||
impl Reoccurrence {
|
||||
pub fn new(start_date: NaiveDate, interval: ReoccurrenceInterval, length: u32) -> Self {
|
||||
Self { start_date, interval, length }
|
||||
}
|
||||
|
||||
pub fn interval(&self) -> &ReoccurrenceInterval {
|
||||
&self.interval
|
||||
}
|
||||
|
||||
pub fn length(&self) -> u32 {
|
||||
self.length
|
||||
}
|
||||
}
|
||||
|
||||
#[serde_with::serde_as]
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct CalendarTime {
|
||||
time: NaiveTime,
|
||||
#[serde_as(as = "Option<DurationSeconds<i64>>")]
|
||||
reminder_offset: Option<Duration>,
|
||||
}
|
||||
|
||||
impl CalendarTime {
|
||||
pub fn new(time: NaiveTime, reminder_offset: Option<Duration>) -> Self {
|
||||
Self { time, reminder_offset }
|
||||
}
|
||||
|
||||
pub fn time(&self) -> NaiveTime {
|
||||
self.time
|
||||
}
|
||||
|
||||
pub fn reminder_offset(&self) -> Option<Duration> {
|
||||
self.reminder_offset
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ use crate::schema::tasks;
|
||||
const TITLE_LENGTH_MIN: u64 = 1;
|
||||
const TITLE_LENGTH_MAX: u64 = 255;
|
||||
|
||||
#[derive(Queryable, Selectable, Serialize, Deserialize, Clone, Debug)]
|
||||
#[derive(Queryable, Selectable, Serialize, Deserialize, PartialEq, Clone, Debug)]
|
||||
#[diesel(table_name = crate::schema::tasks)]
|
||||
#[diesel(check_for_backend(diesel::pg::Pg))]
|
||||
pub struct Task {
|
||||
|
Reference in New Issue
Block a user