From 5166efe517291f5c9fc6326f30651ef799d6db65 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 9 Jan 2022 13:39:10 +0100 Subject: Add id in event model --- src/app/app.rs | 4 +++- src/app/calendar.rs | 23 +++++++++++++++++++---- src/app/form.rs | 5 +++-- src/app/update.rs | 29 ++++++----------------------- src/db/migrations/1-init.sql | 2 +- src/db/mod.rs | 17 ++++++++++------- src/model/event.rs | 5 ++++- 7 files changed, 46 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/app/app.rs b/src/app/app.rs index 45904a9..d93b544 100644 --- a/src/app/app.rs +++ b/src/app/app.rs @@ -17,6 +17,7 @@ pub struct App { pub events: Vec, pub today: NaiveDate, pub start_date: NaiveDate, + pub tx: Sender, } impl App { @@ -35,7 +36,7 @@ impl App { let start_date = NaiveDate::from_isoywd(today.year(), today.iso_week().week(), Weekday::Mon); let events = db::list(&conn).unwrap_or(vec![]); - let grid = calendar::grid(tx, &today, &start_date, &events); + let grid = calendar::create(tx.clone(), &today, &start_date, &events); window.set_child(Some(&grid)); @@ -52,6 +53,7 @@ impl App { events, today, start_date, + tx, } } } diff --git a/src/app/calendar.rs b/src/app/calendar.rs index 847ea71..250101f 100644 --- a/src/app/calendar.rs +++ b/src/app/calendar.rs @@ -5,14 +5,14 @@ use chrono::{Datelike, NaiveDate}; use gtk::glib; use gtk::prelude::*; -use crate::{app::update, app::update::Msg, model::event::Event}; +use crate::{app::update, app::update::Msg, app::App, model::event::Event}; static DAYS: [&str; 7] = ["LUN", "MAR", "MER", "JEU", "VEN", "SAM", "DIM"]; static MONTHES: [&str; 12] = [ "Jan", "Fév", "Mar", "Avr", "Mai", "Juin", "Juil", "Aoû", "Sep", "Oct", "Nov", "Déc", ]; -pub fn grid( +pub fn create( tx: Sender, today: &NaiveDate, start_date: &NaiveDate, @@ -24,12 +24,12 @@ pub fn grid( grid.attach(&day_title(col), col, 0, 1, 1); } - show_days(tx, &grid, &start_date, &today, &events); + attach_days(tx, &grid, &start_date, &today, &events); grid } -fn show_days( +fn attach_days( tx: Sender, grid: >k::Grid, start_date: &NaiveDate, @@ -45,6 +45,21 @@ fn show_days( } } +pub fn refresh_date(app: &App, date: NaiveDate) { + let d = date.signed_duration_since(app.start_date).num_days(); + + let col = (d % 7) as i32; + let row = 1 + (d / 7) as i32; + + app.grid.attach( + &day_entry(app.tx.clone(), &date, &app.today, &app.events), + col, + row, + 1, + 1, + ) +} + fn day_title(col: i32) -> gtk::Box { let vbox = gtk::Box::builder() .orientation(gtk::Orientation::Vertical) diff --git a/src/app/form.rs b/src/app/form.rs index fc3dc83..08a2af1 100644 --- a/src/app/form.rs +++ b/src/app/form.rs @@ -6,10 +6,11 @@ use gtk::glib; use gtk::prelude::*; use rusqlite::Connection; use std::rc::Rc; +use uuid::Uuid; use crate::{app::update, app::update::Msg, db, model::event}; -pub async fn dialog( +pub async fn show( conn: Rc, tx: Sender, window: Rc, @@ -49,7 +50,7 @@ pub async fn dialog( let button = gtk::Button::with_label("Créer"); vbox.append(&button); button.connect_clicked(glib::clone!(@weak dialog => move |_| { - match event::validate(date.buffer().text(), name.buffer().text(), start.buffer().text(), end.buffer().text()) { + match event::validate(Uuid::new_v4(), date.buffer().text(), name.buffer().text(), start.buffer().text(), end.buffer().text()) { Some(event) => { match db::insert(&conn, &event) { Ok(_) => { diff --git a/src/app/update.rs b/src/app/update.rs index 288ec51..e7bf7af 100644 --- a/src/app/update.rs +++ b/src/app/update.rs @@ -1,16 +1,12 @@ -use gtk4 as gtk; - use async_channel::{Receiver, Sender}; use chrono::NaiveDate; -use gtk::prelude::*; use rusqlite::Connection; use std::rc::Rc; -use crate::app::calendar; -use crate::app::form; -use crate::app::utils; -use crate::app::App; -use crate::model::event::Event; +use crate::{ + app::{calendar, form, utils, App}, + model::event::Event, +}; pub fn send(tx: Sender, msg: Msg) { utils::spawn(async move { @@ -27,25 +23,12 @@ pub async fn event_handler(conn: Rc, rx: Receiver, tx: Sender { - form::dialog(Rc::clone(&conn), tx.clone(), Rc::clone(&app.window), date).await; + form::show(Rc::clone(&conn), tx.clone(), Rc::clone(&app.window), date).await; } Msg::AddEvent { event } => { let date = event.date.clone(); - - let d = date.signed_duration_since(app.start_date).num_days(); - app.events.push(event); - - let col = (d % 7) as i32; - let row = 1 + (d / 7) as i32; - - app.grid.attach( - &calendar::day_entry(tx.clone(), &date, &app.today, &app.events), - col, - row, - 1, - 1, - ); + calendar::refresh_date(&app, date); } } } diff --git a/src/db/migrations/1-init.sql b/src/db/migrations/1-init.sql index 72fab80..39b845b 100644 --- a/src/db/migrations/1-init.sql +++ b/src/db/migrations/1-init.sql @@ -1,5 +1,5 @@ CREATE TABLE IF NOT EXISTS "events" ( - "id" INTEGER PRIMARY KEY, + "id" VARCHAR PRIMARY KEY, "date" VARCHAR NOT NULL, "start" VARCHAR NULL, "end" VARCHAR NULL, diff --git a/src/db/mod.rs b/src/db/mod.rs index 6cfa2eb..23cec7e 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,6 +1,7 @@ use anyhow::Result; use rusqlite::{params, Connection}; use rusqlite_migration::{Migrations, M}; +use uuid::Uuid; use crate::model::event::Event; @@ -13,22 +14,24 @@ pub fn init() -> Result { pub fn insert(conn: &Connection, event: &Event) -> Result<()> { conn.execute( - "INSERT INTO events (date, start, end, name, created, updated) VALUES (?, ?, ?, ?, datetime(), datetime())", - params![event.date, event.start, event.end, event.name] + "INSERT INTO events (id, date, start, end, name, created, updated) VALUES (?, ?, ?, ?, ?, datetime(), datetime())", + params![event.id.to_hyphenated().to_string(), event.date, event.start, event.end, event.name] )?; Ok(()) } pub fn list(conn: &Connection) -> Result> { - let mut stmt = conn.prepare("SELECT date, start, end, name FROM events")?; + let mut stmt = conn.prepare("SELECT id, date, start, end, name FROM events")?; let iter = stmt.query_map([], |row| { + let uuid: String = row.get(0)?; Ok(Event { - date: row.get(0)?, - start: row.get(1)?, - end: row.get(2)?, - name: row.get(3)?, + id: Uuid::parse_str(&uuid).unwrap(), + date: row.get(1)?, + start: row.get(2)?, + end: row.get(3)?, + name: row.get(4)?, }) })?; diff --git a/src/model/event.rs b/src/model/event.rs index a0bdc85..27587bc 100644 --- a/src/model/event.rs +++ b/src/model/event.rs @@ -1,10 +1,12 @@ use chrono::Timelike; use chrono::{NaiveDate, NaiveTime}; +use uuid::Uuid; pub static DATE_FORMAT: &str = "%d/%m/%Y"; #[derive(Debug, Clone)] pub struct Event { + pub id: Uuid, pub date: NaiveDate, pub start: Option, pub end: Option, @@ -50,7 +52,7 @@ fn parse_time(t: &str) -> Option { // Validation -pub fn validate(date: String, name: String, start: String, end: String) -> Option { +pub fn validate(id: Uuid, date: String, name: String, start: String, end: String) -> Option { let start = validate_time(start)?; let end = validate_time(end)?; @@ -60,6 +62,7 @@ pub fn validate(date: String, name: String, start: String, end: String) -> Optio } Some(Event { + id, date: NaiveDate::parse_from_str(&date, DATE_FORMAT).ok()?, name: validate_name(name)?, start, -- cgit v1.2.3