Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from chadhs/ring-handlers-refactor
refactoring based on a better understanding of ring and compojure:
- Loading branch information
Showing
3 changed files
with
23 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.")))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters