From 55cbb146e64eac0d5ce5188f66551aa1a6dd9e97 Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Tue, 15 Feb 2022 11:17:02 -0600 Subject: adding oneliners --- oneliners.api.asd | 3 ++- src/main.lisp | 29 +++++++++++++++++++++++++---- src/util.lisp | 20 ++++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 src/util.lisp diff --git a/oneliners.api.asd b/oneliners.api.asd index 2091136..db307e4 100644 --- a/oneliners.api.asd +++ b/oneliners.api.asd @@ -11,7 +11,8 @@ "lambda-riffs") :components ((:module "src" :components - ((:file "main")))) + ((:file "util") + (:file "main")))) :description "" :in-order-to ((test-op (test-op "oneliners.api/tests")))) diff --git a/src/main.lisp b/src/main.lisp index ebd07c1..97e0ffc 100644 --- a/src/main.lisp +++ b/src/main.lisp @@ -289,6 +289,26 @@ (db:with-transaction () (make-instance 'api-access :contributor contributor))) +(defun make-new-oneliner (contributor plist) + (print (list :contributor contributor :plist plist)) + (with-plist + (oneliner commands brief description) plist + (print (list oneliner commands brief description)) + (unless brief + (http-err 400 "Oneliner requires a breif description")) + (unless oneliner + (http-err 400 "Oneliner cannot be blank")) + (unless commands + (http-err 400 "This oneliner does not seem to involve any commands")) + (db:with-transaction () + (make-instance 'oneliner + :created-by contributor + :description (or description "") + :commands commands + :oneliner oneliner + :brief brief)))) + + ;;; ROUTE VARIABLE AND PARAMATER PARSERS (defun an-int (string) @@ -362,7 +382,8 @@ (defendpoint* :post "/add-oneliner" () (:auth t) - ) + (make-new-oneliner (request-contributor) (lzb:request-body)) + "true") ;; (defendpoint* :get "/search" ((commands a-csl) ;; (keywords a-csl ) @@ -527,8 +548,8 @@ names. NAME must be a symbol or a string." (jonathan:*null-value* :null)) (jonathan:to-json thing))) -;; (defun request-contributor () -;; (a:when-let (access (access-by-token (lzb:request-cookie +auth-cookie-name+))) -;; (api-contributor access))) +(defun request-contributor () + (a:when-let (access (access-by-token (lzb:request-cookie +auth-cookie-name+))) + (api-contributor access))) diff --git a/src/util.lisp b/src/util.lisp new file mode 100644 index 0000000..2fc079b --- /dev/null +++ b/src/util.lisp @@ -0,0 +1,20 @@ +;;;; util.lisp -- some utilities + +(in-package :oneliners.api) + +(defun plist-find (indicator plist &key (test 'eq) (key 'identity)) + (loop for (ind val . more) on plist by #'cddr + when (funcall test indicator (funcall key ind)) + return val)) + +(defmacro with-plist ((&rest keys) plist &rest body) + (let ((the-plist (gensym))) + `(let ((,the-plist ,plist)) + (let + ,(loop for key in keys + collect `(,key (plist-find (symbol-name ',key) + ,the-plist + :test #'string-equal + :key #'symbol-name))) + ,@body)))) + -- cgit v1.2.3