aboutsummaryrefslogtreecommitdiffstats
path: root/src/db.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs40
1 files changed, 37 insertions, 3 deletions
diff --git a/src/db.rs b/src/db.rs
index dd35e4d..db5534c 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -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),
}