diff --git a/public/js/title.js b/public/js/title.js index 6f53265..4fbb428 100644 --- a/public/js/title.js +++ b/public/js/title.js @@ -259,8 +259,11 @@ const tagsComponent = () => { newTag: '', inputShown: false, add() { - this.tags.push(this.newTag); - this.newTag = ''; + const tag = this.newTag; + this.request(tag, 'PUT', () => { + this.tags.push(tag); + this.newTag = ''; + }); }, keydown(event) { if (event.key === 'Enter') @@ -268,9 +271,11 @@ const tagsComponent = () => { }, rm(event) { const tag = event.currentTarget.id.split('-')[0]; - const idx = this.tags.indexOf(tag); - if (idx < 0) return; - this.tags.splice(idx, 1); + this.request(tag, 'DELETE', () => { + const idx = this.tags.indexOf(tag); + if (idx < 0) return; + this.tags.splice(idx, 1); + }); }, toggleInput(nextTick) { this.inputShown = !this.inputShown; @@ -279,6 +284,25 @@ const tagsComponent = () => { $('#tag-input').get(0).focus(); }); } + }, + request(tag, method, cb) { + const tid = $('.upload-field').attr('data-title-id'); + const url = `${base_url}api/tags/${tid}/${encodeURIComponent(tag)}`; + $.ajax({ + url: url, + method: method, + dataType: 'json' + }) + .done(data => { + if (data.success) + cb(); + else { + alert('danger', data.error); + } + }) + .fail((jqXHR, status) => { + alert('danger', `Error: [${jqXHR.status}] ${jqXHR.statusText}`); + }); } }; }; diff --git a/src/routes/api.cr b/src/routes/api.cr index ad4f497..4f5f334 100644 --- a/src/routes/api.cr +++ b/src/routes/api.cr @@ -685,6 +685,50 @@ class APIRouter < Router end end + Koa.describe "Adds a new tag to a title" + Koa.path "tid", desc: "A title ID" + Koa.response 200, ref: "$result" + put "/api/tags/:tid/:tag" do |env| + begin + title = (@context.library.get_title env.params.url["tid"]).not_nil! + tag = env.params.url["tag"] + + title.add_tag tag + send_json env, { + "success" => true, + "error" => nil, + }.to_json + rescue e + @context.error e + send_json env, { + "success" => false, + "error" => e.message, + }.to_json + end + end + + Koa.describe "Deletes a tag from a title" + Koa.path "tid", desc: "A title ID" + Koa.response 200, ref: "$result" + delete "/api/tags/:tid/:tag" do |env| + begin + title = (@context.library.get_title env.params.url["tid"]).not_nil! + tag = env.params.url["tag"] + + title.delete_tag tag + send_json env, { + "success" => true, + "error" => nil, + }.to_json + rescue e + @context.error e + send_json env, { + "success" => false, + "error" => e.message, + }.to_json + end + end + doc = Koa.generate @@api_json = doc.to_json if doc