diff options
Diffstat (limited to '')
| -rw-r--r-- | src/db.rs | 40 | ||||
| -rw-r--r-- | src/task.rs | 21 | 
2 files changed, 56 insertions, 5 deletions
| @@ -7,7 +7,7 @@ struct Db {  }  impl Db { -    pub async fn create_task(&self, task: Task) -> Result<i64> { +    pub async fn create_task(&self, task: Task) -> Result<Task> {          let id = sqlx::query!(              "insert into tasks ( name, cron, archived, description ) values ( ?1, ?2, ?3, ?4 )",              task.name, @@ -19,10 +19,44 @@ impl Db {          .await?          .last_insert_rowid(); -        Ok(id) +        for category in task.categories.clone() { +            let category_id = sqlx::query!( +                "insert or ignore into categories ( name ) values ( ?1 )", +                category +            ) +            .execute(&self.pool) +            .await? +            .last_insert_rowid(); + +            sqlx::query!( +                "insert into tasks_categories ( task_id, category_id ) values ( ?1, ?2 )", +                id, +                category_id +            ) +            .execute(&self.pool) +            .await?; +        } + +        Ok(task.add_id(id))      } -    pub async fn read_tasks(&self) -> Result<Vec<Task>> { +    pub async fn read_tasks(&self, select: Vec<TaskSelect>) -> Result<Vec<Task>> { +        Ok(sqlx::query!("select * from tasks") +            .fetch_all(&self.pool) +            .await?) +    } + +    pub async fn update_tasks(&self, select: Vec<TaskSelect>) -> Result<()> {          todo!()      } + +    pub async fn delete_tasks(&self, select: Vec<TaskSelect>) -> Result<()> { +        todo!() +    } +} + +pub enum TaskSelect { +    Name(String), +    Archived(bool), +    Category(String),  } diff --git a/src/task.rs b/src/task.rs index dcfe776..59aa502 100644 --- a/src/task.rs +++ b/src/task.rs @@ -1,36 +1,52 @@  use std::{      borrow::{Borrow, Cow}, +    collections::HashSet,      str::FromStr,  };  use chrono::{NaiveDateTime, TimeDelta};  use sqlx::{      encode::IsNull, +    prelude::FromRow,      sqlite::{SqliteArgumentValue, SqliteTypeInfo, SqliteValue},      Decode, Sqlite, Type, TypeInfo, Value, ValueRef,  }; +#[derive(FromRow)]  pub struct Task { +    id: Option<i64>,      pub name: String,      pub cron: Option<Schedule>,      pub archived: bool,      pub description: Option<String>, +    pub categories: HashSet<String>,  }  impl Task {      pub fn new(          name: String,          cron: Option<Schedule>, -        archived: bool,          description: Option<String>, +        categories: Option<HashSet<String>>,      ) -> Self {          Self { +            id: None,              name,              cron, -            archived, +            archived: false,              description, +            categories: categories.unwrap_or_else(|| HashSet::new()),          }      } + +    pub fn id(&self) -> Option<i64> { +        self.id +    } + +    pub fn add_id(mut self, id: i64) -> Self { +        self.id = Some(id); +        self +    }  }  pub struct Schedule(cron::Schedule); @@ -64,6 +80,7 @@ impl Type<Sqlite> for Schedule {      }  } +#[repr(transparent)]  pub struct Category(String);  impl Category { | 
