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

99 lines
3.2 KiB
Plaintext
Raw 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-04-18 06:23:16 +00:00
import trim from require "lapis.util"
import process_tags from require "helpers"
2019-04-18 04:05:33 +00:00
import Tracks from require "models"
class extends lapis.Application
"/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 = Tracks\find id: tonumber @params.id
updates = {}
2019-06-13 16:11:04 +00:00
-- automatic tags
for field in *{"artist", "mood", "link", "genre"}
if @params[field]
if @params.tags
@params.tags ..= " #{field}:#{@params[field]}"
else
@params.tags = "#{field}:#{@params[field]}"
-- update all fields
2019-04-18 06:23:16 +00:00
for field in pairs Tracks.fields
if @params[field]
switch field
when "status"
2019-04-18 06:43:26 +00:00
updates.status = Tracks.statuses\for_db tonumber(@params.status) or @params.status
2019-04-18 06:23:16 +00:00
when "quality"
2019-04-18 06:43:26 +00:00
updates.quality = Tracks.qualities\for_db tonumber(@params.quality) or @params.quality
2019-04-18 06:23:16 +00:00
when "tags"
updates.tags = process_tags @params.tags
when "id"
nil -- IDs cannot be modified
else
updates[field] = trim @params[field]
2019-04-18 06:43:26 +00:00
@track\update updates
2019-04-18 06:23:16 +00:00
return redirect_to: @url_for "track", id: @track.id
else
return redirect_to: @url_for "tracks"
}
2019-04-18 05:17:04 +00:00
[tracks: "/tracks(/:order[%a])(/:asc_desc)(/:page[%d])"]: =>
2019-04-18 05:08:09 +00:00
-- validate order field
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-04-18 05:17:04 +00:00
tracks = Tracks\paginated "* ORDER BY #{@order} #{@asc_desc}", per_page: 32
-- @last_page = tracks\num_pages! -- TODO figure out why this errors
@last_page = 150
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
2019-04-18 05:08:09 +00:00
@tracks = tracks\get_page @page
return render: true
2019-04-18 06:23:16 +00:00
-- NOTE deprecated
2019-04-18 04:05:33 +00:00
[index: "/(:page[%d])"]: respond_to {
GET: =>
tracks = Tracks\paginated "* ORDER BY id", per_page: 32
@page = tonumber(@params.page) or 1
@tracks = tracks\get_page @page
render: true
POST: =>
if @params.id
track = Tracks\find id: @params.id
track\update {
artist: @params.artist
2019-04-18 06:43:26 +00:00
title: @params.title
link: @params.link
2019-04-18 04:05:33 +00:00
status: tonumber @params.status
}
else
Tracks\create {
artist: @params.artist
2019-04-18 06:43:26 +00:00
title: @params.title
link: @params.link
2019-04-18 04:05:33 +00:00
status: @params.status
}
redirect_to: @url_for "index", page: @params.page
}