fix #2, fix #3, add edit ability to tracks route, modularized some code

This commit is contained in:
Tangent 2019-06-14 17:35:22 -07:00
parent 9d6b5f7f5e
commit 9e43167722
7 changed files with 134 additions and 123 deletions

View File

@ -2,9 +2,8 @@ lapis = require "lapis"
console = require "lapis.console"
import respond_to from require "lapis.application"
import trim from require "lapis.util"
import escape_identifier from require "lapis.db"
import process_tags from require "helpers"
import create_track, update_track from require "helpers"
import Tracks from require "models"
@ -21,36 +20,14 @@ class extends lapis.Application
else
return redirect_to: @url_for "tracks"
POST: =>
if @track = Tracks\find id: tonumber @params.id
updates = {}
-- 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
for field in pairs Tracks.fields
if @params[field]
switch field
when "status"
updates.status = Tracks.statuses\for_db tonumber(@params.status) or @params.status
when "quality"
updates.quality = Tracks.qualities\for_db tonumber(@params.quality) or @params.quality
when "tags"
updates.tags = process_tags @params.tags
when "id"
nil -- IDs cannot be modified
else
updates[field] = trim @params[field]
@track\update updates
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 field
@order = tostring(@params.order)\lower!
unless Tracks.fields[@order]
@ -69,31 +46,15 @@ class extends lapis.Application
if @page > @last_page
return redirect_to: @url_for "tracks", order: @order, asc_desc: @asc_desc, page: @last_page
@tracks = tracks\get_page @page
@tracks = tracks\get_page(@page)
return render: true
-- NOTE deprecated
[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
[new_track: "/new"]: respond_to {
GET: => render: true
POST: =>
if @params.id
track = Tracks\find id: @params.id
track\update {
artist: @params.artist
title: @params.title
link: @params.link
status: tonumber @params.status
}
track = create_track(@params)
if track
redirect_to: @url_for "track", id: track.id
else
Tracks\create {
artist: @params.artist
title: @params.title
link: @params.link
status: @params.status
}
redirect_to: @url_for "index", page: @params.page
redirect_to: @url_for "new_track"
}

View File

@ -12,6 +12,39 @@ process_tags = (tags_str) ->
table.sort taglist, (a, b) -> a\lower! < b\lower!
return " #{table.concat taglist, " "} "
update_track = (user_input) ->
if track = Tracks\find id: tonumber @params.id
updates = {}
-- automatic tags
for field in *{"artist", "mood", "link", "genre"}
if user_input[field] and #user_input[field] > 0 -- NOTE includes fix
if user_input.tags
user_input.tags ..= " #{field}:#{user_input[field]\gsub "%s*", "_"}" -- NOTE fix!
else
user_input.tags = "#{field}:#{user_input[field]\gsub "%s*", "_"}"
-- update all fields
for field in pairs Tracks.fields
if user_input[field]
switch field
when "status"
updates.status = Tracks.statuses\for_db user_input.status
when "quality"
updates.quality = Tracks.qualities\for_db user_input.quality
when "tags"
updates.tags = process_tags user_input.tags
when "id"
nil -- IDs cannot be modified
else
updates[field] = trim user_input[field]
track\update updates
create_track = (user_input) ->
if track = Tracks\create!
user_input.id = track.id
update_track user_input
return {
:process_tags
:update_track
:create_track
}

View File

@ -15,7 +15,7 @@ class Tracks extends Model
imported: 6 -- needs to be checked out
}
@qualities: enum {
not_available: 1 -- not in library
unavailable: 1 -- not in library
lossless: 2
high: 3
acceptable: 4

View File

@ -1,61 +0,0 @@
import Widget from require "lapis.html"
import Tracks from require "models"
class extends Widget
content: =>
div ->
a href: @url_for("index", page: @page - 1), "<"
raw "&nbsp;"
a href: @url_for("index", page: @page + 1), ">"
element "table", ->
tr ->
th "Artist"
th "Title"
th "Link"
th "Status"
th "✓"
for track in *@tracks
tr ->
form {
action: @url_for "index", page: @page
method: "POST"
enctype: "multipart/form-data"
}, ->
td -> input type: "text", name: "artist", value: track.artist
td -> input type: "text", name: "title", value: track.title
td ->
if track.link and #track.link > 0
a href: track.link, "*"
else
text "+"
text " "
input type: "text", name: "link", value: track.link
td ->
element "select", name: "status", ->
for status in *Tracks.statuses
if status == Tracks.statuses[track.status]
option value: Tracks.statuses[status], selected: true, status
else
option value: Tracks.statuses[status], status
td ->
input type: "hidden", name: "id", value: track.id
input type: "submit", value: "Update"
tr ->
form {
action: @url_for "index", page: @page
method: "POST"
enctype: "multipart/form-data"
}, ->
td -> input type: "text", name: "artist", placeholder: "artist"
td -> input type: "text", name: "title", placeholder: "title"
td ->
raw "&nbsp;&nbsp;&nbsp;"
input type: "text", name: "link", placeholder: "link"
td ->
element "select", name: "status", ->
for status in *Tracks.statuses
option value: Tracks.statuses[status], status
td -> input type: "submit", value: "Submit"

54
views/new_track.moon Normal file
View File

@ -0,0 +1,54 @@
import Widget from require "lapis.html"
import trim from require "lapis.util"
import Tracks from require "models"
class extends Widget
content: =>
div ->
form {
action: @url_for "new_track"
method: "POST"
enctype: "multipart/form"
}, ->
text "Artist: "
input type: "text", name: "artist", placeholder: "Artist"
text " Title: "
input type: "text", name: "title", placeholder: "Title"
br!
text "Album: "
input type: "text", name: "album", placeholder: "Album"
text " Link: "
input type: "text", name: "link", placeholder: "Link"
br!
text "Status: "
element "select", name: "status", ->
statuses = Tracks.statuses
for status_name in *statuses
if status_name == "new"
option value: statuses[status_name], selected: true, status_name
else
option value: statuses[status_name], status_name
text " Quality: "
element "select", name: "quality", ->
qualities = Tracks.qualities
for quality_name in *qualities
if quality_name == "unavailable"
option value: qualities[quality_name], selected: true, quality_name
else
option value: qualities[quality_name], quality_name
br!
text "Genre: "
input type: "text", name: "genre", placeholder: "Genre"
text " Mood: "
input type: "text", name: "mood", placeholder: "Mood"
br!
text "Tags: "
textarea rows: 8, name: "tags", placeholder: "untagged"
br!
input type: "submit", value: "Update"

View File

@ -65,7 +65,6 @@ class extends Widget
br!
text "Tags: "
-- input type: "text", name: "tags", value: trim(@track.tags), placeholder: "untagged"
textarea rows: 8, name: "tags", placeholder: "untagged", trim(@track.tags)
br!
input type: "submit", value: "Update"

View File

@ -1,5 +1,6 @@
import Widget from require "lapis.html"
import trim from require "lapis.util"
import Tracks from require "models"
class extends Widget
@ -37,18 +38,42 @@ class extends Widget
th "Link"
th "Status"
th "Quality"
th "✓"
for track in *@tracks
tr ->
td ->
a href: @url_for("track", id: track.id), track.title
td track.artist
td track.genre
td track.mood
td track.album
td ->
if track.link and #track.link > 0
a href: track.link, track.link
td Tracks.statuses[track.status]
td Tracks.qualities[track.quality]
form {
action: @url_for "tracks", order: @order, asc_desc: @asc_desc, page: @page
method: "POST"
enctype: "multipart/form-data"
}, ->
td -> input type: "text", name: "title", value: track.title
td -> input type: "text", name: "artist", value: track.artist
td -> input type: "text", name: "genre", value: track.genre
td -> input type: "text", name: "mood", value: track.mood
td -> input type: "text", name: "album", value: track.album
td ->
if track.link and #track.link > 0
a href: track.link, "*"
else
text "+"
text " "
input type: "text", name: "link", value: track.link
td ->
element "select", name: "status", ->
for status_name in *Tracks.statuses
if status_name == Tracks.statuses[track.status]
option value: Tracks.statuses[status_name], selected: true, status_name
else
option value: Tracks.statuses[status_name], status_name
td ->
element "select", name: "quality", ->
for quality_name in *Tracks.qualities
if quality_name == Tracks.qualities[track.quality]
option value: Tracks.qualities[quality_name], selected: true, quality_name
else
option value: Tracks.qualities[quality_name], quality_name
td ->
input type: "hidden", name: "id", value: track.id
input type: "submit", value: "Update"
@menu!