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

117 lines
4.4 KiB
Plaintext
Raw Permalink Normal View History

2019-04-18 04:05:33 +00:00
lapis = require "lapis"
console = require "lapis.console"
import respond_to from require "lapis.application"
2019-06-29 00:28:48 +00:00
import escape_literal, escape_identifier from require "lapis.db"
2019-06-29 00:55:21 +00:00
import escape, unescape from require "lapis.util"
import create_track, update_track from require "helpers"
2019-04-18 06:23:16 +00:00
2019-06-15 01:08:38 +00:00
import Tracks, Tags from require "models"
2019-04-18 04:05:33 +00:00
class extends lapis.Application
2019-06-13 20:52:22 +00:00
layout: "layout"
2019-04-18 04:05:33 +00:00
"/console": console.make!
2019-04-18 06:23:16 +00:00
[track: "/track/:id[%d]"]: respond_to {
GET: =>
if @track = Tracks\find id: tonumber @params.id
2019-04-18 06:43:26 +00:00
@next = @track\next!
@previous = @track\previous!
2019-04-18 06:23:16 +00:00
return render: true
else
return redirect_to: @url_for "tracks"
POST: =>
if track = update_track(@params)
2019-06-28 03:13:24 +00:00
-- return redirect_to: @url_for "track", id: track.id -- not sure what's happening to make this error out..
return redirect_to: @url_for "track", id: @params.id
2019-04-18 06:23:16 +00:00
else
return redirect_to: @url_for "tracks"
}
2019-04-18 05:17:04 +00:00
[tracks: "/tracks(/:order[%a])(/:asc_desc)(/:page[%d])"]: =>
update_track(@params) if @params.id -- update a track if needed
2019-06-15 01:08:38 +00:00
-- validate order
2019-04-18 05:32:55 +00:00
@order = tostring(@params.order)\lower!
unless Tracks.fields[@order]
@order = "id"
-- validate ascending/descending (ascending default)
2019-04-18 05:08:09 +00:00
@asc_desc = tostring(@params.asc_desc)\upper!
2019-04-18 05:32:55 +00:00
if @asc_desc != "DESC" and @asc_desc != "ASC"
@asc_desc = "ASC"
2019-04-18 05:08:09 +00:00
@page = tonumber(@params.page) or 1
2019-06-13 16:18:22 +00:00
tracks = Tracks\paginated "* ORDER BY #{escape_identifier @order} #{@asc_desc}", per_page: 32
2019-06-13 20:52:22 +00:00
@last_page = 1 + math.floor Tracks\count("true") / 32 -- tracks\num_pages! errors for an unknown reason
2019-04-18 05:08:09 +00:00
-- 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
2019-04-18 05:32:55 +00:00
@tracks = tracks\get_page(@page)
2019-04-18 05:08:09 +00:00
return render: true
[tag: "/tag/:name(/:order[%a])(/:asc_desc)(/:page[%d])"]: =>
2019-06-29 00:55:21 +00:00
update_track(@params) if @params.id -- update a track if needed
-- validate order
@order = tostring(@params.order)\lower!
unless Tags.fields[@order]
@order = "id"
-- validate ascending/descending
@asc_desc = tostring(@params.asc_desc)\upper!
if @asc_desc != "DESC" and @asc_desc != "ASC"
@asc_desc = "ASC"
@page = tonumber(@params.page) or 1
2019-06-29 00:55:21 +00:00
-- create tag pattern
tag_pattern = unescape(@params.name)
if tag_pattern != tag_pattern\lower!
return redirect_to: @url_for "tag", name: escape(tag_pattern\lower!), order: @order, asc_desc: @asc_desc, page: @page
tag_pattern = escape_literal tag_pattern
2019-06-29 02:37:35 +00:00
tag_pattern = "'% #{tag_pattern\sub 2, -2} %'"
2019-06-29 00:55:21 +00:00
2019-06-29 00:28:48 +00:00
tracks = Tracks\paginated "WHERE tags LIKE #{tag_pattern} ORDER BY #{escape_identifier @order} #{@asc_desc}", per_page: 32
@last_page = 1
2019-06-28 03:59:11 +00:00
if tag = Tags\find name: unescape(@params.name)
@last_page = 1 + math.floor tag.count / 32
-- validate page
if @page < 1
return redirect_to: @url_for "tag", name: @params.name, order: @order, asc_desc: @asc_desc, page: 1
if @page > @last_page
return redirect_to: @url_for "tag", name: @params.name, order: @order, asc_desc: @asc_desc, page: @last_page
2019-06-28 04:01:12 +00:00
@tracks = tracks\get_page(@page)
return render: true
2019-06-15 01:08:38 +00:00
[tags: "/tags(/:order[%a])(/:asc_desc)(/:page[%d])"]: =>
-- validate order
@order = tostring(@params.order)\lower!
unless Tags.fields[@order]
@order = "count"
-- validate ascending/descending
2019-06-15 01:08:38 +00:00
@asc_desc = tostring(@params.asc_desc)\upper!
if @asc_desc != "DESC" and @asc_desc != "ASC"
@asc_desc = "DESC"
2019-06-15 01:08:38 +00:00
@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
2019-06-28 03:24:40 +00:00
if @page > @last_page
2019-06-15 01:08:38 +00:00
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
2019-04-18 04:05:33 +00:00
POST: =>
track = create_track(@params)
if track
2019-06-28 02:09:19 +00:00
return redirect_to: @url_for "track", id: track.id
2019-04-18 04:05:33 +00:00
else
2019-06-28 02:09:19 +00:00
return redirect_to: @url_for "new_track"
2019-04-18 04:05:33 +00:00
}