From 8a471893f5716cf6c7f53cb552bfeab06deb496e Mon Sep 17 00:00:00 2001 From: flynn Date: Sun, 9 Jun 2019 00:53:13 +0200 Subject: [PATCH] restructure home routes --- src/clj/cat/handler.clj | 18 +++- src/clj/cat/routes/home.clj | 187 +++++++++++++++++------------------- 2 files changed, 103 insertions(+), 102 deletions(-) diff --git a/src/clj/cat/handler.clj b/src/clj/cat/handler.clj index 60677a0..398a4ca 100644 --- a/src/clj/cat/handler.clj +++ b/src/clj/cat/handler.clj @@ -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 diff --git a/src/clj/cat/routes/home.clj b/src/clj/cat/routes/home.clj index a4f267a..ed82c75 100644 --- a/src/clj/cat/routes/home.clj +++ b/src/clj/cat/routes/home.clj @@ -12,118 +12,111 @@ (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))) + (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 - (let [users (get-users) - relations (get-relations) - user (-> (get-in req [:session :user])) - user-relations (when user - (seq (filter (fn [rel] - (or - (= (:name rel) (:name user)) - (= (:name_2 rel) (:name user)))) - relations))) - other_users (when user - (seq (filter (fn [usr] (not (= (:id usr) (:id user)))) - users))) - 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)})) - 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))) +(defn show-home [req] + (let [users (get-users) + relations (get-relations) + user (-> (get-in req [:session :user])) + user-relations (when user + (seq (filter (fn [rel] + (or + (= (:name rel) (:name user)) + (= (:name_2 rel) (:name user)))) + relations))) + other_users (when user + (seq (filter (fn [usr] (not (= (:id usr) (:id user)))) + users))) + 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)})) + 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/info (str "Relation requests: \n OUTGOING: " rel-requests-out "\n INCOMING: " rel-requests-in)) ;(log/info (str "User relations: " user-relations)) ;(log/info (str "Other Users: " other_users)) ;(log/info (str "rel reqs out: " rel-requests-out)) ;(log/info (str "rel reqs out id: " (seq (map :to_id rel-requests-out)))) - (home-page {:relations relations - :users users - :user user - :user-relations user-relations - :rel-requests-out rel-requests-out - :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" [] - (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")))))) - - + (home-page {:relations relations + :users users + :user user + :user-relations user-relations + :rel-requests-out rel-requests-out + :rel-requests-in rel-requests-in + :non_requested_users non_requested_users + :flash (:flash req)}))) +(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")))))