restructure home routes

This commit is contained in:
flynn 2019-06-09 00:53:13 +02:00
parent 048266f0aa
commit 8a471893f5
2 changed files with 103 additions and 102 deletions

View file

@ -1,7 +1,7 @@
(ns cat.handler (ns cat.handler
(:require [cat.middleware :as middleware] (:require [cat.middleware :as middleware]
[cat.layout :refer [error-page]] [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.oauth :refer [oauth-init oauth-callback clear-session!]]
[cat.routes.admin :refer [set-admin! create-new-relation! create-user!]] [cat.routes.admin :refer [set-admin! create-new-relation! create-user!]]
[compojure.core :refer [routes defroutes GET POST wrap-routes]] [compojure.core :refer [routes defroutes GET POST wrap-routes]]
@ -15,6 +15,14 @@
:start ((or (:init defaults) identity)) :start ((or (:init defaults) identity))
:stop ((or (:stop 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 (defroutes oauth-routes
(GET "/oauth/oauth-init" req (oauth-init req)) (GET "/oauth/oauth-init" req (oauth-init req))
(GET "/oauth/oauth-callback" req (oauth-callback req)) (GET "/oauth/oauth-callback" req (oauth-callback req))
@ -27,12 +35,12 @@
(POST "/users" req (create-user! req))) (POST "/users" req (create-user! req)))
(defroutes app-routes (defroutes app-routes
(-> home-routes (-> public-routes
middleware/wrap-csrf middleware/wrap-csrf
middleware/wrap-formats) middleware/wrap-formats)
(-> oauth-routes) user-routes
(-> admin-routes oauth-routes
middleware/wrap-restricted) admin-routes
(route/not-found (route/not-found
(:body (:body
(error-page {:status 404 (error-page {:status 404

View file

@ -12,24 +12,23 @@
(def request_relation-schema (def request_relation-schema
[[:to_id st/required st/integer-str]]) [[:to_id st/required st/integer-str]])
(defn home-page [params] (defn- home-page [params]
(layout/render "home.html" params)) (layout/render "home.html" params))
(defn get-relations [] (defn- get-relations []
(map (map
(fn [relation] (select-keys relation [:name :name_2])) (fn [relation] (select-keys relation [:name :name_2]))
(db/get-relations))) (db/get-relations)))
(defn get-users [] (defn- get-users []
(db/get-users)) (db/get-users))
(defn response-wrong-parameters [] (defn- response-wrong-parameters []
(error-page {:status 400 (error-page {:status 400
:title "Wrong request parameters" :title "Wrong request parameters"
:message "Please contact your system administrator to fix this issue"})) :message "Please contact your system administrator to fix this issue"}))
(defroutes home-routes (defn show-home [req]
(GET "/" req
(let [users (get-users) (let [users (get-users)
relations (get-relations) relations (get-relations)
user (-> (get-in req [:session :user])) user (-> (get-in req [:session :user]))
@ -59,13 +58,9 @@
:rel-requests-in rel-requests-in :rel-requests-in rel-requests-in
:non_requested_users non_requested_users :non_requested_users non_requested_users
:flash (:flash req)}))) :flash (:flash req)})))
;(GET "/docs" []
; (-> (response/ok (-> "docs/docs.md" io/resource slurp)) (defn show-relations
; (response/header "Content-Type" "text/plain; charset=utf-8"))) []
(GET "/relations" []
(let []
(response/ok {})))
(GET "/relations_zeroed" []
(let [users (db/get-users) (let [users (db/get-users)
relations (db/get-relations) relations (db/get-relations)
used-node-ids (set (flatten (map (fn [ln] [(:from_id ln) (:to_id ln)]) 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 (response/ok {:nodes nodes-indexed
:links rels-indexed}))) :links rels-indexed})))
; TODO make next 2 user protected (defn update-relationrequest-status
(POST "/relation_request/:id/status" [id & body] [id body]
(let [rr_id_map {:id id} (let [rr_id_map {:id id}
success (cond success (cond
(contains? body :accept) (do (contains? body :accept)
(do
(let [rr (db/get-relation-request rr_id_map)] (let [rr (db/get-relation-request rr_id_map)]
(db/create-relation! {:from_id (:from_id rr) :to_id (:to_id rr)})) (db/create-relation! {:from_id (:from_id rr) :to_id (:to_id rr)}))
(db/update-relation-request-status! (assoc rr_id_map :status "accepted"))) (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)] :else false)]
(if success (if success
(response/found "/") (response/found "/")
(response-wrong-parameters)))) (response-wrong-parameters))))
; STATUS ENUM: (open, accepted, rejected)
(POST "/request_relation" req (defn create-relation-request
[req]
(let [data (:params req) (let [data (:params req)
[err result] (st/validate data request_relation-schema) [err result] (st/validate data request_relation-schema)
from-id (get-in req [:session :user :id])] from-id (get-in req [:session :user :id])]
@ -121,9 +119,4 @@
(do (do
(log/debug "Relation request failed") (log/debug "Relation request failed")
(log/debug err) (log/debug err)
(response/unprocessable-entity "Incorrect input")))))) (response/unprocessable-entity "Incorrect input")))))