Skip to content

Commit

Permalink
users can edit tags on their own submissions
Browse files Browse the repository at this point in the history
  • Loading branch information
TangentFoxy committed Feb 10, 2018
1 parent 174d0b1 commit c5e0c3b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
38 changes: 19 additions & 19 deletions applications/ksp_crafts.moon
Expand Up @@ -98,6 +98,25 @@ class KSPCraftsApp extends lapis.Application
if @params.mods_used and @params.mods_used\len! > 0 and @params.mods_used != craft.mods_used
fields.mods_used = @params.mods_used

-- handle tags
if @params.tags
oldTags = CraftTags\hash craft_id: craft.id
newTags = invert split @params.tags
addedTags, removedTags = {}, {}
for tag in pairs newTags
unless oldTags[tag]
addedTags[tag] = true
for tag in pairs oldTags
unless newTags[tag]
removedTags[tag] = true
for name in pairs addedTags
tag = Tags\find(:name) or Tags\create(:name)
CraftTags\create tag_id: tag.id, craft_id: craft.id
for name in pairs removedTags
craftTag = CraftTags\find craft_id: craft.id, tag_id: (Tags\find(:name)).id
craftTag\delete!
-- lack of error checking :/

if user.admin
-- status, episode, notes, creator, user_id
if @params.status and @params.status\len! > 0 and @params.status != craft.status
Expand All @@ -111,25 +130,6 @@ class KSPCraftsApp extends lapis.Application
if @params.user_id and @params.user_id\len! > 0 and @params.user_id != craft.user_id
fields.user_id = tonumber @params.user_id

-- handle tags
if @params.tags
oldTags = CraftTags\hash craft_id: craft.id
newTags = invert split @params.tags
addedTags, removedTags = {}, {}
for tag in pairs newTags
unless oldTags[tag]
addedTags[tag] = true
for tag in pairs oldTags
unless newTags[tag]
removedTags[tag] = true
for name in pairs addedTags
tag = Tags\find(:name) or Tags\create(:name)
CraftTags\create tag_id: tag.id, craft_id: craft.id
for name in pairs removedTags
craftTag = CraftTags\find craft_id: craft.id, tag_id: (Tags\find(:name)).id
craftTag\delete!
-- lack of error checking :/

if @params.delete
if craft\delete!
@session.info = "Craft deleted."
Expand Down
2 changes: 2 additions & 0 deletions views/ksp/crafts_view.moon
Expand Up @@ -67,6 +67,8 @@ class KSPCraftsView extends Widget
input class: "input", type: "text", name: "ksp_version", placeholder: "KSP Version", value: @craft.ksp_version
div class: "control is-expanded", ->
input class: "input", type: "text", name: "mods_used", placeholder: "Mods Used", value: @craft.mods_used
div class: "control is-expanded", ->
input class: "input", type: "text", name: "tags", placeholder: "tags", value: CraftTags\to_string craft_id: @craft.id

div class: "control", ->
div class: "buttons is-centered", ->
Expand Down

0 comments on commit c5e0c3b

Please sign in to comment.