Add modify to habitmgr.

This commit is contained in:
Nicolás A. Ortega Froysa 2022-07-19 14:45:44 +02:00
parent a52071258a
commit ac3b111053

View File

@ -74,7 +74,7 @@ impl HabitMgr
self.habits = serde_json::from_reader(habits_reader).unwrap(); self.habits = serde_json::from_reader(habits_reader).unwrap();
} }
fn write_habits(&self) fn export_habits(&self)
{ {
let habits_file = OpenOptions::new() let habits_file = OpenOptions::new()
.truncate(true) .truncate(true)
@ -95,7 +95,7 @@ impl HabitMgr
{ {
self.import_habits(); self.import_habits();
self.habits.push(Habit::new(name.clone(), bad, weight)); self.habits.push(Habit::new(name.clone(), bad, weight));
self.write_habits(); self.export_habits();
println!("New habit {} added.", &name); println!("New habit {} added.", &name);
} }
@ -104,10 +104,32 @@ impl HabitMgr
{ {
self.import_habits(); self.import_habits();
let old_habit = self.habits.remove(id); let old_habit = self.habits.remove(id);
self.write_habits(); self.export_habits();
println!("Removed habit {}", old_habit.get_name()); println!("Removed habit {}", old_habit.get_name());
} }
pub fn modify(&mut self, id:usize,
name:Option<String>,
bad:Option<bool>,
weight:Option<u8>,
_days:Option<String>)
{
self.import_habits();
if name.is_some()
{
self.habits[id].set_name(name.unwrap());
}
if bad.is_some()
{
self.habits[id].set_bad(bad.unwrap());
}
if weight.is_some()
{
self.habits[id].set_weight(weight.unwrap());
}
self.export_habits();
}
pub fn list(&mut self, _all:bool, _verbose:bool) pub fn list(&mut self, _all:bool, _verbose:bool)
{ {
self.import_habits(); self.import_habits();