From 6c47403b11e7aaf1a22778bdc7615051779cb7bd Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 9 Jan 2022 14:50:45 +0100 Subject: Allow to delete events --- src/app/form.rs | 23 +++++++++++++++++++++-- src/app/update.rs | 10 ++++++++++ src/db/mod.rs | 9 +++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/app/form.rs b/src/app/form.rs index 6c42cd0..7f75db0 100644 --- a/src/app/form.rs +++ b/src/app/form.rs @@ -46,11 +46,14 @@ pub async fn show(app: &App, event: Event, is_new: bool) { vbox.append(&label("Fin")); vbox.append(&end); - let button = gtk::Button::with_label("Créer"); + let button = gtk::Button::builder() + .label(if is_new { "Créer" } else { "Modifier" }) + .margin_bottom(10) + .build(); vbox.append(&button); let conn = app.conn.clone(); let tx = app.tx.clone(); - button.connect_clicked(glib::clone!(@weak dialog => move |_| { + button.connect_clicked(glib::clone!(@weak dialog, @strong event => move |_| { match event::validate(event.id, date.buffer().text(), name.buffer().text(), start.buffer().text(), end.buffer().text()) { Some(new) => { match if is_new { db::insert(&conn, &new) } else { db::update(&conn, &new) } { @@ -66,6 +69,22 @@ pub async fn show(app: &App, event: Event, is_new: bool) { } })); + if !is_new { + let button = gtk::Button::builder().label("Supprimer").build(); + vbox.append(&button); + let conn = app.conn.clone(); + let tx = app.tx.clone(); + button.connect_clicked(glib::clone!(@weak dialog => move |_| { + match db::delete(&conn, &event.id) { + Ok(_) => { + update::send(tx.clone(), Msg::DeleteEvent { event: event.clone() }); + dialog.close() + }, + Err(_) => () + } + })); + } + dialog.run_future().await; } diff --git a/src/app/update.rs b/src/app/update.rs index f1576b5..baf4651 100644 --- a/src/app/update.rs +++ b/src/app/update.rs @@ -17,6 +17,7 @@ pub enum Msg { ShowUpdateForm { event: Event }, AddEvent { new: Event }, UpdateEvent { old: Event, new: Event }, + DeleteEvent { event: Event }, } pub async fn event_handler(rx: Receiver, mut app: App) { @@ -47,6 +48,15 @@ pub async fn event_handler(rx: Receiver, mut app: App) { None => println!("Event not found when updating from {:?} to {:?}", old, new), } } + Msg::DeleteEvent { event } => { + match app.events.iter().position(|e| e.id == event.id) { + Some(index) => { + app.events.remove(index); + calendar::refresh_date(&app, event.date); + } + None => println!("Event not found when trying to delete {:?}", event), + } + } } } } diff --git a/src/db/mod.rs b/src/db/mod.rs index 03692e9..0dd4ddf 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -30,6 +30,15 @@ pub fn update(conn: &Connection, event: &Event) -> Result<()> { Ok(()) } +pub fn delete(conn: &Connection, id: &Uuid) -> Result<()> { + conn.execute( + "DELETE FROM events WHERE id = ?", + params![id.to_hyphenated().to_string()], + )?; + + Ok(()) +} + // TODO: Don’t use unwrap pub fn list(conn: &Connection) -> Result> { let mut stmt = conn.prepare("SELECT id, date, start, end, name FROM events")?; -- cgit v1.2.3