From e75ba059ab8bd87ff95247e4d958277f50fde3cd Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Tue, 22 Oct 2024 23:39:42 +0200 Subject: grocy new product --- elisp/grocy.el | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/elisp/grocy.el b/elisp/grocy.el index 6001a70..2129113 100644 --- a/elisp/grocy.el +++ b/elisp/grocy.el @@ -9,7 +9,7 @@ ;; Version: 0.0.1 ;; Keywords: abbrev bib c calendar comm convenience data docs emulations extensions faces files frames games hardware help hypermedia i18n internal languages lisp local maint mail matching mouse multimedia news outlines processes terminals tex tools unix vc wp ;; Homepage: https://github.com/titan/grocy -;; Package-Requires: ((emacs "27.1")) +;; Package-Requires: ((emacs "29.1")) ;; ;; This file is not part of GNU Emacs. ;; @@ -21,6 +21,8 @@ (require 'url) (require 'dash) +(require 'yaml) +(require 'yaml-mode) ;; Silence byte-compiler. (defvar url-http-end-of-headers) @@ -186,6 +188,15 @@ Set METHOD to other request types and include the already encoded DATA." (json-encode) (encode-coding-string 'utf-8))))) +(defun grocy-buffer-yaml->json (buffer) + "Read the YAML on BUFFER and encode it to json." + (with-current-buffer buffer + (thread-first + (buffer-string) + (yaml-parse-string) + (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 @@ -196,5 +207,32 @@ Set METHOD to other request types and include the already encoded DATA." "PUT" (grocy-buffer-obj->json (current-buffer)))) +(defun grocy-new-product () + (interactive) + (with-current-buffer (get-buffer-create "* Grocy new product *") + (erase-buffer) + (yaml-mode) + (keymap-local-set + "C-c C-c" + (lambda () (interactive) + (grocy--request + "objects/products" + (lambda (status) + (message "%S\n%S" status (buffer-string))) + "POST" + (grocy-buffer-yaml->json (current-buffer))))) + (insert "name: \n# ") + (maphash (lambda (key val) (insert (format "%d:'%s' " key (car val)))) grocy-locations) + (insert "\nlocation_id: \n\# ") + (maphash (lambda (key val) (insert (format "%d:'%s' " key (car val)))) grocy-units) + (insert "\nqu_id_stock: 3 +qu_id_purchase: 3 +qu_id_consume: 3 +qu_id_price: 3 + +min_stock_amount: 0 +default_best_before_days: -1")) + (grocy-buffer-yaml->json (current-buffer))) + (provide 'grocy) ;;; grocy.el ends here -- cgit v1.2.3