cleaning code, implementing API features
This commit is contained in:
parent
384d5ef828
commit
f4acb99909
2
app.moon
2
app.moon
@ -23,6 +23,8 @@ class Simplex extends Application
|
||||
super!
|
||||
|
||||
[index: "/"]: =>
|
||||
@title = "Simplex Task Manager"
|
||||
|
||||
if @user
|
||||
@keys = APIKeys\select "WHERE user_id = ? ORDER BY id ASC", @user.id
|
||||
@tasks = Tasks\select "WHERE user_id = ? ORDER BY id ASC", @user.id
|
||||
|
@ -7,6 +7,23 @@ import api_request, abort from locate "helpers.api"
|
||||
-- import random from locate "calc"
|
||||
-- import escape_similar_to from locate "db"
|
||||
|
||||
get_task = =>
|
||||
if @params.id
|
||||
assert_valid @params, {
|
||||
{"id", exists: true, min_length: 1, "Attempted to select by task id, but no id specified."}
|
||||
{"id", is_integer: true, "Task id is not an integer."}
|
||||
}
|
||||
@task = Tasks\find id: @params.id, user_id: @user.id
|
||||
elseif @params.content
|
||||
assert_valid @params, {
|
||||
{"content", exists: true, min_length: 1, "Attempted to select by task content, but no content specified."}
|
||||
}
|
||||
@task = Tasks\find content: @params.content, user_id: @user.id
|
||||
else
|
||||
abort "Task id or content not specified."
|
||||
|
||||
abort 404, "Invalid task specified." unless @task
|
||||
|
||||
class API extends Application
|
||||
@path: "/v1"
|
||||
@name: "api_"
|
||||
@ -16,11 +33,11 @@ class API extends Application
|
||||
if auth = @req.headers["authorization"]
|
||||
if auth\len! > 0
|
||||
@params.api_key = auth
|
||||
abort "api_key not specified." unless @params.api_key
|
||||
abort "Auth: api_key not specified." unless @params.api_key
|
||||
@api_key = APIKeys\find key: @params.api_key
|
||||
abort "Invalid api_key" unless @api_key
|
||||
abort "Auth: Invalid api_key." unless @api_key
|
||||
@user = Users\find id: @api_key.user_id
|
||||
abort "Invalid api_key." unless @user -- NOTE this should also delete the api_key and error (this should never happen!)
|
||||
abort "Auth: Invalid api_key." unless @user -- NOTE this should also delete the api_key and error (this should never happen!)
|
||||
)
|
||||
|
||||
[new: "/new"]: api_request =>
|
||||
@ -37,50 +54,21 @@ class API extends Application
|
||||
return json: { success: true, :task }
|
||||
|
||||
[get: "/get"]: api_request =>
|
||||
-- TODO
|
||||
get_task(@)
|
||||
-- return json: { success: true, :task }
|
||||
abort 501, "Not implemented."
|
||||
|
||||
[do: "/do"]: api_request =>
|
||||
local task
|
||||
if @params.id
|
||||
assert_valid @params, {
|
||||
{"id", exists: true, min_length: 1, "Attempted to select by task id, but no id specified."}
|
||||
{"id", is_integer: true, "Task id is not an integer."}
|
||||
}
|
||||
task = Tasks\find id: @params.id, user_id: @user.id
|
||||
elseif @params.content
|
||||
assert_valid @params, {
|
||||
{"content", exists: true, min_length: 1, "Attempted to select by task content, but no content specified."}
|
||||
}
|
||||
task = Tasks\find content: @params.content, user_id: @user.id
|
||||
else
|
||||
abort "Task id or content not specified."
|
||||
|
||||
abort 404, "Invalid task specified." unless task
|
||||
task, err = task\update done: true
|
||||
abort 500, err unless task
|
||||
get_task(@)
|
||||
@task, err = @task\update done: true
|
||||
abort 500, err unless @task
|
||||
|
||||
return json: { success: true, :task }
|
||||
|
||||
[undo: "/undo"]: api_request =>
|
||||
local task
|
||||
if @params.id
|
||||
assert_valid @params, {
|
||||
{"id", exists: true, min_length: 1, "Attempted to select by task id, but no id specified."}
|
||||
{"id", is_integer: true, "Task id is not an integer."}
|
||||
}
|
||||
task = Tasks\find id: @params.id, user_id: @user.id
|
||||
elseif @params.content
|
||||
assert_valid @params, {
|
||||
{"content", exists: true, min_length: 1, "Attempted to select by task content, but no content specified."}
|
||||
}
|
||||
task = Tasks\find content: @params.content, user_id: @user.id
|
||||
else
|
||||
abort "Task id or content not specified."
|
||||
|
||||
abort 404, "Invalid task specified." unless task
|
||||
task, err = task\update done: false
|
||||
abort 500, err unless task
|
||||
get_task(@)
|
||||
@task, err = @task\update done: false
|
||||
abort 500, err unless @task
|
||||
|
||||
return json: { success: true, :task }
|
||||
|
||||
@ -94,7 +82,6 @@ class API extends Application
|
||||
|
||||
abort 501, "Not implemented."
|
||||
|
||||
-- TODO figure out how to return random selection
|
||||
-- possibly need to store how many items each user has and use a different strategy for users with low amounts vs high amounts
|
||||
-- key = get_key(@)
|
||||
--
|
||||
@ -144,8 +131,25 @@ class API extends Application
|
||||
return json: { success: true, :api_key }
|
||||
|
||||
[delete_key: "/key/delete"]: api_request =>
|
||||
-- TODO
|
||||
abort 501, "Not implemented."
|
||||
if @params.id
|
||||
assert_valid @params, {
|
||||
{"id", exists: true, min_length: 1, "Attempted to select by API key id, but no id specified."}
|
||||
{"id", is_integer: true, "API key id is not an integer."}
|
||||
}
|
||||
@key_to_delete = APIKeys\find id: @params.id, user_id: @user.id
|
||||
elseif @params.key
|
||||
assert_valid @params, {
|
||||
{"key", exists: true, min_length: 32, max_length: 32, "Invalid api_key specified."}
|
||||
}
|
||||
@key_to_delete = APIKeys\find key: @params.key, user_id: @user.id
|
||||
else
|
||||
abort 400, "No api_key specified."
|
||||
|
||||
abort 404, "Invalid api_key specified." unless @key_to_delete
|
||||
if @key_to_delete\delete!
|
||||
return json: { success: true }
|
||||
else
|
||||
abort 500, "Error deleting api_key."
|
||||
|
||||
-- /new { content: "string" }
|
||||
-- /do { id: # } or content
|
||||
|
@ -81,7 +81,26 @@ function delete_item(e) {
|
||||
while (!e.is("li")) {
|
||||
e = e.parent();
|
||||
}
|
||||
// TODO find whether this is a task or api_key, send delete request, hide item
|
||||
|
||||
checkbox = $("input:checkbox", e);
|
||||
if (checkbox.length) {
|
||||
// TODO find task id, send delete request, hide item
|
||||
// success? delete item, failure? add_error to item and unhide it (how? display: block;)
|
||||
e.remove();
|
||||
return;
|
||||
}
|
||||
|
||||
code = $("code", e);
|
||||
if (code) {
|
||||
e.css("display", "hidden"); // hide it
|
||||
$.post("/v1/key/delete", {key: code.text()}, function(data) {
|
||||
e.remove();
|
||||
})
|
||||
.fail(function(request) {
|
||||
add_error(e);
|
||||
e.css("display", "block");
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
e.remove(); // for errors / others, client-side only
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user