diff --git a/helpers.moon b/helpers.moon index 83ddd98..f66ab51 100644 --- a/helpers.moon +++ b/helpers.moon @@ -13,38 +13,37 @@ process_tags = (tags_str) -> 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! + 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+", "_"}" + 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 - 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 + updates[field] = trim user_input[field] -create_track = (user_input) -> - if track = Tracks\create({}) - user_input.id = track.id - update_track user_input + 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 + create_track: update_track }