From 2f6af2841b1064b0bf97699f648f9e59a344908e Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Mon, 14 Feb 2022 11:26:41 -0600 Subject: parsers section --- src/main.lisp | 112 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 58 insertions(+), 54 deletions(-) (limited to 'src/main.lisp') diff --git a/src/main.lisp b/src/main.lisp index ddfcc20..0dcc5cd 100644 --- a/src/main.lisp +++ b/src/main.lisp @@ -258,57 +258,66 @@ ;; presently if the token merely exists then that's good enough. (request-contributor)) -;;; ENDPOINT DEFINITIONS +;;; ROUTE VARIABLE AND PARAMATER PARSERS -(defendpoint* :get "/search" () () - "A search endpoint returning a JSON encoded array of Oneliner Entries. +(defun an-int (string) + "An Integer") + +(defun a-csl (s) + "A list of strings separated by commas. e.g. \"foo,bar,goo\"" + (mapcar #'str:trim (str:split "," s))) + +(defun a-boolean (s) + "Either \"true\" or \"false\"/" + (cond ((string-equal s "true") t) + ((string-equal s "false") nil) + (t (error "String ~s is neither 'true' nor 'false'" s)))) + +(defun a-page-key (key) + "A page key" + key) -/search accepts the following query parameters: +;;; ENDPOINT DEFINITIONS -- commands : A comma-separated list of commands. E.g. `'ls,grep'` -- keywords : A comma-separated list of words that may appear in the title or description, e.g. `'foo,bar,goo,zar,moo_blar' -- limit : An integer, limiting the number of results returned. Defaults to 10. -- nextpage : true or false; requests that the query be accompanied by a nextpage key -- page : a nextpage token that will continue from a previous search. These expire after 10 minutes. -- notFlagged : true or false; defaults to false. true filters out oneliners that have been flagged. -- onlyAudited : true or false; defaults to false. true filters out oneliners that have not been audited. +(defendpoint* :get "/search" ((commands a-csl) + (keywords a-cls) + (limit an-int) + (page a-page-key) + (nextpage a-boolean) + (notflagged a-boolean) + (onlyaudited a-boolean)) + () + "A search endpoint returning a JSON encoded array of Oneliner Entries. **Note**: either command or keywords are required. " - (lzb:map-parameters - ((commands parse-cls) - (keywords parse-cls) - (limit parse-integer) - (nextpage true-or-false) - (page identity) - (notflagged true-or-false) - (onlyaudited true-or-false)) - (cond - ;; if page is incluced, ignore everything, fetch the next page, - ;; and return it. - (page - (to-json (fetch-next-page page))) - - ;; otherwise one of command keywords are required - ((or commands keywords) - (let* ((limit - (or limit 10)) ; default-limit, should probably be configurable. - (results - (query-oneliners :commands commands - :keywords keywords - :notflagged notflagged - :onlyaudited onlyaudited)) - (limited-results - (a:subseq* results 0 limit))) - (to-json - (if nextpage - (list - :page (make-next-page limit (nthcdr limit results)) - :oneliners limited-results) - (list :oneliners limited-results))))) - ;; if neither command nor keywords were supplied, return a 400 - (t - (http-err 400))))) + (print commands) + (cond + ;; if page is incluced, ignore everything, fetch the next page, + ;; and return it. + (page + (to-json (fetch-next-page page))) + + ;; otherwise one of command keywords are required + ((or commands keywords) + (let* ((limit + (or limit 10)) ; default-limit, should probably be configurable. + (results + (query-oneliners :commands commands + :keywords keywords + :notflagged notflagged + :onlyaudited onlyaudited)) + (limited-results + (a:subseq* results 0 limit))) + (to-json + (if nextpage + (list + :page (make-next-page limit (nthcdr limit results)) + :oneliners limited-results) + (list :oneliners limited-results))))) + ;; if neither command nor keywords were supplied, return a 400 + (t + (http-err 400)))) (defendpoint* :put "/oneliner/:oneliner object-with-id:" () (:auth t) "Updates a oneliner entry in the wiki database." @@ -316,9 +325,9 @@ (oneliner (update-oneliner (request-contributor) oneliner - (lzb:request-body)) ; throws an error if fails, triggering a 500 + (lzb:request-body)) "true") - (t (http-err 404)))) ;no oneliner with the given id. + (t (http-err 404)))) (defendpoint* :post "/oneliner" () (:auth t) "Adds a new oneliner entry to the wiki database." @@ -396,14 +405,9 @@ names. NAME must be a symbol or a string." (defun uuid () (format nil "~a" (uuid:make-v1-uuid))) -(defun parse-cls (s) - "parse comman separated list." - (mapcar #'str:trim (str:split "," s))) -(defun parse-true-or-false (s) - (cond ((string-equal s "true") t) - ((string-equal s "false") nil) - (t (error "String ~s is neither 'true' nor 'false'" s)))) + + (defun oneliner-mentions-any (ol keywords) "A case insensitive search for the presence of any of KEYWORDS in the oneliner OL." -- cgit v1.2.3