From 4512790f2d42e6ead1b3172a23529f4122346908 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Fri, 22 Dec 2017 17:30:07 +0200 Subject: [PATCH] hammond-data: Add duration column to the episode table. --- .../down.sql | 22 ++++++++++++++ .../up.sql | 23 ++++++++++++++ hammond-data/src/dbqueries.rs | 3 +- hammond-data/src/models/insertables.rs | 2 ++ hammond-data/src/models/queryables.rs | 30 +++++++++++++++++++ hammond-data/src/parser.rs | 3 +- hammond-data/src/schema.rs | 1 + 7 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 hammond-data/migrations/2017-12-22-145740_add_duration_column/down.sql create mode 100644 hammond-data/migrations/2017-12-22-145740_add_duration_column/up.sql diff --git a/hammond-data/migrations/2017-12-22-145740_add_duration_column/down.sql b/hammond-data/migrations/2017-12-22-145740_add_duration_column/down.sql new file mode 100644 index 0000000..7f1bbcb --- /dev/null +++ b/hammond-data/migrations/2017-12-22-145740_add_duration_column/down.sql @@ -0,0 +1,22 @@ +ALTER TABLE episode RENAME TO old_table; + +CREATE TABLE episode ( + title TEXT NOT NULL, + uri TEXT, + local_uri TEXT, + description TEXT, + published_date TEXT, + epoch INTEGER NOT NULL DEFAULT 0, + length INTEGER, + guid TEXT, + played INTEGER, + podcast_id INTEGER NOT NULL, + favorite INTEGER DEFAULT 0, + archive INTEGER DEFAULT 0, + PRIMARY KEY (title, podcast_id) +); + +INSERT INTO episode (title, uri, local_uri, description, published_date, epoch, length, guid, played, favorite, archive, podcast_id) +SELECT title, uri, local_uri, description, published_date, epoch, length, guid, played, favorite, archive, podcast_id +FROM old_table; +Drop table old_table; diff --git a/hammond-data/migrations/2017-12-22-145740_add_duration_column/up.sql b/hammond-data/migrations/2017-12-22-145740_add_duration_column/up.sql new file mode 100644 index 0000000..f2dcf10 --- /dev/null +++ b/hammond-data/migrations/2017-12-22-145740_add_duration_column/up.sql @@ -0,0 +1,23 @@ +ALTER TABLE episode RENAME TO old_table; + +CREATE TABLE episode ( + title TEXT NOT NULL, + uri TEXT, + local_uri TEXT, + description TEXT, + published_date TEXT, + epoch INTEGER NOT NULL DEFAULT 0, + length INTEGER, + duration INTEGER, + guid TEXT, + played INTEGER, + podcast_id INTEGER NOT NULL, + favorite INTEGER DEFAULT 0, + archive INTEGER DEFAULT 0, + PRIMARY KEY (title, podcast_id) +); + +INSERT INTO episode (title, uri, local_uri, description, published_date, epoch, length, guid, played, favorite, archive, podcast_id) +SELECT title, uri, local_uri, description, published_date, epoch, length, guid, played, favorite, archive, podcast_id +FROM old_table; +Drop table old_table; \ No newline at end of file diff --git a/hammond-data/src/dbqueries.rs b/hammond-data/src/dbqueries.rs index 4f0c5d2..65236d6 100644 --- a/hammond-data/src/dbqueries.rs +++ b/hammond-data/src/dbqueries.rs @@ -111,6 +111,7 @@ pub fn get_episodes_widgets_with_limit(limit: u32) -> Result Result, published_date: Option, length: Option, + duration: Option, guid: Option, epoch: i32, podcast_id: i32, @@ -207,6 +208,7 @@ impl NewEpisode { if foo.title() != self.title.as_str() || foo.epoch() != self.epoch || foo.uri() != self.uri.as_ref().map(|s| s.as_str()) + || foo.duration() != self.duration { self.update(con, foo.rowid())?; } diff --git a/hammond-data/src/models/queryables.rs b/hammond-data/src/models/queryables.rs index aff1c4b..d4409cd 100644 --- a/hammond-data/src/models/queryables.rs +++ b/hammond-data/src/models/queryables.rs @@ -33,6 +33,7 @@ pub struct Episode { published_date: Option, epoch: i32, length: Option, + duration: Option, guid: Option, played: Option, favorite: bool, @@ -125,6 +126,8 @@ impl Episode { } /// Get the `length`. + /// + /// The number represents the size of the file in bytes. pub fn length(&self) -> Option { self.length } @@ -134,6 +137,18 @@ impl Episode { self.length = value; } + /// Get the `duration` value. + /// + /// The number represents the duration of the item/episode in seconds. + pub fn duration(&self) -> Option { + self.duration + } + + /// Set the `duration`. + pub fn set_duration(&mut self, value: Option) { + self.duration = value; + } + /// Epoch representation of the last time the episode was played. /// /// None/Null for unplayed. @@ -204,6 +219,7 @@ pub struct EpisodeWidgetQuery { local_uri: Option, epoch: i32, length: Option, + duration: Option, played: Option, // favorite: bool, // archive: bool, @@ -250,6 +266,8 @@ impl EpisodeWidgetQuery { } /// Get the `length`. + /// + /// The number represents the size of the file in bytes. pub fn length(&self) -> Option { self.length } @@ -259,6 +277,18 @@ impl EpisodeWidgetQuery { self.length = value; } + /// Get the `duration` value. + /// + /// The number represents the duration of the item/episode in seconds. + pub fn duration(&self) -> Option { + self.duration + } + + /// Set the `duration`. + pub fn set_duration(&mut self, value: Option) { + self.duration = value; + } + /// Epoch representation of the last time the episode was played. /// /// None/Null for unplayed. diff --git a/hammond-data/src/parser.rs b/hammond-data/src/parser.rs index ea7f7db..a08e025 100644 --- a/hammond-data/src/parser.rs +++ b/hammond-data/src/parser.rs @@ -64,13 +64,14 @@ pub(crate) fn new_episode(item: &Item, parent_id: i32) -> Result { let epoch = date.map(|x| x.timestamp() as i32).unwrap_or(0); let length = item.enclosure().map(|x| x.length().parse().unwrap_or(0)); - let _duration = parse_itunes_duration(item); + let duration = parse_itunes_duration(item); Ok(NewEpisodeBuilder::default() .title(title) .uri(uri) .description(description) .length(length) + .duration(duration) .published_date(pub_date) .epoch(epoch) .guid(guid) diff --git a/hammond-data/src/schema.rs b/hammond-data/src/schema.rs index 7b43171..6389143 100644 --- a/hammond-data/src/schema.rs +++ b/hammond-data/src/schema.rs @@ -8,6 +8,7 @@ table! { published_date -> Nullable, epoch -> Integer, length -> Nullable, + duration -> Nullable, guid -> Nullable, played -> Nullable, favorite -> Bool,