todo api done

This commit is contained in:
Paul Liverman III 2018-03-13 02:15:38 -07:00
parent a647b8f501
commit 7c6f7dab03
9 changed files with 305 additions and 0 deletions

115
app.moon Normal file
View File

@ -0,0 +1,115 @@
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." }

1
locator.moon Normal file
View File

@ -0,0 +1 @@
return require "locator.init"

25
locator_config.moon Normal file
View File

@ -0,0 +1,25 @@
{
{
path: "applications.users"
remote: {
name: "users"
fetch: "https://github.com/lazuscripts/users"
}
}
{
path: "utility"
remote: {
name: "utility"
fetch: "https://github.com/lazuscripts/utility"
}
}
{
path: "locator"
remote: {
name: "locator"
fetch: "https://github.com/lazuscripts/locator"
}
}
}

23
migrations.moon Normal file
View File

@ -0,0 +1,23 @@
import create_table, types from require "lapis.db.schema"
import make_migrations, autoload from require "locator"
import settings from autoload "utility"
make_migrations {
[1520941132]: =>
create_table "tasks", {
{"id", types.serial primary_key: true}
{"user_id", types.foreign_key}
{"text", types.text}
{"done", types.boolean default: false}
{"created_at", types.time}
{"updated_at", types.time}
}
create_table "keys", {
{"user_id", types.foreign_key primary_key: true}
{"uuid", types.varchar unique: true}
{"created_at", types.time}
{"updated_at", types.time}
}
}

81
mime.types Normal file
View File

@ -0,0 +1,81 @@
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/x-lua lua;
application/x-moonscript moon;
application/x-javascript js;
application/atom+xml atom;
application/rss+xml rss;
text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc;
image/png png;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
image/svg+xml svg svgz;
image/webp webp;
application/java-archive jar war ear;
application/mac-binhex40 hqx;
application/msword doc;
application/pdf pdf;
application/postscript ps eps ai;
application/rtf rtf;
application/vnd.ms-excel xls;
application/vnd.ms-powerpoint ppt;
application/vnd.wap.wmlc wmlc;
application/vnd.google-earth.kml+xml kml;
application/vnd.google-earth.kmz kmz;
application/x-7z-compressed 7z;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/xhtml+xml xhtml;
application/zip zip;
application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream eot;
application/octet-stream iso img;
application/octet-stream msi msp msm;
audio/midi mid midi kar;
audio/mpeg mp3;
audio/ogg ogg;
audio/x-m4a m4a;
audio/x-realaudio ra;
video/3gpp 3gpp 3gp;
video/mp4 mp4;
video/mpeg mpeg mpg;
video/quicktime mov;
video/webm webm;
video/x-flv flv;
video/x-m4v m4v;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;
}

2
models.moon Normal file
View File

@ -0,0 +1,2 @@
import autoload from require "locator"
return autoload "models"

22
models/Keys.moon Normal file
View File

@ -0,0 +1,22 @@
import Model from require "lapis.db.model"
import autoload, locate from require "locator"
import settings from autoload "utility"
import random from locate "calc"
uuid = ->
fn = ->
r = random(16) - 1
r = (x == "x") and (r + 1) or (r % 4) + 9
return ("0123456789abcdef")\sub r, r
return ("xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx")\gsub "[xy]", fn
class Keys extends Model
@primary_key: "user_id"
@timestamp: true
@create: (values, opts) =>
while true
values.uuid = uuid!
if Keys\find uuid: values.uuid
break
super values, opts

4
models/Tasks.moon Normal file
View File

@ -0,0 +1,4 @@
import Model from require "lapis.db.model"
class Tasks extends Model
@timestamp: true

32
nginx.conf Normal file
View File

@ -0,0 +1,32 @@
worker_processes ${{NUM_WORKERS}};
error_log stderr notice;
daemon off;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
server {
listen ${{PORT}};
lua_code_cache ${{CODE_CACHE}};
location / {
default_type text/html;
content_by_lua '
require("lapis").serve("app")
';
}
location /static/ {
alias static/;
}
location /favicon.ico {
alias static/favicon.ico;
}
}
}