Ver Fonte

Delete event API

Innocenty Enikeew há 11 anos atrás
pai
commit
0282d5b887
2 ficheiros alterados com 45 adições e 25 exclusões
  1. 5 1
      src/utils.lisp
  2. 40 24
      src/web.lisp

+ 5 - 1
src/utils.lisp

@@ -73,7 +73,11 @@
     result))
 
 (defmethod yason:encode ((object document) &optional (stream *standard-output*))
-  (yason:encode (cl-mongo::elements object) stream))
+  (let ((hash (cl-mongo::elements object)))
+    (unless (cl-mongo::_local object)
+      (setf (gethash "_id" hash)
+            (crypto:byte-array-to-hex-string (doc-id object))))
+    (yason:encode hash stream)))
 
 (defmethod yason:encode ((object cl-mongo::bson-time) &optional (stream *standard-output*))
   (yason:encode (cl-mongo::raw object) stream))

+ 40 - 24
src/web.lisp

@@ -23,29 +23,29 @@
   (restas:with-context context
     (cl-mongo:mongo :host "10.8.0.6")
     (cl-mongo:db.use "timeline")
-    (when *run-cron*
-      (restas:context-add-variable
-       context '*import-events-timer*
-       (clon:schedule-function #'import-events
-                               (clon:make-scheduler (clon:make-typed-cron-schedule
-                                                     :minute 10 :hour 0))
-                               :name "import-events"
-                               :thread t))
-      (restas:context-add-variable
-       context '*import-finance-timer*
-       (clon:schedule-function #'import-finance
-                               (clon:make-scheduler (clon:make-typed-cron-schedule
-                                                     :minute 0 :hour 7))
-                               :name "import-finance"
-                               :thread t)))))
+    (restas:context-add-variable context '*import-places-timer*)
+    (restas:context-add-variable context '*import-finance-timer*)
+    (restas:with-context context
+      (when *run-cron*
+        (setf *import-places-timer*
+              (clon:schedule-function #'import-events
+                                      (clon:make-scheduler (clon:make-typed-cron-schedule
+                                                            :minute 10 :hour 0))
+                                      :name "import-places"
+                                      :thread t))
+        (setf *import-finance-timer*
+              (clon:schedule-function #'import-finance
+                                      (clon:make-scheduler (clon:make-typed-cron-schedule
+                                                            :minute 0 :hour 7))
+                                      :name "import-finance"
+                                      :thread t))))))
 
 (defmethod restas:finalize-module-instance :after ((module (eql #.*package*)) context)
-  (let ((events-timer (restas:context-symbol-value context '*import-events-timer*))
-        (finance-timer (restas:context-symbol-value context '*import-finance-timer*)))
-    (when events-timer
-      (trivial-timers::unschedule-timer events-timer))
-    (when finance-timer
-      (trivial-timers::unschedule-timer finance-timer))))
+  (restas:with-context context
+    (when *import-places-timer*
+      (trivial-timers::unschedule-timer *import-places-timer*))
+    (when *import-finance-timer*
+      (trivial-timers::unschedule-timer *import-finance-timer*))))
 
 ;; Static file path
 (defparameter *resources*
@@ -190,6 +190,13 @@
         (cl-mongo:db.sort "events" ($between "ts" start end) :field "ts" :limit 2000))
        stream))))
 
+(restas:define-route api/events/delete ("api/events/:id"
+                                        :method :DELETE
+                                        :content-type "application/json")
+  (cl-mongo:db.delete "events" (cl-mongo:$ "_id" (cl-mongo::make-bson-oid
+                                                  :oid (crypto:hex-string-to-byte-array id))))
+  "OK")
+
 (defparameter +timeliner.css+
   (css-lite:css
     (("body") (:padding-top "70px" :padding-bottom "30px"))
@@ -213,7 +220,7 @@
            *models (create)
            *collections (create)
            *views (create)
-           dispatcher (chain _ (clone (@ *backbone *events)))
+           dispatcher (! _ (clone (@ *backbone *events)))
            :start (lambda (date)
                     (var router (new ((@ *timeliner *router))))
                     (var events (new ((@ *timeliner *collections *events))))
@@ -281,6 +288,9 @@
                class-name "list-group-item"
                :events (create
                         "click" "select")
+               initialize (lambda ()
+                            (! this (listen-to (@ this model) "change" (@ this render)))
+                            (! this (listen-to (@ this model) "destroy" (@ this remove))))
                icon-class (lambda ()
                             (case (! this model (get :type))
                               (:place "glyphicon glyphicon-map-marker")
@@ -370,6 +380,12 @@
                                                           (@ this events-layer))))))
                           (! this (listen-to (@ this collection) "reset"
                                              (@ this render)))
+                          (! this (listen-to (@ this collection) "change"
+                                             (@ this render)))
+                          (! this (listen-to (@ this collection) "add"
+                                             (@ this render)))
+                          (! this (listen-to (@ this collection) "remove"
+                                             (@ this render)))
                           (! this (listen-to (@ *timeliner dispatcher) "event:selected"
                                              (@ this highlight))))
              render (lambda ()
@@ -388,7 +404,7 @@
                                                        :color "#000"
                                                        fill-color "#F00"
                                                        fill-opacity 1))))
-                                   (! e (set "_layer" marker))
+                                   (setf (@ e "_layer") marker)
                                    (! this path-layer (add-lat-lng
                                                        (! marker (get-lat-lng))))
                                    (! this events-layer (add-layer marker))))
@@ -403,7 +419,7 @@
                          200))
                       this)
              highlight (lambda (event)
-                         (var layer (! event (get "_layer")))
+                         (var layer (@ event "_layer"))
                          (! this events-layer
                             (set-style (create fill-color "#F00")))
                          (if layer