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/helpers.moon

52 lines
1.4 KiB
Plaintext
Raw Permalink Normal View History

2019-06-28 02:49:09 +00:00
import trim from require "lapis.util"
2019-06-28 02:25:56 +00:00
import Tracks from require "models"
2019-04-18 06:23:16 +00:00
process_tags = (tags_str) ->
-- split into a table to garuntee uniqueness
unique = {}
for tag in tags_str\gmatch "%S+"
2019-06-15 00:47:18 +00:00
unique[tag\lower!] = true
2019-04-18 06:23:16 +00:00
-- place back into an array
taglist = {}
for tag in pairs unique
table.insert taglist, tag
table.sort taglist
return " #{table.concat taglist, " "} "
update_track = (user_input) ->
updates = {}
-- automatic tags
for field in *{"artist", "mood", "link", "genre"}
2019-06-28 03:09:07 +00:00
if user_input[field] and #user_input[field] > 0
if user_input.tags
user_input.tags ..= " #{field}:#{user_input[field]\gsub "%s+", "_"}"
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"
2019-06-28 03:11:00 +00:00
updates.status = tonumber user_input.status
when "quality"
2019-06-28 03:11:00 +00:00
updates.quality = tonumber 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]
2019-06-28 03:09:07 +00:00
if user_input.id
if track = Tracks\find id: tonumber user_input.id
return track\update updates
else
return Tracks\create updates
2019-04-18 06:23:16 +00:00
return {
:process_tags
:update_track
create_track: update_track
2019-04-18 06:23:16 +00:00
}