mirror of
https://gitlab.com/wheres-the-tp/server.git
synced 2026-01-25 07:54:57 -06:00
move SQL out of code using HugSQL
This commit is contained in:
parent
16bcc4de5d
commit
923014d563
4 changed files with 41 additions and 59 deletions
|
|
@ -13,7 +13,8 @@
|
||||||
[ring-middleware-format "0.7.2"]
|
[ring-middleware-format "0.7.2"]
|
||||||
[org.clojure/data.json "0.2.6"]
|
[org.clojure/data.json "0.2.6"]
|
||||||
[ring/ring-json "0.4.0"]
|
[ring/ring-json "0.4.0"]
|
||||||
[ring/ring-jetty-adapter "1.4.0"]]
|
[ring/ring-jetty-adapter "1.4.0"]
|
||||||
|
[com.layerware/hugsql "0.4.8"]]
|
||||||
:plugins [[lein-ring "0.9.7"] [lein-environ "1.1.0"]]
|
:plugins [[lein-ring "0.9.7"] [lein-environ "1.1.0"]]
|
||||||
:ring {:handler aretherecookies.app/app}
|
:ring {:handler aretherecookies.app/app}
|
||||||
:profiles
|
:profiles
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,16 @@
|
||||||
(ns aretherecookies.db
|
(ns aretherecookies.db
|
||||||
(:require
|
(:require [clojure.java.jdbc :as jdbc]
|
||||||
[clojure.java.jdbc :as jdbc]
|
|
||||||
[environ.core :refer [env]]
|
[environ.core :refer [env]]
|
||||||
[clojure.string :as str])
|
[clojure.string :as str]
|
||||||
|
[hugsql.core :as hugsql])
|
||||||
(:import com.mchange.v2.c3p0.ComboPooledDataSource))
|
(:import com.mchange.v2.c3p0.ComboPooledDataSource))
|
||||||
|
|
||||||
(def db-spec {
|
(hugsql/def-db-fns "aretherecookies/queries.sql")
|
||||||
:uri (env :database-jdbc-uri)
|
|
||||||
|
(def db-spec {:uri (env :database-jdbc-uri)
|
||||||
:password (env :database-password)
|
:password (env :database-password)
|
||||||
:user (env :database-user)
|
:user (env :database-user)
|
||||||
:name (env :database-name)
|
:name (env :database-name)})
|
||||||
})
|
|
||||||
|
|
||||||
(defn pool
|
(defn pool
|
||||||
[spec]
|
[spec]
|
||||||
|
|
@ -26,41 +26,7 @@
|
||||||
(println (str "jdbc:" (:url spec)))
|
(println (str "jdbc:" (:url spec)))
|
||||||
{:datasource cpds}))
|
{:datasource cpds}))
|
||||||
|
|
||||||
(def pooled-db (delay (pool db-spec)))
|
(defonce pooled-db (delay (pool db-spec)))
|
||||||
|
|
||||||
(def location-query "
|
(defn query-food-items-by-distance [{:keys [lat lng radius]}]
|
||||||
SELECT
|
(food-items-by-distance @pooled-db {:dist radius :lng lng :lat lat}))
|
||||||
id,
|
|
||||||
name,
|
|
||||||
place_id,
|
|
||||||
category,
|
|
||||||
images,
|
|
||||||
thumbImage,
|
|
||||||
ST_AsGeoJSON(loc) as location,
|
|
||||||
ST_Distance(
|
|
||||||
loc,
|
|
||||||
@LOCATION@
|
|
||||||
) / 1609 as distance
|
|
||||||
FROM food_items
|
|
||||||
WHERE
|
|
||||||
ST_DWithin(
|
|
||||||
loc,
|
|
||||||
@LOCATION@,
|
|
||||||
@DISTANCE@ * 1609
|
|
||||||
)
|
|
||||||
ORDER BY distance ASC;")
|
|
||||||
|
|
||||||
(defn point-text [lng lat]
|
|
||||||
(->
|
|
||||||
"ST_GeogFromText('SRID=4326;POINT(@LONGITUDE@ @LATITUDE@)')"
|
|
||||||
(str/replace "@LONGITUDE@" (str lng))
|
|
||||||
(str/replace "@LATITUDE@" (str lat))))
|
|
||||||
|
|
||||||
(defn build-fooditems-query [lng lat dist]
|
|
||||||
(->
|
|
||||||
location-query
|
|
||||||
(str/replace "@LOCATION@" (point-text (str lng) (str lat)))
|
|
||||||
(str/replace "@DISTANCE@" (str dist))))
|
|
||||||
|
|
||||||
(defn query-food-items [query]
|
|
||||||
(jdbc/query @pooled-db [query]))
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
(ns aretherecookies.handler
|
(ns aretherecookies.handler
|
||||||
(:require [aretherecookies.db :refer [query-food-items build-fooditems-query]]
|
(:require [aretherecookies.db :refer [query-food-items-by-distance]]
|
||||||
[compojure.core :refer :all]
|
[compojure.core :refer :all]
|
||||||
[compojure.route :as route]
|
[compojure.route :as route]
|
||||||
[clojure.data.json :as json]
|
[clojure.data.json :as json]
|
||||||
|
|
@ -42,7 +42,9 @@
|
||||||
:orderby orderby
|
:orderby orderby
|
||||||
:filter filter
|
:filter filter
|
||||||
:fooditems (food-items-to-json
|
:fooditems (food-items-to-json
|
||||||
(query-food-items (build-fooditems-query lng lat (get filter "radius")))))
|
(query-food-items-by-distance {:lat lat
|
||||||
|
:lng lng
|
||||||
|
:radius (get filter "radius")})))
|
||||||
:value-fn uuid-to-string)))
|
:value-fn uuid-to-string)))
|
||||||
|
|
||||||
(defroutes app-routes
|
(defroutes app-routes
|
||||||
|
|
|
||||||
13
src/aretherecookies/queries.sql
Normal file
13
src/aretherecookies/queries.sql
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
-- :name food-items-by-distance :raw
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
place_id,
|
||||||
|
category,
|
||||||
|
images,
|
||||||
|
thumbImage,
|
||||||
|
ST_AsGeoJSON(loc) as location,
|
||||||
|
ST_Distance(loc, ST_SetSRID(ST_Point(:lng, :lat),4326)::geography) / 1609 as distance
|
||||||
|
FROM food_items
|
||||||
|
WHERE ST_DWithin(loc, ST_SetSRID(ST_Point(:lng, :lat),4326)::geography, :dist * 1609)
|
||||||
|
ORDER BY distance ASC
|
||||||
Loading…
Add table
Reference in a new issue