Skip to content

Commit

Permalink
Added show command, which shows annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
grayson committed Jul 11, 2020
1 parent 8ad75e4 commit f43ee14
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn add_task(conn: &Connection, task: Task) -> Result<(), rusqlite::Error> {
params![task.description, created]
)?;

// TODO: This is borked.
let row_id = conn.last_insert_rowid();
for annotation in task.annotations.iter() {
add_annotation(conn, row_id, annotation)?;
Expand Down Expand Up @@ -74,6 +75,48 @@ fn show_task_list(conn: &Connection) -> Result<(), rusqlite::Error> {
Ok(())
}

fn show_task_details(conn: &Connection, task_id: i64) -> Result<(), rusqlite::Error> {
let mut statement = conn.prepare("SELECT * FROM tasks WHERE id = ?")?;

let task_iterator = statement.query_map(params![task_id], |row| {
let created: String = row.get(2)?;
Ok(Task {
id: row.get(0)?,
description: row.get(1)?,
created: DateTime::parse_from_rfc2822(&created).unwrap().with_timezone(&Utc),
annotations: vec![]
})
})?;

let mut annotation_statement = conn.prepare("SELECT * FROM annotations WHERE task_id = ?")?;

let annotation_iterator = annotation_statement.query_map(params![task_id], |row| {
let annotation: String = row.get(2)?;
Ok(annotation)
})?;

let mut annotation_table = Table::new();
annotation_table.set_format(*format::consts::FORMAT_NO_BORDER);
for annotationr in annotation_iterator {
let annotation = annotationr.unwrap();
annotation_table.add_row(row![annotation]);
}

let mut table = Table::new();
table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
table.set_titles(row!["id", "description", "created", "annotations"]);
for taskr in task_iterator {
let task = taskr.unwrap();
let id = task.id.unwrap();

table.add_row(row![id, task.description, task.created.to_rfc2822(), annotation_table]);
}

table.printstd();

Ok(())
}

fn initialize() -> Result<Connection, rusqlite::Error> {
let conn = Connection::open("test.db")?;

Expand Down Expand Up @@ -177,7 +220,8 @@ fn main() -> () {
show_task_list(&conn).expect("Could not show task list.");
},
("show", Some(submatches)) => {
println!("Not implemented yet");
show_task_details(&conn, value_t!(submatches.value_of("task_id"), i64).unwrap())
.expect("Could not get task details.");
},
("annotate", Some(submatches)) => {
add_annotation(
Expand Down

0 comments on commit f43ee14

Please sign in to comment.