mirror of
https://gitlab.com/wheres-the-tp/server.git
synced 2026-01-25 07:54:57 -06:00
updates to categories handling
This commit is contained in:
parent
66e5a41296
commit
3735124a7e
6 changed files with 55 additions and 52 deletions
|
|
@ -1,8 +1,9 @@
|
|||
{
|
||||
"lat": 30.3033267,
|
||||
"lng": -97.7286718,
|
||||
"orderby": "distance",
|
||||
"filter": {
|
||||
"radius": 10
|
||||
"orderby": "distance",
|
||||
"radius": 10,
|
||||
"categories": ["desserts", "beverages", "entrees", "other"]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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)}))
|
||||
:order (get-orderby filter)}))
|
||||
|
|
@ -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)))
|
||||
|
|
|
|||
|
|
@ -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 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 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-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 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 parse-location [item]
|
||||
(->
|
||||
item
|
||||
build-latlng
|
||||
(merge item)
|
||||
(dissoc :location)))
|
||||
|
||||
(defn food-items-to-json [query-result]
|
||||
(map parse-location query-result))
|
||||
(defn food-items-to-json [query-result]
|
||||
(map parse-location query-result))
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue