116 lines
3.8 KiB
Plaintext
116 lines
3.8 KiB
Plaintext
|
import Application from require "lapis"
|
||
|
import json_params, capture_errors_json, yield_error, assert_error from require "lapis.application"
|
||
|
import assert_valid from require "lapis.validate"
|
||
|
|
||
|
import Keys, Tasks from require "models"
|
||
|
import locate from require "locator"
|
||
|
import random from locate "calc"
|
||
|
import escape_similar_to from locate "db"
|
||
|
|
||
|
get_key = =>
|
||
|
yield_error "api_key not specified." unless @params.api_key and @params.api_key\len! > 0
|
||
|
if key = Keys\find uuid: @params.api_key
|
||
|
return key
|
||
|
else
|
||
|
yield_error "Invalid api_key."
|
||
|
|
||
|
get_task = =>
|
||
|
assert_valid @params, {
|
||
|
{"api_key", exists: true, "api_key not specified."}
|
||
|
{"id", exists: true, "Task id not specified."}
|
||
|
{"id", is_integer: true, "Task id is invalid."}
|
||
|
}
|
||
|
|
||
|
key = Keys\find uuid: @params.api_key
|
||
|
yield_error "Invalid api_key." unless key
|
||
|
|
||
|
task = Tasks\find id: @params.id
|
||
|
yield_error "Invalid task id." unless task
|
||
|
|
||
|
unless task.user_id == key.user_id
|
||
|
yield_error "Invalid task id."
|
||
|
|
||
|
return task
|
||
|
|
||
|
class extends Application
|
||
|
-- api_key AND text
|
||
|
[new: "/new"]: capture_errors_json =>
|
||
|
json_params =>
|
||
|
key = get_key(@)
|
||
|
yield_error "Task text not specified." unless @params.text and @params.text\len! > 0
|
||
|
|
||
|
task = assert_error Tasks\create {
|
||
|
user_id: key.user_id
|
||
|
text: @params.text
|
||
|
}
|
||
|
|
||
|
return json: { success: true, :task }
|
||
|
|
||
|
-- api_key AND id
|
||
|
[do: "/do"]: capture_errors_json =>
|
||
|
json_params =>
|
||
|
task = get_task(@)
|
||
|
task = assert_error task\update { done: true }
|
||
|
return json: { success: true, :task }
|
||
|
|
||
|
-- api_key AND id
|
||
|
[undo: "/undo"]: capture_errors_json =>
|
||
|
json_params =>
|
||
|
task = get_task(@)
|
||
|
task = assert_error task\update { done: false }
|
||
|
return json: { success: true, :task }
|
||
|
|
||
|
-- api_key AND (id OR (done true/false/nil AND/OR page))
|
||
|
[fetch: "/fetch"]: capture_errors_json =>
|
||
|
json_params =>
|
||
|
if @params.id
|
||
|
task = get_task(@)
|
||
|
return json: { success: true, :task }
|
||
|
else
|
||
|
key = get_key(@)
|
||
|
page = tonumber(@params.page) or 1
|
||
|
|
||
|
local paginator
|
||
|
if @params.done != nil
|
||
|
paginator = Tasks\paginated "WHERE user_id = ? AND done = ? ORDER BY id ASC", key.user_id, @params.done, per_page: 50
|
||
|
else
|
||
|
paginator = Tasks\paginated "WHERE user_id = ? ORDER BY id ASC", key.user_id, per_page: 50
|
||
|
|
||
|
tasks = paginator\get_page page
|
||
|
return json: { success: true, :tasks }
|
||
|
|
||
|
-- api_key AND done true/false/nil
|
||
|
[random: "/fetch/random"]: capture_errors_json =>
|
||
|
json_params =>
|
||
|
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 status: 500, json: { errors: "No task matches query." }
|
||
|
|
||
|
-- api_key AND done true/false/nil AND/OR case true
|
||
|
[search: "/search"]: capture_errors_json =>
|
||
|
json_params =>
|
||
|
key = get_key(@)
|
||
|
like = @params.case and "ILIKE" or "LIKE"
|
||
|
|
||
|
local tasks
|
||
|
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
|
||
|
else
|
||
|
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
|
||
|
return json: { success: true, task: tasks[1] }
|
||
|
else
|
||
|
return status: 500, json: { errors: "No task matches query." }
|