works in progress API v1 and install script finishing/fixing

This commit is contained in:
Paul Liverman III 2018-04-23 20:19:34 -07:00
parent 1681487f4a
commit 1707ac7761
2 changed files with 111 additions and 10 deletions

View File

@ -2,8 +2,7 @@ import Application from require "lapis"
import json_params, capture_errors_json, yield_error from require "lapis.application"
import assert_valid from require "lapis.validate"
import APIKeys, Tasks from require "models"
-- import Keys, Tasks from require "models"
import APIKeys, Users, Tasks from require "models"
-- import locate from require "locator"
-- import random from locate "calc"
-- import escape_similar_to from locate "db"
@ -13,30 +12,131 @@ class API extends Application
@name: "api_"
@before_filter( capture_errors_json json_params =>
-- TODO implement Authorization: api_key VALUE as acceptable method to send api_key
@api_key = APIKeys\find key: @params.api_key
yield_error "api_key not specified!" unless @api_key
@user = Users\find id: @api_key.user_id
yield_error "Invalid api_key!" unless @user -- NOTE this should also delete the api_key and error (this should never happen!)
)
[new: "/new"]: =>
-- NOTE assert_valid appears to be non-functional :D
-- also, turns out it only works when I add comments because WHAT THE FUCK
-- TODO intentionally cause an error to see if this is working as intended
handle_error: (err, trace) =>
return status: 500, json: { errors: {err}, :trace } -- NOTE trace should be saved and NOT returned to the user
[new: "/new"]: capture_errors_json json_params =>
assert_valid @params, {
{"content", exists: true, min_length: 1, "Task content not specified."}
}
task, err = Tasks\create {
user_id: @api_key.user_id
user_id: @user.id
content: @params.content
}
yield_error err unless task
return json: { success: true, :task }
[do: "/do"]: capture_errors_json json_params =>
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
yield_error "Task id or content not specified."
yield_error "Invalid task specified." unless task
task, err = task\update done: true
yield_error err unless task
return json: { success: true, :task }
[undo: "/undo"]: capture_errors_json json_params =>
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
yield_error "Task id or content not specified."
yield_error "Invalid task specified." unless task
task, err = task\update done: false
yield_error err unless task
return json: { success: true, :task }
[random: "/random"]: capture_errors_json json_params =>
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
-- 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(@)
--
-- local tasks
-- if @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
-- else
-- 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
--
-- if tasks and #tasks == 1
-- return json: { success: true, task: tasks[1] }
-- else
-- return standard_err!
[list: "/list"]: capture_errors_json json_params =>
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."}
{"page", exists: true, is_integer: true, optional: true, "Page is not an integer."}
{"order", exists: true, one_of: {"asc", "desc"}, optional: true, "Invalid order. (Must be 'asc' or 'desc'.)"}
}
@params.count or= 50
@params.page or= 1
@params.order or= "asc" -- may need to be "desc", don't remember o.o
yield_error "Invalid page. (Must be a positive integer.)" if @params.page < 1
local Paginator
if @params.done == nil
Paginator = Tasks\paginated "WHERE user_id = ? ORDER BY created_at ?", @user.id, @params.order, per_page: @params.count
else
Paginator = Tasks\paginated "WHERE user_id = ? AND done = ? ORDER BY created_at ?", @user.id, @params.done, @params.order, per_page: @params.count
num_pages = Paginator\num_pages!
if num_pages < @params.page
@params.page = num_pages
tasks = Paginator\get_page(@params.page)
-- returns page in case it returned a different page than you asked for! (in the case you ask for a page beyond the end)
return json: { success: true, page: @params.page, :tasks }
-- /new { content: "string" }
-- /do { id: #
-- /undo { id: # }
-- /get { id: # }
-- /do { id: # } or content
-- /undo { id: # } or content
-- /get { id: # } or content
-- /random { count: #, done: bool } (both args optional, defaults count 1, done false)
-- /list { count: #, done: bool, page: #, order: asc/desc } (if done not specified, returns all, default count is 50, default page is 1, default order is latest first
-- /list { count: #, done: bool, page: #, order: asc/desc } (if done not specified, returns all,
-- default count is 50, default page is 1, default order is latest first
--
-- Add ability to send out subscription webhooks.

View File

@ -136,6 +136,7 @@ sudo echo "server {
proxy_pass http://127.0.0.1:$PORT
}
}" > /etc/nginx/sites-enabled/simplex-proxy.conf
sudo nginx -s reload
# Change owner, start service
sudo chown -R www-data:www-data ./