Skip to content

Commit

Permalink
Merge pull request #16 from chadhs/ring-handlers-refactor
Browse files Browse the repository at this point in the history
refactoring based on a better understanding of ring and compojure:
  • Loading branch information
chadhs committed Nov 4, 2017
2 parents a236a6c + 8f57244 commit df90d71
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 47 deletions.
8 changes: 1 addition & 7 deletions src/listopia/core.clj
Expand Up @@ -12,13 +12,7 @@
(-> route/combined-routes
;; wrap-defaults includes ring middleware in the correct order to provide:
;; csrf protection, session data, url parameters, static assets, and more
(wrap-defaults
(-> site-defaults
;; disabling content-type injection since we're returning a ring response
(assoc-in [:security :content-type-options] false)
(assoc-in [:responses :content-types] false)
;; handling this within the application
(assoc-in [:params :keywordize] false)))
(wrap-defaults site-defaults)
wrap-webjars ; set path for webjar assets
middleware/wrap-server)) ; set server name header

Expand Down
35 changes: 13 additions & 22 deletions src/listopia/item/handler.clj
@@ -1,47 +1,38 @@
(ns listopia.item.handler
(:require [listopia.db :refer [database-url]]
[listopia.item.model :as item.model]))
[listopia.item.model :as item.model])
(:require [ring.util.response :as response]))


(defn handle-create-item! [req]
(let [name (get-in req [:params "name"])
description (get-in req [:params "description"])
list-id (java.util.UUID/fromString (get-in req [:params "list-id"]))
(let [name (get-in req [:params :name])
description (get-in req [:params :description])
list-id (java.util.UUID/fromString (get-in req [:params :list-id]))
db database-url
item-id (item.model/create-item!
db
{:name name
:description description
:list-id list-id})]
{:status 302
:headers {"Location" (str "/list/" list-id)}
:body ""}))
(response/redirect (str "/list/" list-id))))


(defn handle-delete-item! [req]
(let [db database-url
item-id (java.util.UUID/fromString (:item-id (:route-params req)))
list-id (java.util.UUID/fromString (get-in req [:params "list-id"]))
list-id (java.util.UUID/fromString (get-in req [:params :list-id]))
exists? (item.model/delete-item! db {:item-id item-id})]
(if exists?
{:status 302
:headers {"Location" (str "/list/" list-id)}
:body ""}
{:status 404
:body "Item not found."
:headers {}})))
(response/redirect (str "/list/" list-id))
(response/not-found "Item not found."))))


(defn handle-update-item! [req]
(let [db database-url
item-id (java.util.UUID/fromString (:item-id (:route-params req)))
list-id (java.util.UUID/fromString (get-in req [:params "list-id"]))
checked (get-in req [:params "checked"])
list-id (java.util.UUID/fromString (get-in req [:params :list-id]))
checked (get-in req [:params :checked])
exists? (item.model/update-item! db {:item-id item-id :checked (= "true" checked)})]
(if exists?
{:status 302
:headers {"Location" (str "/list/" list-id)}
:body ""}
{:status 404
:body "Item not found."
:headers {}})))
(response/redirect (str "/list/" list-id))
(response/not-found "Item not found."))))
27 changes: 9 additions & 18 deletions src/listopia/list/handler.clj
Expand Up @@ -3,35 +3,30 @@
[listopia.list.model :as list.model]
[listopia.item.model :as item.model]
[listopia.list.view.index :as list.view.index]
[listopia.list.view.list :as list.view.list]))
[listopia.list.view.list :as list.view.list])
(:require [ring.util.response :as response]))


(defn handle-index-lists [req]
(let [db database-url
lists (list.model/read-lists db)]
{:status 200
:headers {}
:body (list.view.index/lists-page lists)}))
(list.view.index/lists-page lists)))


(defn handle-index-list [req]
(let [db database-url
list-id (java.util.UUID/fromString (:list-id (:route-params req)))
list (list.model/read-list db {:list-id list-id})
list-items (item.model/read-list-items db {:list-id list-id})]
{:status 200
:headers {}
:body (list.view.list/list-page list list-id list-items)}))
(list.view.list/list-page list list-id list-items)))


(defn handle-create-list! [req]
(let [name (get-in req [:params "name"])
description (get-in req [:params "description"])
(let [name (get-in req [:params :name])
description (get-in req [:params :description])
db database-url
list-id (list.model/create-list! db {:name name :description description})]
{:status 302
:headers {"Location" "/lists"}
:body ""}))
(response/redirect "/lists")))


(defn handle-delete-list! [req]
Expand All @@ -41,9 +36,5 @@
(item.model/delete-list-items! db {:list-id list-id})
(list.model/delete-list! db {:list-id list-id}))]
(if exists?
{:status 302
:headers {"Location" "/lists"}
:body ""}
{:status 404
:body "List not found."
:headers {}})))
(response/redirect "/lists")
(response/not-found "List not found."))))

0 comments on commit df90d71

Please sign in to comment.