From c5abfd1203f37837fd9440146dd8b1e8ac55e0c6 Mon Sep 17 00:00:00 2001 From: Bart Akeley Date: Sun, 8 Apr 2018 10:57:15 -0500 Subject: [PATCH] bugfixes with get/set images --- scripts/ddl.sql | 130 ++++++++++++++++---------------- src/aretherecookies/db.clj | 2 +- src/aretherecookies/handler.clj | 42 +++++++---- src/aretherecookies/queries.sql | 14 +++- 4 files changed, 106 insertions(+), 82 deletions(-) diff --git a/scripts/ddl.sql b/scripts/ddl.sql index e792907..b490818 100644 --- a/scripts/ddl.sql +++ b/scripts/ddl.sql @@ -32,68 +32,68 @@ DROP VIEW IF EXISTS latest_quantities; 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 ); -INSERT INTO food_items (id, name, place_id, category, loc) - VALUES ( - uuid_generate_v4(), - 'Big John Cookies', - 'ChIJf5QJFBK1RIYRjjfxZz9Z0O0', - 'desserts', - ST_GeogFromText('SRID=4326;POINT(-97.7532464 30.270667599999996)') - ); -INSERT INTO food_items (id, name, place_id, category, loc) - VALUES ( - uuid_generate_v4(), - 'Jelly Filled Donuts', - 'ChIJ72if-Qe1RIYRCzMucGEEdBA', - 'desserts', - ST_GeogFromText('SRID=4326;POINT(-97.742308 30.263963300000004)') - ); -INSERT INTO food_items (id, name, place_id, category, loc) - VALUES ( - uuid_generate_v4(), - 'Spelt Brownies', - 'ChIJG44vBQi1RIYRvWGHdYUolZY', - 'desserts', - ST_GeogFromText('SRID=4326;POINT(-97.7419085 30.265019100000004)') - ); -INSERT INTO food_items (id, name, place_id, category, loc) - VALUES ( - uuid_generate_v4(), - 'Oatmeal Raisin Cookies', - 'ChIJf5QJFBK1RIYRjjfxZz9Z0O0', - 'desserts', - ST_GeogFromText('SRID=4326;POINT(-97.7532464 30.270667599999996)') - ); -INSERT INTO food_items (id, name, place_id, category, loc) - VALUES ( - uuid_generate_v4(), - 'Donuts with Sprinkles', - 'ChIJ72if-Qe1RIYRCzMucGEEdBA', - 'desserts', - ST_GeogFromText('SRID=4326;POINT(-97.742308 30.263963300000004)') - ); -INSERT INTO food_items (id, name, place_id, category, loc) - VALUES ( - uuid_generate_v4(), - 'Powdered Donuts', - 'ChIJ72if-Qe1RIYRCzMucGEEdBA', - 'desserts', - ST_GeogFromText('SRID=4326;POINT(-97.742308 30.263963300000004)') - ); -INSERT INTO food_items (id, name, place_id, category, loc) - VALUES ( - uuid_generate_v4(), - 'Snickerdoodles', - 'ChIJr3szW6a1RIYRkM7LRpnBIO0', - 'desserts', - ST_GeogFromText('SRID=4326;POINT(-97.73798459999999 30.266898599999998)') - ); -INSERT INTO food_items (id, name, place_id, category, loc) - VALUES ( - uuid_generate_v4(), - 'Pizza', - 'ChIJr3szW6a1RIYRkM7LRpnBIO0', - 'desserts', - ST_GeogFromText('SRID=4326;POINT(-97.73798459999999 30.266898599999998)') - ); -INSERT INTO quantities SELECT id, current_timestamp, 'many' FROM food_items; \ No newline at end of file +--INSERT INTO food_items (id, name, place_id, category, loc) +-- VALUES ( +-- uuid_generate_v4(), +-- 'Big John Cookies', +-- 'ChIJf5QJFBK1RIYRjjfxZz9Z0O0', +-- 'desserts', +-- ST_GeogFromText('SRID=4326;POINT(-97.7532464 30.270667599999996)') +-- ); +--INSERT INTO food_items (id, name, place_id, category, loc) +-- VALUES ( +-- uuid_generate_v4(), +-- 'Jelly Filled Donuts', +-- 'ChIJ72if-Qe1RIYRCzMucGEEdBA', +-- 'desserts', +-- ST_GeogFromText('SRID=4326;POINT(-97.742308 30.263963300000004)') +-- ); +--INSERT INTO food_items (id, name, place_id, category, loc) +-- VALUES ( +-- uuid_generate_v4(), +-- 'Spelt Brownies', +-- 'ChIJG44vBQi1RIYRvWGHdYUolZY', +-- 'desserts', +-- ST_GeogFromText('SRID=4326;POINT(-97.7419085 30.265019100000004)') +-- ); +--INSERT INTO food_items (id, name, place_id, category, loc) +-- VALUES ( +-- uuid_generate_v4(), +-- 'Oatmeal Raisin Cookies', +-- 'ChIJf5QJFBK1RIYRjjfxZz9Z0O0', +-- 'desserts', +-- ST_GeogFromText('SRID=4326;POINT(-97.7532464 30.270667599999996)') +-- ); +--INSERT INTO food_items (id, name, place_id, category, loc) +-- VALUES ( +-- uuid_generate_v4(), +-- 'Donuts with Sprinkles', +-- 'ChIJ72if-Qe1RIYRCzMucGEEdBA', +-- 'desserts', +-- ST_GeogFromText('SRID=4326;POINT(-97.742308 30.263963300000004)') +-- ); +--INSERT INTO food_items (id, name, place_id, category, loc) +-- VALUES ( +-- uuid_generate_v4(), +-- 'Powdered Donuts', +-- 'ChIJ72if-Qe1RIYRCzMucGEEdBA', +-- 'desserts', +-- ST_GeogFromText('SRID=4326;POINT(-97.742308 30.263963300000004)') +-- ); +--INSERT INTO food_items (id, name, place_id, category, loc) +-- VALUES ( +-- uuid_generate_v4(), +-- 'Snickerdoodles', +-- 'ChIJr3szW6a1RIYRkM7LRpnBIO0', +-- 'desserts', +-- ST_GeogFromText('SRID=4326;POINT(-97.73798459999999 30.266898599999998)') +-- ); +--INSERT INTO food_items (id, name, place_id, category, loc) +-- VALUES ( +-- uuid_generate_v4(), +-- 'Pizza', +-- 'ChIJr3szW6a1RIYRkM7LRpnBIO0', +-- 'desserts', +-- ST_GeogFromText('SRID=4326;POINT(-97.73798459999999 30.266898599999998)') +-- ); +--INSERT INTO quantities SELECT id, current_timestamp, 'many' FROM food_items; \ No newline at end of file diff --git a/src/aretherecookies/db.clj b/src/aretherecookies/db.clj index 56a0254..a5c8c17 100644 --- a/src/aretherecookies/db.clj +++ b/src/aretherecookies/db.clj @@ -84,7 +84,7 @@ (defn get-images "query database for a list of images for a food item id" [foodItemId] - (select-images @pooled-db {:id foodItemId})) + (select-images @pooled-db {:foodItemId foodItemId})) (defn add-image "update the list of images for given food item id" diff --git a/src/aretherecookies/handler.clj b/src/aretherecookies/handler.clj index 6f0ebe7..f771dfb 100644 --- a/src/aretherecookies/handler.clj +++ b/src/aretherecookies/handler.clj @@ -27,16 +27,24 @@ {:status 400 :body (safe-json body)}) -;(defn image-filename-to-url +(defn update-key-to-s3 + "return a map with key updated to point to s3" + ([key] + (fn [map] + (update map key build-s3-url))) + ([key map] + (update map key build-s3-url))) + +;(defn update-image-url-to-s3 ; "convert image filename key to url key" ; [image] -; (assoc image :url (build-s3-url (get image :filename)))) - - -(defn thumbimage-s3-url - "convert all the images of a food item to fully qualified s3 URLs" - [food-item] - (update food-item :thumbimage build-s3-url)) +; (update image :url build-s3-url)) +; +; +;(defn update-thumbimage-to-s3 +; "convert all the images of a food item to fully qualified s3 URLs" +; [food-item] +; (update food-item :thumbimage build-s3-url)) (defn food-items-handler @@ -50,7 +58,7 @@ :fooditems (->> (query-food-items body) (map parse-location) - (map thumbimage-s3-url)))))) + (map (update-key-to-s3 :thumbimage))))))) (defn quantity-handler @@ -90,7 +98,10 @@ "return all images for a given foodItemId" [req] (let [foodItemId (get-in req [:params :foodItemId])] - (get-images foodItemId))) + (->> + (get-images foodItemId) + (map #(update % :url build-s3-url)) + safe-json))) (defn add-image-handler @@ -103,7 +114,10 @@ img-name (str foodItemId "/" (UUID/randomUUID) ".png")] (println "/addimage ---->" img-name) (put-s3 img-name image) - (add-image {:food_item_id foodItemId - :filename img-name - :username username}) - (build-s3-url img-name))) \ No newline at end of file + (->> + (add-image {:food_item_id foodItemId + :filename img-name + :username username}) + first + (update-key-to-s3 :url) + safe-json))) \ No newline at end of file diff --git a/src/aretherecookies/queries.sql b/src/aretherecookies/queries.sql index 81e1d3d..a93f1d7 100644 --- a/src/aretherecookies/queries.sql +++ b/src/aretherecookies/queries.sql @@ -62,7 +62,13 @@ RETURNING ST_Distance(loc, ST_SetSRID(ST_Point(:longitude, :latitude), 4326)::geography) / 1609 AS distance -- :name select-images -SELECT * FROM images WHERE food_item-id=:v:food_item_id:uuid +SELECT + filename as url, + date, + username, + food_item_id +FROM images +WHERE food_item_id=:v:foodItemId::uuid -- :name insert-image INSERT INTO images (filename, username, date, food_item_id) @@ -72,4 +78,8 @@ INSERT INTO images (filename, username, date, food_item_id) current_timestamp, :v:food_item_id::uuid ) -RETURNING * \ No newline at end of file +RETURNING + filename as url, + date, + username, + food_item_id \ No newline at end of file