Browse Source

cleaning code, implementing API features

Paul Liverman III 6 years ago
parent
commit
f4acb99909
3 changed files with 71 additions and 46 deletions
  1. 2
    0
      app.moon
  2. 47
    43
      applications/api.moon
  3. 22
    3
      static/index.js

+ 2
- 0
app.moon View File

@@ -23,6 +23,8 @@ class Simplex extends Application
23 23
       super!
24 24
 
25 25
   [index: "/"]: =>
26
+    @title = "Simplex Task Manager"
27
+
26 28
     if @user
27 29
       @keys = APIKeys\select "WHERE user_id = ? ORDER BY id ASC", @user.id
28 30
       @tasks = Tasks\select "WHERE user_id = ? ORDER BY id ASC", @user.id

+ 47
- 43
applications/api.moon View File

@@ -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

+ 22
- 3
static/index.js View File

@@ -81,7 +81,26 @@ function delete_item(e) {
81 81
   while (!e.is("li")) {
82 82
     e = e.parent();
83 83
   }
84
-  // TODO find whether this is a task or api_key, send delete request, hide item
85
-  // success? delete item, failure? add_error to item and unhide it (how? display: block;)
86
-  e.remove();
84
+
85
+  checkbox = $("input:checkbox", e);
86
+  if (checkbox.length) {
87
+    // TODO find task id, send delete request, hide item
88
+    // success? delete item, failure? add_error to item and unhide it (how? display: block;)
89
+    return;
90
+  }
91
+
92
+  code = $("code", e);
93
+  if (code) {
94
+    e.css("display", "hidden"); // hide it
95
+    $.post("/v1/key/delete", {key: code.text()}, function(data) {
96
+      e.remove();
97
+    })
98
+      .fail(function(request) {
99
+        add_error(e);
100
+        e.css("display", "block");
101
+    });
102
+    return;
103
+  }
104
+
105
+  e.remove(); // for errors / others, client-side only
87 106
 }