pub struct Tasks {
pub conn: Connection,
pub id: Option<i32>,
}Expand description
Task table access; remembers the last inserted id for chaining.
Fields§
§conn: Connection§id: Option<i32>Id of the most recently inserted task, for update_id/get chaining.
Implementations§
Source§impl Tasks
impl Tasks
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Opens the database and ensures the tasks table exists.
use kasl::db::tasks::Tasks;
let mut tasks = Tasks::new()?;Sourcepub fn insert(&mut self, task: &Task) -> Result<&mut Self>
pub fn insert(&mut self, task: &Task) -> Result<&mut Self>
Inserts the task, storing the assigned id for chaining.
use kasl::db::tasks::Tasks;
use kasl::libs::task::Task;
let mut tasks = Tasks::new()?;
let task = Task::new("Code review", "Review PR #123", Some(50));
tasks.insert(&task)?
.update_id()?; // Method chainingSourcepub fn update_id(&mut self) -> Result<&mut Self>
pub fn update_id(&mut self) -> Result<&mut Self>
Points the just-inserted task’s task_id at its own id - the
convention for a standalone task that groups its own history.
use kasl::libs::task::Task;
let mut tasks = Tasks::new()?;
let subtask = Task::new("Subtask", "Part of larger task", Some(0));
tasks.insert(&subtask)?
.update_id()?;Sourcepub fn get(&mut self) -> Result<Vec<Task>>
pub fn get(&mut self) -> Result<Vec<Task>>
Fetches the most recently inserted task.
use kasl::libs::task::Task;
let mut tasks = Tasks::new()?;
let task = Task::new("New task", "Description", Some(100));
let inserted_tasks = tasks.insert(&task)?
.get()?;Sourcepub fn fetch(&mut self, filter: TaskFilter) -> Result<Vec<Task>>
pub fn fetch(&mut self, filter: TaskFilter) -> Result<Vec<Task>>
Runs the query for the given filter and attaches each task’s tags.
use kasl::db::tasks::Tasks;
use kasl::libs::task::TaskFilter;
use chrono::Local;
let mut tasks = Tasks::new()?;
let all_tasks = tasks.fetch(TaskFilter::All)?;
let today = tasks.fetch(TaskFilter::Date(Local::now().date_naive()))?;
let urgent = tasks.fetch(TaskFilter::ByTag("urgent".to_string()))?;
let incomplete = tasks.fetch(TaskFilter::Incomplete)?;Sourcepub fn delete(&mut self, id: i32) -> Result<usize>
pub fn delete(&mut self, id: i32) -> Result<usize>
Deletes one task; returns the number of rows removed.
let mut tasks = Tasks::new()?;
let task_id = 1;
let deleted_count = tasks.delete(task_id)?;
if deleted_count > 0 {
println!("Task deleted successfully");
}Sourcepub fn delete_many(&mut self, ids: &[i32]) -> Result<usize>
pub fn delete_many(&mut self, ids: &[i32]) -> Result<usize>
Deletes several tasks in one statement; unknown ids are simply not counted, and an empty slice is a no-op.
let mut tasks = Tasks::new()?;
let ids_to_delete = vec![101, 102, 103];
let deleted_count = tasks.delete_many(&ids_to_delete)?;
println!("Deleted {} tasks", deleted_count);Sourcepub fn exists(&mut self, id: i32) -> Result<bool>
pub fn exists(&mut self, id: i32) -> Result<bool>
True when a task with this id exists.
let mut tasks = Tasks::new()?;
let task_id = 1;
if tasks.exists(task_id)? {
println!("Task exists and can be updated");
} else {
println!("Task not found");
}Sourcepub fn update(&mut self, task: &Task) -> Result<()>
pub fn update(&mut self, task: &Task) -> Result<()>
Updates name, comment and completeness; identity fields and tag links stay as they are. Errors when the task has no id or no longer exists.
let mut tasks = Tasks::new()?;
let task_id = 1;
let mut task = tasks.get_by_id(task_id)?.unwrap();
task.name = "Updated task name".to_string();
task.completeness = Some(75);
tasks.update(&task)?;