|
@@ -7,6 +7,23 @@ import api_request, abort from locate "helpers.api"
|
7
|
7
|
-- import random from locate "calc"
|
8
|
8
|
-- import escape_similar_to from locate "db"
|
9
|
9
|
|
|
10
|
+get_task = =>
|
|
11
|
+ if @params.id
|
|
12
|
+ assert_valid @params, {
|
|
13
|
+ {"id", exists: true, min_length: 1, "Attempted to select by task id, but no id specified."}
|
|
14
|
+ {"id", is_integer: true, "Task id is not an integer."}
|
|
15
|
+ }
|
|
16
|
+ @task = Tasks\find id: @params.id, user_id: @user.id
|
|
17
|
+ elseif @params.content
|
|
18
|
+ assert_valid @params, {
|
|
19
|
+ {"content", exists: true, min_length: 1, "Attempted to select by task content, but no content specified."}
|
|
20
|
+ }
|
|
21
|
+ @task = Tasks\find content: @params.content, user_id: @user.id
|
|
22
|
+ else
|
|
23
|
+ abort "Task id or content not specified."
|
|
24
|
+
|
|
25
|
+ abort 404, "Invalid task specified." unless @task
|
|
26
|
+
|
10
|
27
|
class API extends Application
|
11
|
28
|
@path: "/v1"
|
12
|
29
|
@name: "api_"
|
|
@@ -16,11 +33,11 @@ class API extends Application
|
16
|
33
|
if auth = @req.headers["authorization"]
|
17
|
34
|
if auth\len! > 0
|
18
|
35
|
@params.api_key = auth
|
19
|
|
- abort "api_key not specified." unless @params.api_key
|
|
36
|
+ abort "Auth: api_key not specified." unless @params.api_key
|
20
|
37
|
@api_key = APIKeys\find key: @params.api_key
|
21
|
|
- abort "Invalid api_key" unless @api_key
|
|
38
|
+ abort "Auth: Invalid api_key." unless @api_key
|
22
|
39
|
@user = Users\find id: @api_key.user_id
|
23
|
|
- abort "Invalid api_key." unless @user -- NOTE this should also delete the api_key and error (this should never happen!)
|
|
40
|
+ abort "Auth: Invalid api_key." unless @user -- NOTE this should also delete the api_key and error (this should never happen!)
|
24
|
41
|
)
|
25
|
42
|
|
26
|
43
|
[new: "/new"]: api_request =>
|
|
@@ -37,50 +54,21 @@ class API extends Application
|
37
|
54
|
return json: { success: true, :task }
|
38
|
55
|
|
39
|
56
|
[get: "/get"]: api_request =>
|
40
|
|
- -- TODO
|
|
57
|
+ get_task(@)
|
|
58
|
+ -- return json: { success: true, :task }
|
41
|
59
|
abort 501, "Not implemented."
|
42
|
60
|
|
43
|
61
|
[do: "/do"]: api_request =>
|
44
|
|
- local task
|
45
|
|
- if @params.id
|
46
|
|
- assert_valid @params, {
|
47
|
|
- {"id", exists: true, min_length: 1, "Attempted to select by task id, but no id specified."}
|
48
|
|
- {"id", is_integer: true, "Task id is not an integer."}
|
49
|
|
- }
|
50
|
|
- task = Tasks\find id: @params.id, user_id: @user.id
|
51
|
|
- elseif @params.content
|
52
|
|
- assert_valid @params, {
|
53
|
|
- {"content", exists: true, min_length: 1, "Attempted to select by task content, but no content specified."}
|
54
|
|
- }
|
55
|
|
- task = Tasks\find content: @params.content, user_id: @user.id
|
56
|
|
- else
|
57
|
|
- abort "Task id or content not specified."
|
58
|
|
-
|
59
|
|
- abort 404, "Invalid task specified." unless task
|
60
|
|
- task, err = task\update done: true
|
61
|
|
- abort 500, err unless task
|
|
62
|
+ get_task(@)
|
|
63
|
+ @task, err = @task\update done: true
|
|
64
|
+ abort 500, err unless @task
|
62
|
65
|
|
63
|
66
|
return json: { success: true, :task }
|
64
|
67
|
|
65
|
68
|
[undo: "/undo"]: api_request =>
|
66
|
|
- local task
|
67
|
|
- if @params.id
|
68
|
|
- assert_valid @params, {
|
69
|
|
- {"id", exists: true, min_length: 1, "Attempted to select by task id, but no id specified."}
|
70
|
|
- {"id", is_integer: true, "Task id is not an integer."}
|
71
|
|
- }
|
72
|
|
- task = Tasks\find id: @params.id, user_id: @user.id
|
73
|
|
- elseif @params.content
|
74
|
|
- assert_valid @params, {
|
75
|
|
- {"content", exists: true, min_length: 1, "Attempted to select by task content, but no content specified."}
|
76
|
|
- }
|
77
|
|
- task = Tasks\find content: @params.content, user_id: @user.id
|
78
|
|
- else
|
79
|
|
- abort "Task id or content not specified."
|
80
|
|
-
|
81
|
|
- abort 404, "Invalid task specified." unless task
|
82
|
|
- task, err = task\update done: false
|
83
|
|
- abort 500, err unless task
|
|
69
|
+ get_task(@)
|
|
70
|
+ @task, err = @task\update done: false
|
|
71
|
+ abort 500, err unless @task
|
84
|
72
|
|
85
|
73
|
return json: { success: true, :task }
|
86
|
74
|
|
|
@@ -94,7 +82,6 @@ class API extends Application
|
94
|
82
|
|
95
|
83
|
abort 501, "Not implemented."
|
96
|
84
|
|
97
|
|
- -- TODO figure out how to return random selection
|
98
|
85
|
-- possibly need to store how many items each user has and use a different strategy for users with low amounts vs high amounts
|
99
|
86
|
-- key = get_key(@)
|
100
|
87
|
--
|
|
@@ -144,8 +131,25 @@ class API extends Application
|
144
|
131
|
return json: { success: true, :api_key }
|
145
|
132
|
|
146
|
133
|
[delete_key: "/key/delete"]: api_request =>
|
147
|
|
- -- TODO
|
148
|
|
- abort 501, "Not implemented."
|
|
134
|
+ if @params.id
|
|
135
|
+ assert_valid @params, {
|
|
136
|
+ {"id", exists: true, min_length: 1, "Attempted to select by API key id, but no id specified."}
|
|
137
|
+ {"id", is_integer: true, "API key id is not an integer."}
|
|
138
|
+ }
|
|
139
|
+ @key_to_delete = APIKeys\find id: @params.id, user_id: @user.id
|
|
140
|
+ elseif @params.key
|
|
141
|
+ assert_valid @params, {
|
|
142
|
+ {"key", exists: true, min_length: 32, max_length: 32, "Invalid api_key specified."}
|
|
143
|
+ }
|
|
144
|
+ @key_to_delete = APIKeys\find key: @params.key, user_id: @user.id
|
|
145
|
+ else
|
|
146
|
+ abort 400, "No api_key specified."
|
|
147
|
+
|
|
148
|
+ abort 404, "Invalid api_key specified." unless @key_to_delete
|
|
149
|
+ if @key_to_delete\delete!
|
|
150
|
+ return json: { success: true }
|
|
151
|
+ else
|
|
152
|
+ abort 500, "Error deleting api_key."
|
149
|
153
|
|
150
|
154
|
-- /new { content: "string" }
|
151
|
155
|
-- /do { id: # } or content
|