aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoris2022-01-09 14:50:45 +0100
committerJoris2022-01-09 14:50:45 +0100
commit6c47403b11e7aaf1a22778bdc7615051779cb7bd (patch)
tree66b643ed4032b65e16dffce9182a6a1e46c56e3a /src
parentd0a9063631e71928484a698f6c585ebb3915e8a2 (diff)
Allow to delete events
Diffstat (limited to 'src')
-rw-r--r--src/app/form.rs23
-rw-r--r--src/app/update.rs10
-rw-r--r--src/db/mod.rs9
3 files changed, 40 insertions, 2 deletions
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<Msg>, mut app: App) {
@@ -47,6 +48,15 @@ pub async fn event_handler(rx: Receiver<Msg>, 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<Vec<Event>> {
let mut stmt = conn.prepare("SELECT id, date, start, end, name FROM events")?;