Skip to content

Commit

Permalink
[2/2] remove deleted time records from db
Browse files Browse the repository at this point in the history
  • Loading branch information
4DA committed Nov 27, 2021
1 parent 490efca commit 99cd5c1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/activity_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use chrono::prelude::*;
use crate::task::*;
use crate::app_model::*;
use crate::common::*;
use crate::db;

type TimeRecordCtx = (AppModel, TimeRecord);

Expand Down Expand Up @@ -123,24 +124,26 @@ impl ActivityLogWidget {
|outer: &mut AppModel, inner: (AppModel, Vector<TimeRecord>)|
{
if !outer.records.same(&inner.0.records) {
let diff = outer.records.clone().difference(inner.0.records.clone());
outer.records = inner.0.records;

let mut sums = TaskSums::new();
let mut records = TimeRecordMap::new();

for tr in inner.1 {
records.insert(*tr.from.clone(), tr);
}

// TODO don't rebuild sums completely, touch only upper prefices
for (uid, _) in &inner.0.tasks {
let sum = build_time_prefix_sum(&inner.0.tasks, &records, uid.clone());
let sum = build_time_prefix_sum(&inner.0.tasks, &outer.records,
uid.clone());
sums.insert(uid.clone(), sum);
}

outer.task_sums = sums;
// remove deleted time records from db
for (_k, d) in &diff {
if let Err(what) = db::remove_time_record(inner.0.db.clone(), d) {
println!("db error: {}", what);
}
}

// todo: remove time record from db
outer.task_sums = sums;
}
},
));
Expand Down
12 changes: 12 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@ pub fn add_time_record(conn: Rc<Connection>, record: &TimeRecord) -> anyhow::Res
Ok(())
}

pub fn remove_time_record(conn: Rc<Connection>, record: &TimeRecord) -> anyhow::Result<()>
{
conn.execute(
"DELETE FROM time_records WHERE ts_from = ?1",
params![TimeWrapper(*record.from)],
)?;

println!("remove ok | t: {:?}", &record);

Ok(())
}

pub fn get_time_records(conn: Rc<Connection>, from: &DateTime<Utc>, to: &DateTime<Utc>)
-> anyhow::Result<TimeRecordMap>
{
Expand Down

0 comments on commit 99cd5c1

Please sign in to comment.