rm silly error, updated /docs, util fn

This commit is contained in:
Paul Liverman III 2018-05-01 15:41:08 -07:00
parent d13071f858
commit 49d65805e9
3 changed files with 30 additions and 28 deletions

View File

@ -3,7 +3,7 @@ import assert_valid from require "lapis.validate"
import APIKeys, Users, Tasks from require "models"
import locate from require "locator"
import api_request, abort from locate "helpers.api"
import api_request, abort, assert_model from locate "helpers.api"
-- import random from locate "calc"
-- import escape_similar_to from locate "db"
@ -11,16 +11,14 @@ 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."}
{"id", is_integer: true, "Task id specified is not an integer."}
}
@task, err = Tasks\find id: @params.id, user_id: @user.id
abort 500, err if err
@task = assert_model 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, err = Tasks\find content: @params.content, user_id: @user.id
abort 500, err if err
@task = assert_model Tasks\find content: @params.content, user_id: @user.id
else
abort "Task id or content not specified."
@ -47,11 +45,10 @@ class API extends Application
{"content", exists: true, min_length: 1, "Task content not specified."}
}
task, err = Tasks\create {
task = assert_model Tasks\create {
user_id: @user.id
content: @params.content
}
abort 500, err unless task
return json: { success: true, :task }
@ -61,15 +58,13 @@ class API extends Application
[do: "/do"]: api_request =>
get_task(@)
@task, err = @task\update done: true
abort 500, err unless @task
@task = assert_model @task\update done: true
return json: { success: true, :task }
[undo: "/undo"]: api_request =>
get_task(@)
@task, err = @task\update done: false
abort 500, err unless @task
@task = assert_model @task\update done: false
return json: { success: true, :task }
@ -90,7 +85,7 @@ class API extends Application
@params.count or= 50
@params.page or= 1
@params.order or= "asc"
abort "Invalid page. (Must be a positive integer.)" if @params.page < 1
@params.page = 1 if @params.page < 1
local Paginator
if @params.done == nil
@ -107,12 +102,12 @@ class API extends Application
return json: { success: true, page: @params.page, pages: num_pages, :tasks }
[random: "/random"]: api_request =>
assert_valid @params, {
{"count", exists: true, is_integer: true, optional: true, "Count is not an integer."}
{"done", exists: true, one_of: {true, false}, optional: true, "Done is not a boolean."}
}
@params.count or= 1
@params.done = false if @params.done == nil
-- assert_valid @params, {
-- {"count", exists: true, is_integer: true, optional: true, "Count is not an integer."}
-- {"done", exists: true, one_of: {true, false}, optional: true, "Done is not a boolean."}
-- }
-- @params.count or= 1
-- @params.done = false if @params.done == nil
abort 501, "Not implemented."
@ -133,9 +128,7 @@ class API extends Application
-- abort!
[new_key: "/key/new"]: api_request =>
api_key, err = APIKeys\create(@user)
abort 500, err unless api_key
api_key = assert_model APIKeys\create(@user)
return json: { success: true, :api_key }
[delete_key: "/key/delete"]: api_request =>
@ -144,14 +137,12 @@ class API extends Application
{"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, err = APIKeys\find id: @params.id, user_id: @user.id
abort 500, err if err
@key_to_delete = assert_model 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, err = APIKeys\find key: @params.key, user_id: @user.id
abort 500, err if err
@key_to_delete = assert_model APIKeys\find key: @params.key, user_id: @user.id
else
abort 400, "No api_key specified."

View File

@ -5,9 +5,15 @@ class Docs extends Application
@name: "docs_"
[index: ""]: =>
@title = "Simplex API Docs"
@html ->
h3 "Hi there."
a href: @url_for("docs_v1"), "click here"
h2 "Select API Version"
ol ->
li ->
a href: @url_for("docs_v1"), "API v1"
text " (current, supported indefinitely*)"
p "* APIs are supported indefinitely unless an incompatible change is planned, in which case they will be available for 6 months before being retired."
[v1: "/v1"]: =>
@title = "Simplex API v1"

View File

@ -23,7 +23,12 @@ abort = (status, message) ->
else
yield_error status
assert_model = (result, err) ->
abort 500, err if err
return result
{
:api_request
:abort
:assert_model
}