Compare commits
10 Commits
aeab084ecb
...
aa98c4c478
Author | SHA1 | Date | |
---|---|---|---|
|
aa98c4c478 | ||
|
b2a31cc045 | ||
|
30f3fb516a | ||
|
47869e2047 | ||
|
203a276cdf | ||
|
083bfbb427 | ||
|
548758abf5 | ||
|
1b55d56b43 | ||
|
c3438cd38f | ||
|
3c825ad645 |
4
app.moon
4
app.moon
@ -17,8 +17,8 @@ class extends Application
|
||||
@keys = Keys\find user_id: @user.id
|
||||
@tasks = Tasks\find user_id: @user.id -- TODO convert to paginated
|
||||
|
||||
unless #@keys > 0
|
||||
table.insert @keys, Keys\create user_id: @user.id
|
||||
unless @keys and #@keys > 0
|
||||
@keys = {Keys\create user_id: @user.id}
|
||||
|
||||
return render: "index.logged_in"
|
||||
|
||||
|
@ -34,88 +34,82 @@ get_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
|
||||
[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
|
||||
}
|
||||
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 =>
|
||||
if task = get_task(@)
|
||||
task = assert_error task\update { done: true }
|
||||
return json: { success: true, :task }
|
||||
else
|
||||
return standard_err!
|
||||
|
||||
-- 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 }
|
||||
else
|
||||
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!
|
||||
[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))
|
||||
[fetch: "/fetch"]: capture_errors_json =>
|
||||
json_params =>
|
||||
if @params.id
|
||||
if task = get_task(@)
|
||||
return json: { success: true, :task }
|
||||
else
|
||||
return standard_err!
|
||||
[fetch: "/fetch"]: capture_errors_json json_params =>
|
||||
if @params.id
|
||||
if task = get_task(@)
|
||||
return json: { success: true, :task }
|
||||
else
|
||||
key = get_key(@)
|
||||
page = tonumber(@params.page) or 1
|
||||
return standard_err!
|
||||
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
|
||||
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 }
|
||||
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(@)
|
||||
[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
|
||||
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!
|
||||
if tasks and #tasks == 1
|
||||
return json: { success: true, task: tasks[1] }
|
||||
else
|
||||
return standard_err!
|
||||
|
||||
-- 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"
|
||||
[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
|
||||
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, :tasks }
|
||||
else
|
||||
return standard_err!
|
||||
if tasks and #tasks >= 1
|
||||
return json: { success: true, :tasks }
|
||||
else
|
||||
return standard_err!
|
||||
|
@ -1,4 +1,4 @@
|
||||
import create_table, types from require "lapis.db.schema"
|
||||
import create_table, types, create_index from require "lapis.db.schema"
|
||||
import make_migrations, autoload from require "locator"
|
||||
import settings from autoload "utility"
|
||||
|
||||
@ -14,7 +14,7 @@ make_migrations {
|
||||
{"updated_at", types.time}
|
||||
}
|
||||
create_table "keys", {
|
||||
{"user_id", types.foreign_key primary_key: true}
|
||||
{"user_id", types.foreign_key}
|
||||
{"uuid", types.varchar unique: true}
|
||||
|
||||
{"created_at", types.time}
|
||||
|
@ -17,6 +17,6 @@ class Keys extends Model
|
||||
@create: (values, opts) =>
|
||||
while true
|
||||
values.uuid = uuid!
|
||||
if Keys\find uuid: values.uuid
|
||||
unless Keys\find uuid: values.uuid
|
||||
break
|
||||
super values, opts
|
||||
|
@ -1,6 +1,6 @@
|
||||
worker_processes ${{NUM_WORKERS}};
|
||||
error_log stderr notice;
|
||||
daemon off;
|
||||
daemon on;
|
||||
pid logs/nginx.pid;
|
||||
|
||||
events {
|
||||
|
@ -28,7 +28,7 @@ function check(id) {
|
||||
}
|
||||
}
|
||||
|
||||
function new() {
|
||||
function new_task() {
|
||||
var text = document.getElementById("new-task");
|
||||
console.log(text); // TEMPORARY
|
||||
var xhr = request("/new");
|
||||
|
@ -6,20 +6,22 @@ class extends Widget
|
||||
script src: "/static/index.js"
|
||||
p "API Keys:"
|
||||
ul ->
|
||||
for key in *@keys
|
||||
li key.uuid
|
||||
if @keys
|
||||
for key in *@keys
|
||||
li key.uuid
|
||||
-- li ->
|
||||
-- a onclick: "new_key()"
|
||||
|
||||
p "Tasks:"
|
||||
ul ->
|
||||
for task in *@tasks
|
||||
li ->
|
||||
input type: "checkbox", id: "task-#{task.id}", onchange: "check(#{task.id})" checked: task.done
|
||||
text " #{task.text}"
|
||||
if @tasks
|
||||
for task in *@tasks
|
||||
li ->
|
||||
input type: "checkbox", id: "task-#{task.id}", onchange: "check(#{task.id});", checked: task.done
|
||||
text " #{task.text}"
|
||||
li ->
|
||||
form {
|
||||
onsubmit: "new()"
|
||||
onsubmit: "return new_task();"
|
||||
}, ->
|
||||
input type: "text", id: "new-task", placeholder: "new task"
|
||||
input type: "submit", value: "add task"
|
||||
|
Loading…
Reference in New Issue
Block a user