first form

This commit is contained in:
2026-08-05 01:43:09 +03:00
parent 7115f505a7
commit fa1cc7df77
+28 -10
View File
@@ -2,12 +2,17 @@
(defun get-product (n)
;; Query the DB.
(list n (format nil "Product nb ~a" n) 9.99))
(list n (format nil "Product nb ~R" n) 9.99))
(defun products (&optional (n 5))
(loop for i from 0 below n
collect (get-product i)))
(defun search-products (products query)
(loop for product in products
if (search query (second product) :test #'equalp)
collect product))
(defvar *server* nil
"Server instance (Hunchentoot acceptor).")
@@ -21,16 +26,28 @@
args))
(defparameter *template-root* "
<title> Lisp web app </title>
<body>
<form action=\"/\" method=\"GET\">
<div>
<label for=\"query\">What do you search for?</label>
<input name=\"query\" id=\"query\" placeholder=\"Search…\" />
</div>
<div>
<button>Search</button>
</div>
</form>
{% if query %}
<div> query is: {{ query }} </div>
<ul>
{% for product in products %}
{% for product in results %}
<li>
<a href=\"/product/{{ product.0 }}\">{{ product.1 }} - {{ product.2 }}</a>
</li>
{% endfor %}
</ul>
</body>
{% endif %}
")
(defparameter *template-product* "
@@ -42,15 +59,16 @@
</body>
")
(easy-routes:defroute root ("/") ()
(render *template-root* :products (products)))
(easy-routes:defroute product-route ("/product/:n")
(defroute product-route ("/product/:n")
(&get debug &path (n 'integer))
(render *template-product* :product (get-product n) :debug debug))
(defroute root ("/" :method :get) ()
(render-products))
(defroute root ("/" :method :get) (query)
(render *template-root*
:results (search-products (products) query)
:query query))