From d3a6a99e9c1751ec08bed526d4ce7237425cdf73 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 23 Aug 2020 17:06:24 +0200 Subject: Change start/end times to complete timestamps. The way we had this until now with a date and separate start and end times does not really work. There are cases when a screening starts on one day, but ends on the next. By keeping a complete timestamp for both the start and end times, we don't fall into this problem. --- .../2020-08-23-140315_fix_screening_timestamps/down.sql | 7 +++++++ .../2020-08-23-140315_fix_screening_timestamps/up.sql | 10 ++++++++++ public/js/app.js | 2 +- src/controllers/screening.rs | 13 ++++++++----- src/db.rs | 14 ++++++-------- src/models/screening.rs | 16 ++++++---------- src/schema.rs | 5 ++--- templates/screening/list.tera | 4 ++-- templates/screening/new.tera | 7 ++----- 9 files changed, 44 insertions(+), 34 deletions(-) create mode 100644 migrations/2020-08-23-140315_fix_screening_timestamps/down.sql create mode 100644 migrations/2020-08-23-140315_fix_screening_timestamps/up.sql diff --git a/migrations/2020-08-23-140315_fix_screening_timestamps/down.sql b/migrations/2020-08-23-140315_fix_screening_timestamps/down.sql new file mode 100644 index 0000000..49ceb2c --- /dev/null +++ b/migrations/2020-08-23-140315_fix_screening_timestamps/down.sql @@ -0,0 +1,7 @@ +-- Revert date and timestamps back to separate state. +-- There should be no reason to do this, but included for completeness- +-- This will re-add the 'date' column, but not set the proper date. +ALTER TABLE screenings + ADD COLUMN date TYPE DATE NOT NULL, + ALTER COLUMN start_time TYPE TIME, + ALTER COLUMN end_time TYPE TIME; diff --git a/migrations/2020-08-23-140315_fix_screening_timestamps/up.sql b/migrations/2020-08-23-140315_fix_screening_timestamps/up.sql new file mode 100644 index 0000000..fd08e80 --- /dev/null +++ b/migrations/2020-08-23-140315_fix_screening_timestamps/up.sql @@ -0,0 +1,10 @@ +-- Change the type of start and end times to include data and timezone. +-- Drop the now obsolete date table +ALTER TABLE screenings + ALTER COLUMN start_time TYPE timestamp with time zone + USING date::TIMESTAMP + start_time, + + ALTER COLUMN end_time TYPE timestamp with time zone + USING date::TIMESTAMP + end_time, + + DROP COLUMN date; diff --git a/public/js/app.js b/public/js/app.js index 65bb92f..d661b52 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -19,7 +19,7 @@ function load_screenings(data_done) { let req = new XMLHttpRequest() req.addEventListener("load", data_done) - req.open("GET", "ramaskrik-2019.json") + req.open("GET", "/screenings") req.responseType = "json" req.send() } diff --git a/src/controllers/screening.rs b/src/controllers/screening.rs index 9e78130..aca8e32 100644 --- a/src/controllers/screening.rs +++ b/src/controllers/screening.rs @@ -68,18 +68,21 @@ pub fn new_screening(db: db::Connection) -> Result> { pub struct NewScreeningForm { film_id: i32, room_id: i32, - date: String, start_time: String, end_time: String, } #[post("/", format = "application/x-www-form-urlencoded", data = "")] pub fn create_screening(db: db::Connection, screening: Form) -> Result> { - let date = chrono::NaiveDate::parse_from_str(dbg!(&screening.date), "%Y-%m-%d")?; - let start_time = chrono::NaiveTime::parse_from_str(dbg!(&screening.start_time), "%H:%M")?; - let end_time = chrono::NaiveTime::parse_from_str(dbg!(&screening.end_time), "%H:%M")?; + let start_time = chrono::DateTime::parse_from_rfc3339(dbg!(&screening.start_time))?; + let end_time = chrono::DateTime::parse_from_rfc3339(dbg!(&screening.end_time))?; + + db.create_screening( + dbg!(screening.room_id), + dbg!(screening.film_id), + start_time.with_timezone(&chrono::Utc), + end_time.with_timezone(&chrono::Utc))?; - db.create_screening(dbg!(screening.room_id), dbg!(screening.film_id), date, start_time, end_time)?; Ok(Redirect::to("/screenings")) } diff --git a/src/db.rs b/src/db.rs index 4297d25..4c562ef 100644 --- a/src/db.rs +++ b/src/db.rs @@ -36,11 +36,10 @@ impl Connection { &self, room_id: i32, film_id: i32, - date: chrono::NaiveDate, - start_time: chrono::NaiveTime, - end_time: chrono::NaiveTime) -> QueryResult + start_time: chrono::DateTime, + end_time: chrono::DateTime) -> QueryResult { - let s = models::NewScreening { room_id, film_id, date, start_time, end_time }; + let s = models::NewScreening { room_id, film_id, start_time, end_time }; diesel::insert_into(screenings::table) .values(&s) .execute(&**self) @@ -50,11 +49,10 @@ impl Connection { &self, room: &models::Room, film: &models::Film, - date: chrono::NaiveDate, - start_time: chrono::NaiveTime, - end_time: chrono::NaiveTime) -> QueryResult + start_time: chrono::DateTime, + end_time: chrono::DateTime) -> QueryResult { - self.create_screening(room.id, film.id, date, start_time, end_time) + self.create_screening(room.id, film.id, start_time, end_time) } pub fn get_screenings(&self) -> QueryResult> { diff --git a/src/models/screening.rs b/src/models/screening.rs index b111218..1426605 100644 --- a/src/models/screening.rs +++ b/src/models/screening.rs @@ -28,9 +28,8 @@ joinable!(screenings -> films (film_id)); pub struct NewScreening { pub film_id: i32, pub room_id: i32, - pub date: chrono::NaiveDate, - pub start_time: chrono::NaiveTime, - pub end_time: chrono::NaiveTime, + pub start_time: chrono::DateTime, + pub end_time: chrono::DateTime, } #[derive(Deserialize, Identifiable, PartialEq, Serialize, Queryable)] @@ -39,9 +38,8 @@ pub struct Screening { pub id: i32, pub film_id: i32, pub room_id: i32, - pub date: chrono::NaiveDate, - pub start_time: chrono::NaiveTime, - pub end_time: chrono::NaiveTime, + pub start_time: chrono::DateTime, + pub end_time: chrono::DateTime, } /// Aggregate screening, room and film info into one struct. @@ -50,9 +48,8 @@ pub struct AggregatedScreening { pub id: i32, pub film: Film, pub room: Room, - pub date: chrono::NaiveDate, - pub start_time: chrono::NaiveTime, - pub end_time: chrono::NaiveTime, + pub start_time: chrono::DateTime, + pub end_time: chrono::DateTime, } impl AggregatedScreening { @@ -62,7 +59,6 @@ impl AggregatedScreening { id: s.id, film: f.as_ref().unwrap().clone().to_owned(), room: r.as_ref().unwrap().clone().to_owned(), - date: s.date, start_time: s.start_time, end_time: s.end_time, } diff --git a/src/schema.rs b/src/schema.rs index 88e1e00..8931558 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -18,9 +18,8 @@ table! { id -> Int4, film_id -> Int4, room_id -> Int4, - date -> Date, - start_time -> Time, - end_time -> Time, + start_time -> Timestamptz, + end_time -> Timestamptz, } } diff --git a/templates/screening/list.tera b/templates/screening/list.tera index 263d3ea..55ac971 100644 --- a/templates/screening/list.tera +++ b/templates/screening/list.tera @@ -15,8 +15,8 @@ No screenings defined. {% for s in screenings -%} - - + +
{{ s.date }}{{ s.start_time }} - {{ s.end_time }}:{{ s.start_time | date }}{{ s.start_time | date(format="%H:%M") }} - {{ s.end_time | date(format="%H:%M") }}: {{ s.film.title }} ({{ s.room.name }}) diff --git a/templates/screening/new.tera b/templates/screening/new.tera index 0c3eb73..fd6be2f 100644 --- a/templates/screening/new.tera +++ b/templates/screening/new.tera @@ -20,14 +20,11 @@ {% endfor %} - - - - + - + -- cgit v1.2.3