correct api definition errors

This commit is contained in:
Paul Liverman III 2018-03-16 17:15:38 -07:00
parent b2a31cc045
commit aa98c4c478

View File

@ -34,88 +34,82 @@ get_task = =>
class extends Application class extends Application
-- api_key AND text -- api_key AND text
[new: "/new"]: capture_errors_json => [new: "/new"]: capture_errors_json json_params =>
json_params => key = get_key(@)
key = get_key(@) yield_error "Task text not specified." unless @params.text and @params.text\len! > 0
yield_error "Task text not specified." unless @params.text and @params.text\len! > 0
task = assert_error Tasks\create { task = assert_error Tasks\create {
user_id: key.user_id user_id: key.user_id
text: @params.text text: @params.text
} }
return json: { success: true, :task }
-- api_key AND id
[do: "/do"]: capture_errors_json json_params =>
if task = get_task(@)
task = assert_error task\update { done: true }
return json: { success: true, :task } return json: { success: true, :task }
else
return standard_err!
-- api_key AND id -- api_key AND id
[do: "/do"]: capture_errors_json => [undo: "/undo"]: capture_errors_json json_params =>
json_params => if task = get_task(@)
if task = get_task(@) task = assert_error task\update { done: false }
task = assert_error task\update { done: true } return json: { success: true, :task }
return json: { success: true, :task } else
else return standard_err!
return standard_err!
-- api_key AND id
[undo: "/undo"]: capture_errors_json =>
json_params =>
if task = get_task(@)
task = assert_error task\update { done: false }
return json: { success: true, :task }
else
return standard_err!
-- api_key AND (id OR (done true/false/nil AND/OR page)) -- api_key AND (id OR (done true/false/nil AND/OR page))
[fetch: "/fetch"]: capture_errors_json => [fetch: "/fetch"]: capture_errors_json json_params =>
json_params => if @params.id
if @params.id if task = get_task(@)
if task = get_task(@) return json: { success: true, :task }
return json: { success: true, :task }
else
return standard_err!
else else
key = get_key(@) return standard_err!
page = tonumber(@params.page) or 1 else
key = get_key(@)
page = tonumber(@params.page) or 1
local paginator local paginator
if @params.done != nil if @params.done != nil
paginator = Tasks\paginated "WHERE user_id = ? AND done = ? ORDER BY id ASC", key.user_id, @params.done, per_page: 50 paginator = Tasks\paginated "WHERE user_id = ? AND done = ? ORDER BY id ASC", key.user_id, @params.done, per_page: 50
else else
paginator = Tasks\paginated "WHERE user_id = ? ORDER BY id ASC", key.user_id, per_page: 50 paginator = Tasks\paginated "WHERE user_id = ? ORDER BY id ASC", key.user_id, per_page: 50
tasks = paginator\get_page page tasks = paginator\get_page page
return json: { success: true, :tasks } return json: { success: true, :tasks }
-- api_key AND done true/false/nil -- api_key AND done true/false/nil
[random: "/fetch/random"]: capture_errors_json => [random: "/fetch/random"]: capture_errors_json json_params =>
json_params => key = get_key(@)
key = get_key(@)
local tasks local tasks
if @params.done if @params.done
offset = random Tasks\count "user_id = ? AND done = ? ORDER BY id ASC", key.user_id, @params.done offset = random Tasks\count "user_id = ? AND done = ? ORDER BY id ASC", key.user_id, @params.done
tasks = Tasks\select "WHERE user_id = ? AND done = ? ORDER BY id ASC OFFSET ? LIMIT 1", key.user_id, @params.done, offset tasks = Tasks\select "WHERE user_id = ? AND done = ? ORDER BY id ASC OFFSET ? LIMIT 1", key.user_id, @params.done, offset
else else
offset = random Tasks\count "user_id = ? ORDER BY id ASC", key.user_id offset = random Tasks\count "user_id = ? ORDER BY id ASC", key.user_id
tasks = Tasks\select "WHERE user_id = ? ORDER BY id ASC OFFSET ? LIMIT 1", key.user_id, offset tasks = Tasks\select "WHERE user_id = ? ORDER BY id ASC OFFSET ? LIMIT 1", key.user_id, offset
if tasks and #tasks == 1 if tasks and #tasks == 1
return json: { success: true, task: tasks[1] } return json: { success: true, task: tasks[1] }
else else
return standard_err! return standard_err!
-- api_key AND done true/false/nil AND/OR case true -- api_key AND done true/false/nil AND/OR case true
[search: "/search"]: capture_errors_json => [search: "/search"]: capture_errors_json json_params =>
json_params => key = get_key(@)
key = get_key(@) like = @params.case and "ILIKE" or "LIKE"
like = @params.case and "ILIKE" or "LIKE"
local tasks local tasks
if @params.done if @params.done
tasks = Tasks\select "WHERE user_id = ? AND done = ? AND text #{like} ? ORDER BY id ASC", key.user_id, @params.done, escape_similar_to @params.text tasks = Tasks\select "WHERE user_id = ? AND done = ? AND text #{like} ? ORDER BY id ASC", key.user_id, @params.done, escape_similar_to @params.text
else else
tasks = Tasks\select "WHERE user_id = ? AND text #{like} ? ORDER BY id ASC", key.user_id, escape_similar_to @params.text tasks = Tasks\select "WHERE user_id = ? AND text #{like} ? ORDER BY id ASC", key.user_id, escape_similar_to @params.text
if tasks and #tasks >= 1 if tasks and #tasks >= 1
return json: { success: true, :tasks } return json: { success: true, :tasks }
else else
return standard_err! return standard_err!