aboutsummaryrefslogtreecommitdiffstats
path: root/elisp
diff options
context:
space:
mode:
Diffstat (limited to 'elisp')
-rw-r--r--elisp/grocy.el46
1 files changed, 19 insertions, 27 deletions
diff --git a/elisp/grocy.el b/elisp/grocy.el
index 4a9b444..49638fb 100644
--- a/elisp/grocy.el
+++ b/elisp/grocy.el
@@ -166,6 +166,18 @@ Set METHOD to other request types and include the already encoded DATA."
(message "%S" status)
(display-buffer (current-buffer))))
+(defun grocy-push-buffer-and-kill-fn (endpoint method)
+ "Function to assign to keymap that pushed to ENDPOINT by METHOD."
+ (lambda ()
+ (interactive)
+ "Push current buffer to defined endpoint."
+ (grocy--request
+ endpoint
+ #'grocy--message-error-reply
+ method
+ (grocy-buffer-obj->json (current-buffer)))
+ (kill-buffer)))
+
(defun grocy-add-product-shopping-list ()
"Add selected product to first shopping list."
(interactive)
@@ -176,14 +188,9 @@ Set METHOD to other request types and include the already encoded DATA."
(erase-buffer)
(emacs-lisp-mode)
(insert (format ";; %s\n(:product_id %d\n:list_id 1\n:product_amount 1\n;; :note \"\"\n)\n" name id))
- (keymap-local-set "C-c C-c"
- (lambda () (interactive)
- (grocy--request
- "stock/shoppinglist/add-product"
- #'grocy--message-error-reply
- "POST"
- (grocy-buffer-obj->json (current-buffer)))
- (kill-buffer)))
+ (keymap-local-set
+ "C-c C-c"
+ (grocy-push-buffer-and-kill-fn "stock/shoppinglist/add-product" "POST"))
(switch-to-buffer (current-buffer)))))
(defun grocy-update-product ()
@@ -198,10 +205,9 @@ Set METHOD to other request types and include the already encoded DATA."
(map-into 'alist)
(pp (current-buffer)))
(emacs-lisp-mode)
- (keymap-local-set "C-c C-c"
- (lambda () (interactive)
- (grocy-update-object (format "products/%d" id))
- (kill-buffer)))
+ (keymap-local-set
+ "C-c C-c"
+ (grocy-push-buffer-and-kill-fn (format "objects/products/%d" id) "PUT"))
(switch-to-buffer (current-buffer)))))
(defun grocy-buffer-obj->json (buffer)
@@ -215,14 +221,6 @@ Set METHOD to other request types and include the already encoded DATA."
(json-encode)
(encode-coding-string 'utf-8)))))
-(defun grocy-update-object (object-endpoint)
- "Update on the OBJECT-ENDPOINT with the Lisp object on buffer."
- (grocy--request
- (format "objects/%s" object-endpoint)
- #'grocy--message-error-reply
- "PUT"
- (grocy-buffer-obj->json (current-buffer))))
-
(defun grocy-new-product ()
"Add new product to the database."
(interactive)
@@ -231,13 +229,7 @@ Set METHOD to other request types and include the already encoded DATA."
(emacs-lisp-mode)
(keymap-local-set
"C-c C-c"
- (lambda () (interactive)
- (grocy--request
- "objects/products"
- #'grocy--message-error-reply
- "POST"
- (grocy-buffer-obj->json (current-buffer)))
- (kill-buffer)))
+ (grocy-push-buffer-and-kill-fn "objects/products" "POST"))
(insert "(\n:name \n;; ")
(maphash (lambda (key val) (insert (format "%d:'%s' " key (car val)))) grocy-locations)
(insert "\n:location_id \n;; ")