diff --git a/scripts/ddl.sql b/scripts/ddl.sql index b490818..4f2724c 100644 --- a/scripts/ddl.sql +++ b/scripts/ddl.sql @@ -1,5 +1,6 @@ CREATE EXTENSION IF NOT EXISTS "postgis"; CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; +CREATE EXTENSION IF NOT EXISTS "fuzzystrmatch"; DROP TYPE IF EXISTS QUANTITY CASCADE; CREATE TYPE QUANTITY AS ENUM ('lots', 'many', 'few', 'none'); DROP TYPE IF EXISTS CATEGORY CASCADE; diff --git a/src/aretherecookies/db.clj b/src/aretherecookies/db.clj index a5c8c17..14eb4f3 100644 --- a/src/aretherecookies/db.clj +++ b/src/aretherecookies/db.clj @@ -49,12 +49,16 @@ (defn wrap-in-quotes [token] (str/replace token #"(^|$)" "'")) +(defn wildcard + [term] + (str "%" (str/join (interpose "%" term)) "%")) + (defn get-where [{:keys [:lat :lng :filter]}] - (let [radius (or (:radius filter) 10) - categories (:categories filter)] - (cond - categories (has-category {:categories (map wrap-in-quotes categories) :lat lat :lng lng :dist radius}) - :else (within-radius {:lat lat :lng lng :dist radius})))) + (food-items-where-clause {:lat lat + :lng lng + :dist (:radius filter) + :categories (map wrap-in-quotes (:categories filter)) + :search (wildcard (:search filter))})) (defn query-food-items [{lat :lat lng :lng filter :filter}] (select-food-items @@ -72,12 +76,12 @@ (defn create-food-item [{:keys [:name :placeId :category :quantity :latitude :longitude]}] (let [food-item (first (insert-food-item - @pooled-db - {:name name - :placeId placeId - :category category - :longitude longitude - :latitude latitude})) + @pooled-db + {:name name + :placeId placeId + :category category + :longitude longitude + :latitude latitude})) quantity (first (insert-quantity {:foodItemId (:id food-item) :quantity quantity}))] (merge food-item (select-keys quantity [:quantity :date])))) diff --git a/src/aretherecookies/handler.clj b/src/aretherecookies/handler.clj index fbce204..12ce484 100644 --- a/src/aretherecookies/handler.clj +++ b/src/aretherecookies/handler.clj @@ -33,7 +33,7 @@ (fn [map] (update map key build-s3-url))) ([key map] - (update map key build-s3-url))) + (update map key build-s3-url))) (defn food-items-handler diff --git a/src/aretherecookies/queries.sql b/src/aretherecookies/queries.sql index a93f1d7..9328721 100644 --- a/src/aretherecookies/queries.sql +++ b/src/aretherecookies/queries.sql @@ -24,6 +24,12 @@ category IN (:i*:categories) -- :snip within-radius WHERE ST_DWithin(loc, ST_SetSRID(ST_Point(:lng, :lat), 4326)::geography, :dist * 1609) +-- :snip food-items-where-clause +WHERE +ST_DWithin(loc, ST_SetSRID(ST_Point(:lng, :lat), 4326)::geography, :dist * 1609) +--~ (when (seq (:categories params)) "AND category IN (:i*:categories)") +--~ (when (not-empty (:search params)) "AND name like :v:search") + -- :snip by-distance ORDER BY distance ASC