Add API endpoints for tags

This commit is contained in:
Alex Ling 2020-12-30 07:25:13 +00:00
parent 0f1d1099f6
commit b05ed57762
2 changed files with 73 additions and 5 deletions

View File

@ -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}`);
});
}
};
};

View File

@ -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