From 7630f78e37e2c2ef62bdd2c1f079911666006d6b Mon Sep 17 00:00:00 2001 From: dreamer Date: Sat, 26 Dec 2020 04:12:55 +0100 Subject: [PATCH] html view + .json for track/artist/release --- code/app.lua | 6 ++-- code/handlers/artisthandler.lua | 23 ++++++++++----- code/handlers/releasehandler.lua | 19 +++++++++---- code/handlers/trackhandler.lua | 20 +++++++++---- code/views/artist.etlua | 48 ++++++++++++++++++++++++++++++++ code/views/release.etlua | 34 ++++++++++++++++++++++ code/views/track.etlua | 46 ++++++++++++++++++++++++++++++ 7 files changed, 176 insertions(+), 20 deletions(-) create mode 100644 code/views/artist.etlua create mode 100644 code/views/release.etlua create mode 100644 code/views/track.etlua diff --git a/code/app.lua b/code/app.lua index b943f71..145494b 100644 --- a/code/app.lua +++ b/code/app.lua @@ -22,15 +22,15 @@ app:match("station", "/station/:station(.:json)", json_params(function(self) return handlers.Stationhandler(self) end)) -app:match("track", "/track/:track", json_params(function(self) +app:match("track", "/track/:track(.:json)", json_params(function(self) return handlers.Trackhandler(self) end)) -app:match("artist", "/artist/:artist", json_params(function(self) +app:match("artist", "/artist/:artist(.:json)", json_params(function(self) return handlers.Artisthandler(self) end)) -app:match("release", "/release/:release", json_params(function(self) +app:match("release", "/release/:release(.:json)", json_params(function(self) return handlers.Releasehandler(self) end)) diff --git a/code/handlers/artisthandler.lua b/code/handlers/artisthandler.lua index 0c36c81..5d6d508 100644 --- a/code/handlers/artisthandler.lua +++ b/code/handlers/artisthandler.lua @@ -8,6 +8,7 @@ local models = autoload("models") local function Artisthandler(self) local artist_id = self.params.artist + local json = self.params.json or nil -- find the artist local artist = models.Artists:find({ id = artist_id }) @@ -28,7 +29,7 @@ local function Artisthandler(self) -- find all tracks with this artist local tracks = db.query([[ - SELECT track.name AS track_name, track.id + SELECT track.name AS track_name, track.id AS track_id, track.img_url FROM track_artists INNER JOIN tracks AS track ON (track_artists.track = track.id) @@ -43,21 +44,24 @@ local function Artisthandler(self) for i, track in ipairs(tracks) do local track_airtimes = 0 + track["last_airtime"] = "" local airtimes = db.query([[ - SELECT airtime, station AS station_id + SELECT airtime FROM airtimes WHERE track = ? - ]], track["id"]) + ]], track["track_id"]) - artist["tracks"][i]["airtimes"] = {} + -- this can probably be done in SQL directly for j, airtime in ipairs(airtimes) do - table.insert(artist["tracks"][i]["airtimes"], airtime) track_airtimes = track_airtimes + 1 total_airtimes = total_airtimes + 1 if airtime.airtime > last_airtime then last_airtime = airtime.airtime end + if airtime.airtime > track["last_airtime"] then + track["last_airtime"] = airtime.airtime + end end artist["tracks"][i]["track_airtimes"] = track_airtimes @@ -68,7 +72,7 @@ local function Artisthandler(self) INNER JOIN releases AS release ON (track_releases.release = release.id) WHERE track = ? - ]], track["id"]) + ]], track["track_id"]) artist["tracks"][i]["releases"] = {} for k, release in ipairs(releases) do @@ -79,7 +83,12 @@ local function Artisthandler(self) artist["total_airtimes"] = total_airtimes artist["last_airtime"] = last_airtime - return { json = artist } + if json then + return { json = artist } + end + + self.artist = artist + return { render = "artist" } end diff --git a/code/handlers/releasehandler.lua b/code/handlers/releasehandler.lua index 360f52d..514d571 100644 --- a/code/handlers/releasehandler.lua +++ b/code/handlers/releasehandler.lua @@ -8,6 +8,7 @@ local models = autoload("models") local function Releasehandler(self) local release_id = self.params.release + local json = self.params.json or nil local release = models.releases:find({ id = release_id }) if not release then @@ -27,7 +28,7 @@ local function Releasehandler(self) -- get all tracks on this release local tracks = db.query([[ - SELECT track.name AS track_name, track.id, artist.name AS artist_name + SELECT track.name AS track_name, track.id AS track_id, track.img_url, artist.name AS artist_name, artist.id AS artist_id FROM track_releases INNER JOIN tracks AS track ON (track_releases.track = track.id) @@ -46,28 +47,36 @@ local function Releasehandler(self) for i, track in ipairs(tracks) do local track_airtimes = 0 + track["last_airtime"] = "" local airtimes = db.query([[ SELECT airtime, station AS station_id FROM airtimes WHERE track = ? - ]], track["id"]) + ]], track["track_id"]) - release["tracks"][i]["airtimes"] = {} + -- this can probably be done in SQL directly for j, airtime in ipairs(airtimes) do - table.insert(release["tracks"][i]["airtimes"], airtime) track_airtimes = track_airtimes + 1 total_airtimes = total_airtimes + 1 if airtime.airtime > last_airtime then last_airtime = airtime.airtime end + if airtime.airtime > track["last_airtime"] then + track["last_airtime"] = airtime.airtime + end end release["tracks"][i]["track_airtimes"] = track_airtimes end release["total_airtimes"] = total_airtimes release["last_airtime"] = last_airtime - return { json = release } + if json then + return { json = release } + end + + self.release = release + return { render = "release" } end diff --git a/code/handlers/trackhandler.lua b/code/handlers/trackhandler.lua index 91063de..f23e78d 100644 --- a/code/handlers/trackhandler.lua +++ b/code/handlers/trackhandler.lua @@ -8,6 +8,7 @@ local models = autoload("models") local function Trackhandler(self) local track_id = self.params.track + local json = self.params.json or nil local track = models.Tracks:find({ id = track_id }) if not track then @@ -27,23 +28,27 @@ local function Trackhandler(self) -- find the artist for this track local artists = db.query([[ - SELECT artist.name AS artist_name + SELECT artist.name AS artist_name, artist.id AS artist_id FROM track_artists INNER JOIN - artists AS artist ON (track_artists.artist = artist.id) + artists AS artist ON (track_artists.artist = artist.id) WHERE track_artists.track = ? ]], track["id"]) track["artist"] = artists[1]["artist_name"] + track["artist_id"] = artists[1]["artist_id"] -- find all airtimes for this track local track_airtimes = 0 local last_airtime = "" local airtimes = db.query([[ - SELECT airtime, station AS station_id + SELECT airtime, station.id AS station_id, station.name AS station_name FROM airtimes - WHERE track = ? + INNER JOIN + stations AS station ON (airtimes.station = station.id) + WHERE track = ? + ORDER BY airtime DESC ]], track["id"]) track["airtimes"] = {} @@ -72,7 +77,12 @@ local function Trackhandler(self) track["releases"][i] = release end - return { json = track } + if json then + return { json = track } + end + + self.track = track + return { render = "track" } end diff --git a/code/views/artist.etlua b/code/views/artist.etlua new file mode 100644 index 0000000..b36df91 --- /dev/null +++ b/code/views/artist.etlua @@ -0,0 +1,48 @@ +<% render("views.navigation") %> + +
+ +
+ <%= artist.name %> +
+ Total airtimes: <%= artist.total_airtimes %> +
+ Last airtime: <%= artist.last_airtime %> +
+ + + <% for i, track in ipairs(artist.tracks) do %> +
+ <% if string.len(track.img_url) == 0 then %> + <% track.img_url = "blanco.png" %> + <% end %> + " rel="ia_lightbox"> + "> + +
+
+
+ "><%= track.track_name %> + <% if track.info_url then %><% if track.info_url:match("[^.]+$") == "jpg" then %>| <%= track.info_url %> + <% else %>| <%= track.info_url %><% end %> + <% end %> +
+ Track airtimes: <%= track.track_airtimes %> +
+ Last aired: <%= track.last_airtime %> +
+ Track releases: +
+ <% if track.releases then %> + <% for i, item in ipairs(track.releases) do %> + " style="color: #ffffff; text-decoration: none;"><%= item.release_name %>
+ <% if item.label then %>| <%= item.label %><% end %> + <% if item.year then %>| <%= item.year %><% end %> + <% if item.country then %>| <%= item.country %><% end %> + <% end %> + <% end %> +
+
+
+ <% end %> +
diff --git a/code/views/release.etlua b/code/views/release.etlua new file mode 100644 index 0000000..4af4975 --- /dev/null +++ b/code/views/release.etlua @@ -0,0 +1,34 @@ +<% render("views.navigation") %> + +
+
+ <%= release.name %> +
+ Total airtimes: <%= release.total_airtimes %> +
+ Last airtime: <%= release.last_airtime %> +
+ <% for i, track in ipairs(release.tracks) do %> +
+
+ <% if string.len(track.img_url) == 0 then %> + <% track.img_url = "blanco.png" %> + <% end %> + " rel="ia_lightbox"> + "> + +
+ "><%= track.artist_name %> - + "><%= track.track_name %> + <% if track.info_url then %><% if track.info_url:match("[^.]+$") == "jpg" then %>| <%= track.info_url %> + <% else %>| <%= track.info_url %><% end %> + <% end %> +
+ Track airtimes: <%= track.track_airtimes %> +
+ Last aired: <%= track.last_airtime %> +
+
+
+ <% end %> +
diff --git a/code/views/track.etlua b/code/views/track.etlua new file mode 100644 index 0000000..6da12a0 --- /dev/null +++ b/code/views/track.etlua @@ -0,0 +1,46 @@ +<% render("views.navigation") %> + +
+
+ <% if string.len(track.img_url) == 0 then %> + <% track.img_url = "blanco.png" %> + <% end %> + " rel="ia_lightbox"> + "> + +
+ +
+
+ "><%= track.artist %> - <%= track.name %> + <% if track.info_url then %><% if track.info_url:match("[^.]+$") == "jpg" then %>| <%= track.info_url %> + <% else %>| <%= track.info_url %><% end %> + <% end %> +
+
+ Total airtimes: <%= track.total_airtimes %> +
+ Last aired: <%= track.last_airtime %> +
+
+

+ + <% if track.releases then %> + <% for i, item in ipairs(track.releases) do %> + " style="color: #ffffff; text-decoration: none;"><%= item.release_name %>
+ <% if item.label then %>| <%= item.label %><% end %> + <% if item.year then %>| <%= item.year %><% end %> + <% if item.country then %>| <%= item.country %><% end %> + <% end %> + <% end %> +
+

+ <% for i, item in ipairs(track.airtimes) do %> +
+ + <%= item.airtime %> on "> <%= item.station_name %> + +
+ <% end %> +
+