mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-03 03:15:31 -04:00
Add API endpoints for tags
This commit is contained in:
parent
0f1d1099f6
commit
b05ed57762
@ -259,8 +259,11 @@ const tagsComponent = () => {
|
|||||||
newTag: '',
|
newTag: '',
|
||||||
inputShown: false,
|
inputShown: false,
|
||||||
add() {
|
add() {
|
||||||
this.tags.push(this.newTag);
|
const tag = this.newTag;
|
||||||
this.newTag = '';
|
this.request(tag, 'PUT', () => {
|
||||||
|
this.tags.push(tag);
|
||||||
|
this.newTag = '';
|
||||||
|
});
|
||||||
},
|
},
|
||||||
keydown(event) {
|
keydown(event) {
|
||||||
if (event.key === 'Enter')
|
if (event.key === 'Enter')
|
||||||
@ -268,9 +271,11 @@ const tagsComponent = () => {
|
|||||||
},
|
},
|
||||||
rm(event) {
|
rm(event) {
|
||||||
const tag = event.currentTarget.id.split('-')[0];
|
const tag = event.currentTarget.id.split('-')[0];
|
||||||
const idx = this.tags.indexOf(tag);
|
this.request(tag, 'DELETE', () => {
|
||||||
if (idx < 0) return;
|
const idx = this.tags.indexOf(tag);
|
||||||
this.tags.splice(idx, 1);
|
if (idx < 0) return;
|
||||||
|
this.tags.splice(idx, 1);
|
||||||
|
});
|
||||||
},
|
},
|
||||||
toggleInput(nextTick) {
|
toggleInput(nextTick) {
|
||||||
this.inputShown = !this.inputShown;
|
this.inputShown = !this.inputShown;
|
||||||
@ -279,6 +284,25 @@ const tagsComponent = () => {
|
|||||||
$('#tag-input').get(0).focus();
|
$('#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}`);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -685,6 +685,50 @@ class APIRouter < Router
|
|||||||
end
|
end
|
||||||
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
|
doc = Koa.generate
|
||||||
@@api_json = doc.to_json if doc
|
@@api_json = doc.to_json if doc
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user