diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2020-08-23 17:06:24 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2020-08-23 17:06:24 +0200 |
commit | d3a6a99e9c1751ec08bed526d4ce7237425cdf73 (patch) | |
tree | 1a045f2d0059610a5bc60d200f6becf309a211a8 | |
parent | 0a2d6fc06fc1988f860ab81fe53d1e6dae470407 (diff) | |
download | ramaskrik-social-d3a6a99e9c1751ec08bed526d4ce7237425cdf73.tar.gz ramaskrik-social-d3a6a99e9c1751ec08bed526d4ce7237425cdf73.tar.bz2 ramaskrik-social-d3a6a99e9c1751ec08bed526d4ce7237425cdf73.zip |
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.
-rw-r--r-- | migrations/2020-08-23-140315_fix_screening_timestamps/down.sql | 7 | ||||
-rw-r--r-- | migrations/2020-08-23-140315_fix_screening_timestamps/up.sql | 10 | ||||
-rw-r--r-- | public/js/app.js | 2 | ||||
-rw-r--r-- | src/controllers/screening.rs | 13 | ||||
-rw-r--r-- | src/db.rs | 14 | ||||
-rw-r--r-- | src/models/screening.rs | 16 | ||||
-rw-r--r-- | src/schema.rs | 5 | ||||
-rw-r--r-- | templates/screening/list.tera | 4 | ||||
-rw-r--r-- | templates/screening/new.tera | 7 |
9 files changed, 44 insertions, 34 deletions
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<Template, Box<dyn Error>> { 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 = "<screening>")] pub fn create_screening(db: db::Connection, screening: Form<NewScreeningForm>) -> Result<Redirect, Box<dyn Error>> { - 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")) } @@ -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<usize> + start_time: chrono::DateTime<chrono::Utc>, + end_time: chrono::DateTime<chrono::Utc>) -> QueryResult<usize> { - 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<usize> + start_time: chrono::DateTime<chrono::Utc>, + end_time: chrono::DateTime<chrono::Utc>) -> QueryResult<usize> { - 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<Vec<models::Screening>> { 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<chrono::Utc>, + pub end_time: chrono::DateTime<chrono::Utc>, } #[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<chrono::Utc>, + pub end_time: chrono::DateTime<chrono::Utc>, } /// 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<chrono::Utc>, + pub end_time: chrono::DateTime<chrono::Utc>, } 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. <table> {% for s in screenings -%} <tr> - <td>{{ s.date }}</td> - <td>{{ s.start_time }} - {{ s.end_time }}:</td> + <td>{{ s.start_time | date }}</td> + <td>{{ s.start_time | date(format="%H:%M") }} - {{ s.end_time | date(format="%H:%M") }}:</td> <td>{{ s.film.title }}</td> <td>({{ s.room.name }})</td> <td> 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 @@ <option value="{{ room.id }}">{{ room.name }}</option> {% endfor %} - <label for="date">Date: </label> - <input name="date" type="date"> - <label for="start_time">Start time: </label> - <input name="start_time" type="time"> + <input name="start_time" type="datetime"> <label for="end_time">End time: </label> - <input name="end_time" type="time"> + <input name="end_time" type="datetime"> </select> <input type="submit" value="Save"> |