aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLibravatar cel 🌸 <cel@blos.sm>2024-09-22 17:35:32 +0100
committerLibravatar cel 🌸 <cel@blos.sm>2024-09-22 17:35:32 +0100
commit811ff64dd08c8835d19c57fd959e42a28d800a53 (patch)
tree7290a8ba56da79d65ea858c8a36fd245862e33e1 /src
parentc3fde18b74fa967a02b3a922a9a3554b80207b06 (diff)
downloadfj-811ff64dd08c8835d19c57fd959e42a28d800a53.tar.gz
fj-811ff64dd08c8835d19c57fd959e42a28d800a53.tar.bz2
fj-811ff64dd08c8835d19c57fd959e42a28d800a53.zip
WIP: dbHEADmain
Diffstat (limited to '')
-rw-r--r--src/db.rs40
-rw-r--r--src/task.rs21
2 files changed, 56 insertions, 5 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),
}
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 {