added automatic tags

This commit is contained in:
Tangent 2019-06-13 09:11:04 -07:00
parent 60930e5d5c
commit 717b5b7dc0
2 changed files with 20 additions and 2 deletions

View File

@ -21,6 +21,14 @@ class extends lapis.Application
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
@ -29,7 +37,6 @@ class extends lapis.Application
when "quality"
updates.quality = Tracks.qualities\for_db tonumber(@params.quality) or @params.quality
when "tags"
-- TODO automatic tags -> artist:Artist, mood:Mood, link:Link, genre:Genre
updates.tags = process_tags @params.tags
when "id"
nil -- IDs cannot be modified
@ -41,7 +48,6 @@ class extends lapis.Application
return redirect_to: @url_for "tracks"
}
-- TODO figure out why [%u] character class does not work
[tracks: "/tracks(/:order[%a])(/:asc_desc)(/:page[%d])"]: =>
-- validate order field
@order = tostring(@params.order)\lower!

View File

@ -1,4 +1,7 @@
import create_table, rename_column, add_column, types from require "lapis.db.schema"
import trim from require "lapis.util"
import process_tags from require "helpers"
import Tracks from require "models"
{
[1]: =>
@ -23,4 +26,13 @@ import create_table, rename_column, add_column, types from require "lapis.db.sch
{ "mood", types.text null: true }
}
add_column "tracks", unpack column
[3]: =>
for track in *Tracks\select "*"
tags = ""
for field in *{"artist", "mood", "link", "genre"}
if track[field]
tags ..= " #{field}:#{track[field]}"
if #tags > 0
track.tags = process_tags trim(track.tags or "untagged") .. tags
track\update "tags"
}