restructure home routes
This commit is contained in:
parent
048266f0aa
commit
8a471893f5
2 changed files with 103 additions and 102 deletions
|
@ -1,7 +1,7 @@
|
|||
(ns cat.handler
|
||||
(:require [cat.middleware :as middleware]
|
||||
[cat.layout :refer [error-page]]
|
||||
[cat.routes.home :refer [home-routes]]
|
||||
[cat.routes.home :refer [show-home show-relations update-relationrequest-status create-relation-request]]
|
||||
[cat.routes.oauth :refer [oauth-init oauth-callback clear-session!]]
|
||||
[cat.routes.admin :refer [set-admin! create-new-relation! create-user!]]
|
||||
[compojure.core :refer [routes defroutes GET POST wrap-routes]]
|
||||
|
@ -15,6 +15,14 @@
|
|||
:start ((or (:init defaults) identity))
|
||||
:stop ((or (:stop defaults) identity)))
|
||||
|
||||
(defroutes public-routes
|
||||
(GET "/" req (show-home req))
|
||||
(GET "/relations_zeroed" [] (show-relations)))
|
||||
|
||||
(defroutes user-routes
|
||||
(POST "/relation_request/:id/status" [id & body] (update-relationrequest-status id body)) ; STATUS ENUM: (open, accepted, rejected)
|
||||
(POST "/request_relation" req (create-relation-request req)))
|
||||
|
||||
(defroutes oauth-routes
|
||||
(GET "/oauth/oauth-init" req (oauth-init req))
|
||||
(GET "/oauth/oauth-callback" req (oauth-callback req))
|
||||
|
@ -27,12 +35,12 @@
|
|||
(POST "/users" req (create-user! req)))
|
||||
|
||||
(defroutes app-routes
|
||||
(-> home-routes
|
||||
(-> public-routes
|
||||
middleware/wrap-csrf
|
||||
middleware/wrap-formats)
|
||||
(-> oauth-routes)
|
||||
(-> admin-routes
|
||||
middleware/wrap-restricted)
|
||||
user-routes
|
||||
oauth-routes
|
||||
admin-routes
|
||||
(route/not-found
|
||||
(:body
|
||||
(error-page {:status 404
|
||||
|
|
|
@ -12,24 +12,23 @@
|
|||
(def request_relation-schema
|
||||
[[:to_id st/required st/integer-str]])
|
||||
|
||||
(defn home-page [params]
|
||||
(defn- home-page [params]
|
||||
(layout/render "home.html" params))
|
||||
|
||||
(defn get-relations []
|
||||
(defn- get-relations []
|
||||
(map
|
||||
(fn [relation] (select-keys relation [:name :name_2]))
|
||||
(db/get-relations)))
|
||||
|
||||
(defn get-users []
|
||||
(defn- get-users []
|
||||
(db/get-users))
|
||||
|
||||
(defn response-wrong-parameters []
|
||||
(defn- response-wrong-parameters []
|
||||
(error-page {:status 400
|
||||
:title "Wrong request parameters"
|
||||
:message "Please contact your system administrator to fix this issue"}))
|
||||
|
||||
(defroutes home-routes
|
||||
(GET "/" req
|
||||
(defn show-home [req]
|
||||
(let [users (get-users)
|
||||
relations (get-relations)
|
||||
user (-> (get-in req [:session :user]))
|
||||
|
@ -59,13 +58,9 @@
|
|||
:rel-requests-in rel-requests-in
|
||||
:non_requested_users non_requested_users
|
||||
:flash (:flash req)})))
|
||||
;(GET "/docs" []
|
||||
; (-> (response/ok (-> "docs/docs.md" io/resource slurp))
|
||||
; (response/header "Content-Type" "text/plain; charset=utf-8")))
|
||||
(GET "/relations" []
|
||||
(let []
|
||||
(response/ok {})))
|
||||
(GET "/relations_zeroed" []
|
||||
|
||||
(defn show-relations
|
||||
[]
|
||||
(let [users (db/get-users)
|
||||
relations (db/get-relations)
|
||||
used-node-ids (set (flatten (map (fn [ln] [(:from_id ln) (:to_id ln)]) relations)))
|
||||
|
@ -89,21 +84,24 @@
|
|||
(response/ok {:nodes nodes-indexed
|
||||
:links rels-indexed})))
|
||||
|
||||
; TODO make next 2 user protected
|
||||
(POST "/relation_request/:id/status" [id & body]
|
||||
(defn update-relationrequest-status
|
||||
[id body]
|
||||
(let [rr_id_map {:id id}
|
||||
success (cond
|
||||
(contains? body :accept) (do
|
||||
(contains? body :accept)
|
||||
(do
|
||||
(let [rr (db/get-relation-request rr_id_map)]
|
||||
(db/create-relation! {:from_id (:from_id rr) :to_id (:to_id rr)}))
|
||||
(db/update-relation-request-status! (assoc rr_id_map :status "accepted")))
|
||||
(contains? body :decline) (db/update-relation-request-status! (assoc rr_id_map :status "declined"))
|
||||
(contains? body :decline)
|
||||
(db/update-relation-request-status! (assoc rr_id_map :status "declined"))
|
||||
:else false)]
|
||||
(if success
|
||||
(response/found "/")
|
||||
(response-wrong-parameters))))
|
||||
; STATUS ENUM: (open, accepted, rejected)
|
||||
(POST "/request_relation" req
|
||||
|
||||
(defn create-relation-request
|
||||
[req]
|
||||
(let [data (:params req)
|
||||
[err result] (st/validate data request_relation-schema)
|
||||
from-id (get-in req [:session :user :id])]
|
||||
|
@ -121,9 +119,4 @@
|
|||
(do
|
||||
(log/debug "Relation request failed")
|
||||
(log/debug err)
|
||||
(response/unprocessable-entity "Incorrect input"))))))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
(response/unprocessable-entity "Incorrect input")))))
|
||||
|
|
Loading…
Reference in a new issue