some more improvements; basics for testing; new endpoints with html + json output
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
local db = require("lapis.db")
|
||||
local to_json = require("lapis.util").to_json
|
||||
local autoload = require("lapis.util").autoload
|
||||
local preload = require("lapis.db.model").preload
|
||||
|
||||
local models = autoload("models")
|
||||
|
||||
|
||||
function Artisthandler(self)
|
||||
local artist_id = self.params.artist
|
||||
|
||||
local artist = models.Artists:find({ id = artist_id })
|
||||
if not artist then
|
||||
self:write({"Not Found", status = 404})
|
||||
end
|
||||
|
||||
local hidden_fields = {
|
||||
"unique_name",
|
||||
"created_at",
|
||||
"updated_at"
|
||||
}
|
||||
|
||||
for i, field in ipairs(hidden_fields) do
|
||||
artist[field] = nil
|
||||
end
|
||||
|
||||
local tracks = db.query([[
|
||||
SELECT track.name AS track_name, release.name AS release_name
|
||||
FROM track_artists
|
||||
INNER JOIN
|
||||
tracks AS track ON (track_artists.track = track.id)
|
||||
INNER JOIN
|
||||
track_releases AS track_release ON (track_release.track = track.id)
|
||||
INNER JOIN
|
||||
releases AS release ON (track_release.release = release.id)
|
||||
WHERE track_artists.artist = ?
|
||||
]], artist["id"])
|
||||
|
||||
|
||||
artist["tracks"] = tracks
|
||||
|
||||
-- local airtimes = db.query([[
|
||||
-- SELECT airtime
|
||||
-- FROM airtimes
|
||||
-- WHERE track = ?
|
||||
-- ]], track["id"])
|
||||
|
||||
-- artist["airtimes"] = {}
|
||||
-- for i, airtime in ipairs(airtimes) do
|
||||
-- artist["airtimes"][i] = airtime.airtime
|
||||
-- end
|
||||
|
||||
return { json = artist }
|
||||
|
||||
end
|
||||
|
||||
return Artisthandler
|
||||
@@ -39,32 +39,15 @@ function Splhandler(self)
|
||||
|
||||
local track = nil
|
||||
local track_name = self.params.track_name -- mandatory
|
||||
local track_info_url = nil
|
||||
if self.params.info_url then
|
||||
track_info_url = self.params.info_url
|
||||
end
|
||||
local track_img_url = nil
|
||||
if self.params.img_url then
|
||||
track_img_url = self.params.img_url
|
||||
end
|
||||
local track_info_url = self.params.info_url or nil
|
||||
local track_img_url = self.params.img_url or "blanco.png"
|
||||
|
||||
local release = nil
|
||||
local release_name = nil
|
||||
if self.params.release_name then
|
||||
release_name = self.params.release_name
|
||||
end
|
||||
local release_year = nil
|
||||
if self.params.year then
|
||||
release_year = tonumber(self.params.year)
|
||||
end
|
||||
local release_country = nil
|
||||
if self.params.country then
|
||||
release_country = self.params.country
|
||||
end
|
||||
local release_label = nil
|
||||
if self.params.label then
|
||||
release_label = self.params.label
|
||||
end
|
||||
local release_name = self.params.release_name or nil
|
||||
local release_year = tonumber(self.params.year) or nil
|
||||
|
||||
local release_country = self.params.country or nil
|
||||
local release_label = self.params.label or nil
|
||||
|
||||
local airtime = self.params.time -- mandatory
|
||||
local airdate = self.params.date -- mandatory
|
||||
@@ -172,4 +155,4 @@ function Splhandler(self)
|
||||
|
||||
end
|
||||
|
||||
return Splhandler
|
||||
return Splhandler
|
||||
|
||||
@@ -8,6 +8,7 @@ local models = autoload("models")
|
||||
|
||||
function Stationhandler(self)
|
||||
local station_id = self.params.station
|
||||
local json = self.params.json or nil
|
||||
|
||||
local station = models.Stations:find({ station = station_id })
|
||||
if not station then
|
||||
@@ -16,11 +17,8 @@ function Stationhandler(self)
|
||||
print("This is: " .. station["name"])
|
||||
end
|
||||
|
||||
local artist = nil
|
||||
local release = nil
|
||||
local return_dict = {}
|
||||
local limit = 10
|
||||
local offset = 0
|
||||
local limit = self.params.limit or 10
|
||||
local offset = self.params.offset or 0
|
||||
|
||||
-- local airtimes = models.Airtimes:paginated("where station = ? order by airtime desc", station["id"], {
|
||||
-- per_page = 10,
|
||||
@@ -31,33 +29,41 @@ function Stationhandler(self)
|
||||
-- return airtime
|
||||
-- end
|
||||
-- })
|
||||
|
||||
|
||||
-- airtimes = airtimes:get_page(1)
|
||||
|
||||
local airtimes = db.query([[
|
||||
SELECT airtime, track.name AS track, track.year, track.info_url, track.img_url,
|
||||
artist.name as artist,
|
||||
release.name AS release, release.country, release.label AS label
|
||||
self.airtimes = db.query([[
|
||||
SELECT airtime, track.id AS track_id, track.name AS track, track.year, track.info_url, track.img_url,
|
||||
artist.id AS artist_id, artist.name as artist,
|
||||
release.id AS release_id, release.name AS release, release.country, release.label AS label
|
||||
FROM airtimes
|
||||
INNER JOIN
|
||||
INNER JOIN
|
||||
tracks AS track ON (airtimes.track = track.id)
|
||||
INNER JOIN
|
||||
track_artists as track_artist ON (track_artist.track = track.id)
|
||||
INNER JOIN
|
||||
artists as artist ON (track_artist.artist = artist.id)
|
||||
INNER JOIN
|
||||
track_releases as track_release ON (track_release.track = track.id)
|
||||
INNER JOIN
|
||||
releases as release ON (track_release.release = release.id)
|
||||
INNER JOIN
|
||||
track_artists AS track_artist ON (track_artist.track = track.id)
|
||||
INNER JOIN
|
||||
artists AS artist ON (track_artist.artist = artist.id)
|
||||
INNER JOIN
|
||||
track_releases AS track_release ON (track_release.track = track.id)
|
||||
INNER JOIN
|
||||
releases AS release ON (track_release.release = release.id)
|
||||
WHERE station = ?
|
||||
ORDER BY airtime
|
||||
ORDER BY airtime
|
||||
DESC LIMIT ?
|
||||
OFFSET ?
|
||||
]], station["id"], limit, offset*limit
|
||||
)
|
||||
|
||||
return { json = airtimes }
|
||||
if json then
|
||||
return { json = self.airtimes }
|
||||
end
|
||||
|
||||
for i, item in ipairs(self.airtimes) do
|
||||
print(to_json(item))
|
||||
end
|
||||
|
||||
return { render = "station" }
|
||||
|
||||
end
|
||||
|
||||
return Stationhandler
|
||||
return Stationhandler
|
||||
|
||||
@@ -24,9 +24,6 @@ function Trackhandler(self)
|
||||
track[field] = nil
|
||||
end
|
||||
|
||||
local artists = {}
|
||||
local airtimes = nil
|
||||
|
||||
local artists = db.query([[
|
||||
SELECT artist.name AS artist_name
|
||||
FROM track_artists
|
||||
@@ -35,7 +32,7 @@ function Trackhandler(self)
|
||||
WHERE track_artists.track = ?
|
||||
]], track["id"])
|
||||
|
||||
|
||||
|
||||
track["artist"] = artists[1]["artist_name"]
|
||||
|
||||
local airtimes = db.query([[
|
||||
@@ -48,9 +45,9 @@ function Trackhandler(self)
|
||||
for i, airtime in ipairs(airtimes) do
|
||||
track["airtimes"][i] = airtime.airtime
|
||||
end
|
||||
|
||||
|
||||
return { json = track }
|
||||
|
||||
end
|
||||
|
||||
return Trackhandler
|
||||
return Trackhandler
|
||||
|
||||
Reference in New Issue
Block a user