quantity endpoint

inserts a new quantity into the quantities table and returns what it created - timestamp is generated at the SQL level
This commit is contained in:
Bart Akeley 2017-11-26 19:46:55 -06:00
parent 3735124a7e
commit bcb7699a7e
8 changed files with 47 additions and 17 deletions

View file

@ -16,4 +16,6 @@ To start a web server for the application, run:
To test a request/response, run:
curl --data @sample-post.json --header "Content-Type: application/json" http://localhost:3000/fooditems
curl --data @sample-food-item-post.json --header "Content-Type: application/json" http://localhost:3000/fooditems
curl --data @sample-quantity-post.json --header "Content-Type: application/json" http://localhost:3000/quantity

View file

@ -0,0 +1,4 @@
{
"foodItemId": "9e1661c8-a453-4eb9-af07-d06cbaebd650",
"quantity": "lots"
}

View file

@ -23,7 +23,7 @@ create table quantities (
PRIMARY KEY(food_item_id, date)
);
CREATE INDEX food_loc_index ON food_items USING GIST ( loc );
CREATE INDEX IF NOT EXISTS food_loc_index ON food_items USING GIST ( loc );
INSERT INTO food_items (id, name, place_id, category, images, thumbImage, loc)
VALUES (
@ -114,3 +114,7 @@ INSERT INTO food_items (id, name, place_id, category, images, thumbImage, loc)
);
INSERT INTO quantities SELECT id, current_timestamp, 'many' FROM food_items;
CREATE VIEW latest_quantities AS SELECT food_item_id, quantity, date from quantities q1 where date = (
SELECT max(date) FROM quantities q2 WHERE q1.food_item_id=q2.food_item_id
);

View file

@ -1,6 +1,6 @@
(ns aretherecookies.app
(:gen-class)
(:require [aretherecookies.handler :refer [food-items-handler]]
(:require [aretherecookies.handler :refer [food-items-handler quantity-handler]]
[environ.core :refer [env]]
[compojure.handler :refer [site]]
[compojure.core :refer :all]
@ -11,11 +11,10 @@
[ring.middleware.defaults :refer [wrap-defaults api-defaults]]))
(defroutes app-routes
(GET "/" [] (str {:csrf-token
*anti-forgery-token*}))
(GET "/" [] (str {:csrf-token *anti-forgery-token*}))
(POST "/test" req "ok")
(POST "/fooditems" req (wrap-json-body food-items-handler))
(GET "/echo" [location filter] (str location filter)))
(POST "/quantity" req (wrap-json-body quantity-handler)))
(def app-config (assoc-in api-defaults [:security :anti-forgery] false))

View file

@ -44,14 +44,14 @@
:else by-distance)
args))
(defn wrap-single-quotes [tokenVec]
(map #(str/replace % #"(^|$)" "'") tokenVec))
(defn wrap-in-quotes [token]
(str/replace token #"(^|$)" "'"))
(defn get-where [{:keys [:lat :lng :filter]}]
(let [radius (-> (filter "radius") (or 10))
categories (filter "categories")]
(cond
categories (has-category {:categories (wrap-single-quotes categories) :lat lat :lng lng :dist radius})
categories (has-category {:categories (map wrap-in-quotes categories) :lat lat :lng lng :dist radius})
:else (within-radius {:lat lat :lng lng :dist radius}))))
(defn query-food-items [{lat "lat" lng "lng" filter "filter"}]
@ -60,4 +60,10 @@
{:lat lat
:lng lng
:where (get-where {:lat lat :lng lng :filter filter})
:order (get-orderby filter)}))
:order (get-orderby filter)}))
(defn insert-quantity [{:keys [foodItemId quantity]}]
(insert-quantity-query @pooled-db {:food_item_id (wrap-in-quotes foodItemId) :quantity (wrap-in-quotes quantity)}))
(defn select-latest-quantity [{:keys [:foodItemId]}]
(select-latest-quantity-query @pooled-db {:food_item_id (wrap-in-quotes foodItemId)}))

View file

@ -1,15 +1,21 @@
(ns aretherecookies.handler
(:require [aretherecookies.db :refer [query-food-items]]
(:require [aretherecookies.db :refer [query-food-items insert-quantity]]
[aretherecookies.parsers :refer [food-items-to-json
parse-special-types]]
[clojure.data.json :as json]
[clojure.string :as str]))
(defn food-items-handler [req]
(println "req---->" (:body req))
(println "req ---->" (:body req))
(let [{body :body} req]
(json/write-str
(hash-map
:filter (get body "filter")
:fooditems (food-items-to-json (query-food-items body)))
:value-fn parse-special-types)))
(defn quantity-handler [{{foodItemId "foodItemId" quantity "quantity"} :body}]
(println "req ---->" foodItemId quantity)
(json/write-str
(insert-quantity {:foodItemId foodItemId :quantity quantity})
:value-fn parse-special-types))

View file

@ -9,11 +9,9 @@ SELECT
ST_AsGeoJSON(f.loc) AS location,
ST_Distance(f.loc, ST_SetSRID(ST_Point(:lng, :lat), 4326)::geography) / 1609 AS distance,
q.quantity AS quantity,
q.updated AS lastUpdated
q.date AS lastUpdated
FROM food_items f
LEFT OUTER JOIN (
SELECT food_item_id, quantity, MAX(date) AS updated FROM quantities GROUP BY food_item_id, quantity
) q
LEFT OUTER JOIN latest_quantities q
ON f.id = q.food_item_id
:snip:where
:snip:order
@ -34,4 +32,15 @@ ORDER BY distance ASC
ORDER BY lastUpdated DESC
-- :snip by-quantity
ORDER BY quantity DESC
ORDER BY quantity DESC
-- :name insert-quantity-query
INSERT INTO quantities (food_item_id, quantity, date)
VALUES (:i:food_item_id, :i:quantity, current_timestamp)
RETURNING *
-- :name select-latest-quantity-query
SELECT food_item_id, quantity, date AS updated
FROM quantities
WHERE food_item_id=:i:food_item_id
ORDER BY date DESC LIMIT 1