New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring based on a better understanding of ring and compojure: #16

Merged
merged 1 commit into from Nov 4, 2017
File filter...
Filter file types
Jump to file or symbol
Failed to load files and symbols.
+23 −47
Diff settings

Always

Just for now

Copy path View file
@@ -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

Copy path View file
@@ -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."))))
Copy path View file
@@ -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]
@@ -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."))))
ProTip! Use n and p to navigate between commits in a pull request.