import trim from require "lapis.util" import Tracks from require "models" process_tags = (tags_str) -> -- split into a table to garuntee uniqueness unique = {} for tag in tags_str\gmatch "%S+" unique[tag\lower!] = true -- 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"} 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" updates.status = tonumber user_input.status when "quality" 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] if user_input.id if track = Tracks\find id: tonumber user_input.id return track\update updates else return Tracks\create updates return { :process_tags :update_track create_track: update_track }