This repository has been archived on 2024-09-20. You can view files and clone it, but cannot push or open issues or pull requests.
musicapp/app.moon
2019-06-27 19:09:19 -07:00

88 lines
3.0 KiB
Plaintext

lapis = require "lapis"
console = require "lapis.console"
import respond_to from require "lapis.application"
import escape_identifier from require "lapis.db"
import create_track, update_track from require "helpers"
import Tracks, Tags from require "models"
class extends lapis.Application
layout: "layout"
"/console": console.make!
[track: "/track/:id[%d]"]: respond_to {
GET: =>
if @track = Tracks\find id: tonumber @params.id
@next = @track\next!
@previous = @track\previous!
return render: true
else
return redirect_to: @url_for "tracks"
POST: =>
if track = update_track(@params)
return redirect_to: @url_for "track", id: @track.id
else
return redirect_to: @url_for "tracks"
}
[tracks: "/tracks(/:order[%a])(/:asc_desc)(/:page[%d])"]: =>
update_track(@params) if @params.id -- update a track if needed
-- validate order
@order = tostring(@params.order)\lower!
unless Tracks.fields[@order]
@order = "id"
-- validate ascending/descending (ascending default)
@asc_desc = tostring(@params.asc_desc)\upper!
if @asc_desc != "DESC" and @asc_desc != "ASC"
@asc_desc = "ASC"
@page = tonumber(@params.page) or 1
tracks = Tracks\paginated "* ORDER BY #{escape_identifier @order} #{@asc_desc}", per_page: 32
@last_page = 1 + math.floor Tracks\count("true") / 32 -- tracks\num_pages! errors for an unknown reason
-- validate page
if @page < 1
return redirect_to: @url_for "tracks", order: @order, asc_desc: @asc_desc, page: 1
if @page > @last_page
return redirect_to: @url_for "tracks", order: @order, asc_desc: @asc_desc, page: @last_page
@tracks = tracks\get_page(@page)
return render: true
[tag: "/tag/:name(/:asc_desc)(/:page[%d])"]: =>
-- SHOULD GRAB A GROUP OF TRACKS AND PAGINATE THEM
-- REPURPOSE THE TRACKS VIEW TO BE USABLE ON ANY PAGE
@write "Haven't made this yet!"
[tags: "/tags(/:order[%a])(/:asc_desc)(/:page[%d])"]: =>
-- validate order
@order = tostring(@params.order)\lower!
unless Tags.fields[@order]
@order = "id"
-- validate ascending/descending (ascending default)
@asc_desc = tostring(@params.asc_desc)\upper!
if @asc_desc != "DESC" and @asc_desc != "ASC"
@asc_desc = "ASC"
@page = tonumber(@params.page) or 1
tags = Tags\paginated "* ORDER BY #{escape_identifier @order} #{@asc_desc}", per_page: 32
@last_page = 1 + math.floor Tags\count("true") / 32 -- copying from tracks since num_pages wasn't working then
-- validate page
if @page < 1
return redirect_to: @url_for "tags", order: @order, asc_desc: @asc_desc, page: 1
else
return redirect_to: @url_for "tags", order: @order, asc_desc: @asc_desc, page: @last_page
@tags = tags\get_page(@page)
return render: true
[new_track: "/new"]: respond_to {
GET: => render: true
POST: =>
track = create_track(@params)
if track
return redirect_to: @url_for "track", id: track.id
else
return redirect_to: @url_for "new_track"
}