From 3735124a7ea826a93b38d0eff0d36ba6cb537c3b Mon Sep 17 00:00:00 2001 From: Bart Akeley Date: Sat, 11 Nov 2017 20:13:23 -0600 Subject: [PATCH] updates to categories handling --- sample-post.json | 5 +-- src/aretherecookies/app.clj | 13 +++++++- src/aretherecookies/db.clj | 15 +++++---- src/aretherecookies/handler.clj | 14 +------- src/aretherecookies/parsers.clj | 58 ++++++++++++++++----------------- src/aretherecookies/queries.sql | 2 +- 6 files changed, 55 insertions(+), 52 deletions(-) diff --git a/sample-post.json b/sample-post.json index 36022b6..76eb67a 100644 --- a/sample-post.json +++ b/sample-post.json @@ -1,8 +1,9 @@ { "lat": 30.3033267, "lng": -97.7286718, - "orderby": "distance", "filter": { - "radius": 10 + "orderby": "distance", + "radius": 10, + "categories": ["desserts", "beverages", "entrees", "other"] } } diff --git a/src/aretherecookies/app.clj b/src/aretherecookies/app.clj index 88ad90e..4da5314 100644 --- a/src/aretherecookies/app.clj +++ b/src/aretherecookies/app.clj @@ -1,11 +1,22 @@ (ns aretherecookies.app (:gen-class) - (:require [aretherecookies.handler :refer [app-routes]] + (:require [aretherecookies.handler :refer [food-items-handler]] [environ.core :refer [env]] [compojure.handler :refer [site]] + [compojure.core :refer :all] + [compojure.route :as route] [ring.adapter.jetty :as jetty] + [ring.middleware.anti-forgery :refer :all] + [ring.middleware.json :refer [wrap-json-body]] [ring.middleware.defaults :refer [wrap-defaults api-defaults]])) +(defroutes app-routes + (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))) + (def app-config (assoc-in api-defaults [:security :anti-forgery] false)) (def app (wrap-defaults app-routes app-config)) diff --git a/src/aretherecookies/db.clj b/src/aretherecookies/db.clj index 033382e..49bbe13 100644 --- a/src/aretherecookies/db.clj +++ b/src/aretherecookies/db.clj @@ -35,7 +35,7 @@ (defonce pooled-db (delay (pool db-spec))) -(defn get-orderby [orderby & args] +(defn get-orderby [{orderby "orderby"} & args] (apply (cond (= orderby "distance") by-distance @@ -44,17 +44,20 @@ :else by-distance) args)) +(defn wrap-single-quotes [tokenVec] + (map #(str/replace % #"(^|$)" "'") tokenVec)) + (defn get-where [{:keys [:lat :lng :filter]}] - (let [radius (or (get filter "radius") 10) - categories (get filter "categories")] + (let [radius (-> (filter "radius") (or 10)) + categories (filter "categories")] (cond - categories (has-category {:categories categories :lat lat :lng lng :dist radius}) + categories (has-category {:categories (wrap-single-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" orderby "orderby"}] +(defn query-food-items [{lat "lat" lng "lng" filter "filter"}] (select-food-items @pooled-db {:lat lat :lng lng :where (get-where {:lat lat :lng lng :filter filter}) - :order (get-orderby orderby)})) \ No newline at end of file + :order (get-orderby filter)})) \ No newline at end of file diff --git a/src/aretherecookies/handler.clj b/src/aretherecookies/handler.clj index 0fbeb1a..473d2e4 100644 --- a/src/aretherecookies/handler.clj +++ b/src/aretherecookies/handler.clj @@ -2,26 +2,14 @@ (:require [aretherecookies.db :refer [query-food-items]] [aretherecookies.parsers :refer [food-items-to-json parse-special-types]] - [compojure.core :refer :all] - [compojure.route :as route] [clojure.data.json :as json] - [clojure.string :as str] - [ring.middleware.anti-forgery :refer :all] - [ring.middleware.json :refer [wrap-json-body]])) + [clojure.string :as str])) (defn food-items-handler [req] (println "req---->" (:body req)) (let [{body :body} req] (json/write-str (hash-map - :orderby (get body "orderby") :filter (get body "filter") :fooditems (food-items-to-json (query-food-items body))) :value-fn parse-special-types))) - -(defroutes app-routes - (GET "/" [] (str {:csrf-token - *anti-forgery-token*})) - (POST "/test" req "ok") - (POST "/fooditems" req (wrap-json-body food-items-handler)) - (GET "/echo" [location orderby filter] (str location orderby filter))) diff --git a/src/aretherecookies/parsers.clj b/src/aretherecookies/parsers.clj index ce687a3..e391fb8 100644 --- a/src/aretherecookies/parsers.clj +++ b/src/aretherecookies/parsers.clj @@ -4,32 +4,32 @@ [ring.middleware.anti-forgery :refer :all] [ring.middleware.json :refer [wrap-json-body]])) - (defn parse-special-types [key value] - (cond - (instance? java.util.UUID value) (.toString value) - (instance? java.sql.Timestamp value) (.getTime value) - :else value)) - - (defn get-coords [item] - (-> - item - (get :location) - json/read-str - (get "coordinates"))) - - (defn build-lat-lng [[lng lat]] - (hash-map :longitude lng :latitude lat)) - - (defn build-latlng [item] - (let [[lng lat] (get-coords item)] - (hash-map :longitude lng :latitude lat))) - - (defn parse-location [item] - (-> - item - build-latlng - (merge item) - (dissoc :location))) - - (defn food-items-to-json [query-result] - (map parse-location query-result)) \ No newline at end of file +(defn parse-special-types [key value] + (cond + (instance? java.util.UUID value) (.toString value) + (instance? java.sql.Timestamp value) (.getTime value) + :else value)) + +(defn get-coords [item] + (-> + item + (get :location) + json/read-str + (get "coordinates"))) + +(defn build-lat-lng [[lng lat]] + (hash-map :longitude lng :latitude lat)) + +(defn build-latlng [item] + (let [[lng lat] (get-coords item)] + (hash-map :longitude lng :latitude lat))) + +(defn parse-location [item] + (-> + item + build-latlng + (merge item) + (dissoc :location))) + +(defn food-items-to-json [query-result] + (map parse-location query-result)) \ No newline at end of file diff --git a/src/aretherecookies/queries.sql b/src/aretherecookies/queries.sql index dac81ce..1584d83 100644 --- a/src/aretherecookies/queries.sql +++ b/src/aretherecookies/queries.sql @@ -22,7 +22,7 @@ ON f.id = q.food_item_id WHERE ST_DWithin(loc, ST_SetSRID(ST_Point(:lng, :lat), 4326)::geography, :dist * 1609) AND -category IN (:categories) +category IN (:i*:categories) -- :snip within-radius WHERE ST_DWithin(loc, ST_SetSRID(ST_Point(:lng, :lat), 4326)::geography, :dist * 1609)