mirror of
https://gitlab.com/wheres-the-tp/server.git
synced 2026-01-25 04:34:55 -06:00
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:
parent
3735124a7e
commit
bcb7699a7e
8 changed files with 47 additions and 17 deletions
|
|
@ -16,4 +16,6 @@ To start a web server for the application, run:
|
||||||
|
|
||||||
To test a request/response, 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
|
||||||
|
|
|
||||||
4
sample-quantity-post.json
Normal file
4
sample-quantity-post.json
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"foodItemId": "9e1661c8-a453-4eb9-af07-d06cbaebd650",
|
||||||
|
"quantity": "lots"
|
||||||
|
}
|
||||||
|
|
@ -23,7 +23,7 @@ create table quantities (
|
||||||
PRIMARY KEY(food_item_id, date)
|
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)
|
INSERT INTO food_items (id, name, place_id, category, images, thumbImage, loc)
|
||||||
VALUES (
|
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;
|
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
|
||||||
|
);
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
(ns aretherecookies.app
|
(ns aretherecookies.app
|
||||||
(:gen-class)
|
(:gen-class)
|
||||||
(:require [aretherecookies.handler :refer [food-items-handler]]
|
(:require [aretherecookies.handler :refer [food-items-handler quantity-handler]]
|
||||||
[environ.core :refer [env]]
|
[environ.core :refer [env]]
|
||||||
[compojure.handler :refer [site]]
|
[compojure.handler :refer [site]]
|
||||||
[compojure.core :refer :all]
|
[compojure.core :refer :all]
|
||||||
|
|
@ -11,11 +11,10 @@
|
||||||
[ring.middleware.defaults :refer [wrap-defaults api-defaults]]))
|
[ring.middleware.defaults :refer [wrap-defaults api-defaults]]))
|
||||||
|
|
||||||
(defroutes app-routes
|
(defroutes app-routes
|
||||||
(GET "/" [] (str {:csrf-token
|
(GET "/" [] (str {:csrf-token *anti-forgery-token*}))
|
||||||
*anti-forgery-token*}))
|
|
||||||
(POST "/test" req "ok")
|
(POST "/test" req "ok")
|
||||||
(POST "/fooditems" req (wrap-json-body food-items-handler))
|
(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))
|
(def app-config (assoc-in api-defaults [:security :anti-forgery] false))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -44,14 +44,14 @@
|
||||||
:else by-distance)
|
:else by-distance)
|
||||||
args))
|
args))
|
||||||
|
|
||||||
(defn wrap-single-quotes [tokenVec]
|
(defn wrap-in-quotes [token]
|
||||||
(map #(str/replace % #"(^|$)" "'") tokenVec))
|
(str/replace token #"(^|$)" "'"))
|
||||||
|
|
||||||
(defn get-where [{:keys [:lat :lng :filter]}]
|
(defn get-where [{:keys [:lat :lng :filter]}]
|
||||||
(let [radius (-> (filter "radius") (or 10))
|
(let [radius (-> (filter "radius") (or 10))
|
||||||
categories (filter "categories")]
|
categories (filter "categories")]
|
||||||
(cond
|
(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}))))
|
:else (within-radius {:lat lat :lng lng :dist radius}))))
|
||||||
|
|
||||||
(defn query-food-items [{lat "lat" lng "lng" filter "filter"}]
|
(defn query-food-items [{lat "lat" lng "lng" filter "filter"}]
|
||||||
|
|
@ -60,4 +60,10 @@
|
||||||
{:lat lat
|
{:lat lat
|
||||||
:lng lng
|
:lng lng
|
||||||
:where (get-where {:lat lat :lng lng :filter filter})
|
: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)}))
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,21 @@
|
||||||
(ns aretherecookies.handler
|
(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
|
[aretherecookies.parsers :refer [food-items-to-json
|
||||||
parse-special-types]]
|
parse-special-types]]
|
||||||
[clojure.data.json :as json]
|
[clojure.data.json :as json]
|
||||||
[clojure.string :as str]))
|
[clojure.string :as str]))
|
||||||
|
|
||||||
(defn food-items-handler [req]
|
(defn food-items-handler [req]
|
||||||
(println "req---->" (:body req))
|
(println "req ---->" (:body req))
|
||||||
(let [{body :body} req]
|
(let [{body :body} req]
|
||||||
(json/write-str
|
(json/write-str
|
||||||
(hash-map
|
(hash-map
|
||||||
:filter (get body "filter")
|
:filter (get body "filter")
|
||||||
:fooditems (food-items-to-json (query-food-items body)))
|
:fooditems (food-items-to-json (query-food-items body)))
|
||||||
:value-fn parse-special-types)))
|
: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))
|
||||||
|
|
|
||||||
|
|
@ -9,11 +9,9 @@ SELECT
|
||||||
ST_AsGeoJSON(f.loc) AS location,
|
ST_AsGeoJSON(f.loc) AS location,
|
||||||
ST_Distance(f.loc, ST_SetSRID(ST_Point(:lng, :lat), 4326)::geography) / 1609 AS distance,
|
ST_Distance(f.loc, ST_SetSRID(ST_Point(:lng, :lat), 4326)::geography) / 1609 AS distance,
|
||||||
q.quantity AS quantity,
|
q.quantity AS quantity,
|
||||||
q.updated AS lastUpdated
|
q.date AS lastUpdated
|
||||||
FROM food_items f
|
FROM food_items f
|
||||||
LEFT OUTER JOIN (
|
LEFT OUTER JOIN latest_quantities q
|
||||||
SELECT food_item_id, quantity, MAX(date) AS updated FROM quantities GROUP BY food_item_id, quantity
|
|
||||||
) q
|
|
||||||
ON f.id = q.food_item_id
|
ON f.id = q.food_item_id
|
||||||
:snip:where
|
:snip:where
|
||||||
:snip:order
|
:snip:order
|
||||||
|
|
@ -34,4 +32,15 @@ ORDER BY distance ASC
|
||||||
ORDER BY lastUpdated DESC
|
ORDER BY lastUpdated DESC
|
||||||
|
|
||||||
-- :snip by-quantity
|
-- :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
|
||||||
Loading…
Add table
Reference in a new issue