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,118 +12,111 @@
(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])) user-relations (when user
user-relations (when user (seq (filter (fn [rel]
(seq (filter (fn [rel] (or
(or (= (:name rel) (:name user))
(= (:name rel) (:name user)) (= (:name_2 rel) (:name user))))
(= (:name_2 rel) (:name user)))) relations)))
relations))) other_users (when user
other_users (when user (seq (filter (fn [usr] (not (= (:id usr) (:id user))))
(seq (filter (fn [usr] (not (= (:id usr) (:id user)))) users)))
users))) rel-requests-out (seq (db/get-relation-requests-from-user {:from_id (:id user)}))
rel-requests-out (seq (db/get-relation-requests-from-user {:from_id (:id user)})) rel-requests-in (seq (db/get-relation-requests-to-user {:to_id (:id user)}))
rel-requests-in (seq (db/get-relation-requests-to-user {:to_id (:id user)})) non_requested_users (seq (filter (fn [other-user] (not (some (partial = (:id other-user)) (map :to_id rel-requests-out)))) other_users))]
non_requested_users (seq (filter (fn [other-user] (not (some (partial = (:id other-user)) (map :to_id rel-requests-out)))) other_users))] (log/debug (str "Session: " (:session req)))
(log/debug (str "Session: " (:session req)))
;(log/info (str "Relation requests: \n OUTGOING: " rel-requests-out "\n INCOMING: " rel-requests-in)) ;(log/info (str "Relation requests: \n OUTGOING: " rel-requests-out "\n INCOMING: " rel-requests-in))
;(log/info (str "User relations: " user-relations)) ;(log/info (str "User relations: " user-relations))
;(log/info (str "Other Users: " other_users)) ;(log/info (str "Other Users: " other_users))
;(log/info (str "rel reqs out: " rel-requests-out)) ;(log/info (str "rel reqs out: " rel-requests-out))
;(log/info (str "rel reqs out id: " (seq (map :to_id rel-requests-out)))) ;(log/info (str "rel reqs out id: " (seq (map :to_id rel-requests-out))))
(home-page {:relations relations (home-page {:relations relations
:users users :users users
:user user :user user
:user-relations user-relations :user-relations user-relations
:rel-requests-out rel-requests-out :rel-requests-out rel-requests-out
: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))
; (response/header "Content-Type" "text/plain; charset=utf-8")))
(GET "/relations" []
(let []
(response/ok {})))
(GET "/relations_zeroed" []
(let [users (db/get-users)
relations (db/get-relations)
used-node-ids (set (flatten (map (fn [ln] [(:from_id ln) (:to_id ln)]) relations)))
filtered-users (filter (fn [{id :id}] (contains? used-node-ids id)) users)
id-index-map (:map (reduce (fn [{map :map idx :index} usr]
{:map (assoc map (:id usr) idx)
:index (inc idx)})
{:map {} :index 0}
filtered-users))
rels-indexed (map (fn [{src :from_id target :to_id}]
{:source (get id-index-map src)
:target (get id-index-map target)
:value (+ 20 (rand-int 30))})
relations)
nodes-indexed (->> filtered-users
(map (fn [usr]
(-> usr
(dissoc :gender :id)
(assoc :index (get id-index-map (:id usr)))
(assoc :group (rand-int 5))))))]
(response/ok {:nodes nodes-indexed
:links rels-indexed})))
; TODO make next 2 user protected
(POST "/relation_request/:id/status" [id & body]
(let [rr_id_map {:id id}
success (cond
(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"))
:else false)]
(if success
(response/found "/")
(response-wrong-parameters))))
; STATUS ENUM: (open, accepted, rejected)
(POST "/request_relation" req
(let [data (:params req)
[err result] (st/validate data request_relation-schema)
from-id (get-in req [:session :user :id])]
(if (nil? from-id) (response/found (error-page
{:status 400
:title "No user id found in session"})))
(log/debug "Post to " (:uri req) "\n with data " result)
(if (nil? err)
(do
(log/debug "Create relation request")
(db/create-relation-request! {:from_id from-id
:to_id (:to_id result)
:status "open"})
(response/found "/"))
(do
(log/debug "Relation request failed")
(log/debug err)
(response/unprocessable-entity "Incorrect input"))))))
(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)))
filtered-users (filter (fn [{id :id}] (contains? used-node-ids id)) users)
id-index-map (:map (reduce (fn [{map :map idx :index} usr]
{:map (assoc map (:id usr) idx)
:index (inc idx)})
{:map {} :index 0}
filtered-users))
rels-indexed (map (fn [{src :from_id target :to_id}]
{:source (get id-index-map src)
:target (get id-index-map target)
:value (+ 20 (rand-int 30))})
relations)
nodes-indexed (->> filtered-users
(map (fn [usr]
(-> usr
(dissoc :gender :id)
(assoc :index (get id-index-map (:id usr)))
(assoc :group (rand-int 5))))))]
(response/ok {:nodes nodes-indexed
:links rels-indexed})))
(defn update-relationrequest-status
[id body]
(let [rr_id_map {:id id}
success (cond
(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"))
:else false)]
(if success
(response/found "/")
(response-wrong-parameters))))
(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])]
(if (nil? from-id) (response/found (error-page
{:status 400
:title "No user id found in session"})))
(log/debug "Post to " (:uri req) "\n with data " result)
(if (nil? err)
(do
(log/debug "Create relation request")
(db/create-relation-request! {:from_id from-id
:to_id (:to_id result)
:status "open"})
(response/found "/"))
(do
(log/debug "Relation request failed")
(log/debug err)
(response/unprocessable-entity "Incorrect input")))))