some comments + total airtimes for tracks/artist/releases

This commit is contained in:
dreamer 2020-10-16 17:34:47 +02:00
parent 9df604a203
commit 727e06851d
4 changed files with 27 additions and 2 deletions

View File

@ -9,6 +9,7 @@ local models = autoload("models")
local function Artisthandler(self)
local artist_id = self.params.artist
-- find the artist
local artist = models.Artists:find({ id = artist_id })
if not artist then
self:write({"Not Found", status = 404})
@ -25,6 +26,7 @@ local function Artisthandler(self)
artist[field] = nil
end
-- find all tracks with this artist
local tracks = db.query([[
SELECT track.name AS track_name, track.id
FROM track_artists
@ -35,7 +37,12 @@ local function Artisthandler(self)
artist["tracks"] = tracks
-- find all airtimes for each track
local total_airtimes = 0
for i, track in ipairs(tracks) do
local track_airtimes = 0
local airtimes = db.query([[
SELECT airtime, station AS station_id
FROM airtimes
@ -45,8 +52,12 @@ local function Artisthandler(self)
artist["tracks"][i]["airtimes"] = {}
for j, airtime in ipairs(airtimes) do
table.insert(artist["tracks"][i]["airtimes"], airtime)
track_airtimes = track_airtimes + 1
total_airtimes = total_airtimes + 1
end
artist["tracks"][i]["track_airtimes"] = track_airtimes
-- find all releases with this track
local releases = db.query([[
SELECT release.id AS release_id, release.name AS release_name
FROM track_releases
@ -62,6 +73,7 @@ local function Artisthandler(self)
end
end
artist["total_airtimes"] = total_airtimes
return { json = artist }

View File

@ -25,6 +25,7 @@ local function Releasehandler(self)
release[field] = nil
end
-- get all tracks on this release
local tracks = db.query([[
SELECT track.name AS track_name, track.id, artist.name AS artist_name
FROM track_releases
@ -39,7 +40,11 @@ local function Releasehandler(self)
release["tracks"] = tracks
-- find all airtimes for each track
local total_airtimes = 0
for i, track in ipairs(tracks) do
local track_airtimes = 0
local airtimes = db.query([[
SELECT airtime, station AS station_id
FROM airtimes
@ -49,8 +54,12 @@ local function Releasehandler(self)
release["tracks"][i]["airtimes"] = {}
for j, airtime in ipairs(airtimes) do
table.insert(release["tracks"][i]["airtimes"], airtime)
track_airtimes = track_airtimes + 1
total_airtimes = total_airtimes + 1
end
release["tracks"][i]["track_airtimes"] = track_airtimes
end
release["total_airtimes"] = total_airtimes
return { json = release }

View File

@ -58,7 +58,6 @@ local function Stationhandler(self)
return { json = self.airtimes }
end
self.station_id = station_id
self.offset = tonumber(offset)
self.limit = tonumber(limit)

View File

@ -25,6 +25,7 @@ local function Trackhandler(self)
track[field] = nil
end
-- find the artist for this track
local artists = db.query([[
SELECT artist.name AS artist_name
FROM track_artists
@ -33,9 +34,10 @@ local function Trackhandler(self)
WHERE track_artists.track = ?
]], track["id"])
track["artist"] = artists[1]["artist_name"]
-- find all airtimes for this track
local track_airtimes = 0
local airtimes = db.query([[
SELECT airtime, station AS station_id
FROM airtimes
@ -45,8 +47,11 @@ local function Trackhandler(self)
track["airtimes"] = {}
for i, airtime in ipairs(airtimes) do
track["airtimes"][i] = airtime
track_airtimes = track_airtimes + 1
end
track["total_airtimes"] = track_airtimes
-- find all releases for this track
local releases = db.query([[
SELECT release.id AS release_id, release.name AS release_name
FROM track_releases