 ;;**************************************************************************************************
 
 (defn routes-webusers* [roles-set]
-  (context "/tc/rb/webusers" []
-           (POST "/list" {{:keys [page page-size fts-query]
-                           :or {page 1 page-size 10 fts-query ""}} :params}
+  (routes
+   (context "/tc/rb/webusers" []
+            (POST "/list" {{:keys [page page-size fts-query]
+                            :or {page 1 page-size 10 fts-query ""}} :params}
+                  (friend/authorize
+                   roles-set
+                   (ring.util.response/response
+                    (-> ix/webuser-select*
+                        (ix/com-pred-page* (dec page) page-size)
+
+                        (as-> query
+                            (let [fts-query (clojure.string/trim fts-query)]
+                              (if (empty? fts-query)
+                                (korma.core/order query :id :desc)
+                                (ix/webdoc-pred-search* query fts-query))))
+
+                        ix/com-exec
+                        ((partial map #(dissoc % :password)))))))
+
+
+            (GET "/find/new" []
                  (friend/authorize
                   roles-set
-                  (ring.util.response/response
-                   (-> ix/webuser-select*
-                       (ix/com-pred-page* (dec page) page-size)
-
-                       (as-> query
-                           (let [fts-query (clojure.string/trim fts-query)]
-                             (if (empty? fts-query)
-                               (korma.core/order query :id :desc)
-                               (ix/webdoc-pred-search* query fts-query))))
-
-                       ix/com-exec
-                       ((partial map #(dissoc % :password)))))))
-
-
-           (GET "/find/new" []
-                (friend/authorize
-                 roles-set
-                 (-> {}
-                     (assoc :troles-set [(ix/webrole-list) #{}])
-                     ring.util.response/response)))
-           (GET "/find/:id" [id]
-                (friend/authorize
-                 roles-set
-                 (-> id Long/parseLong
-                     ((partial ix/com-find ix/webuser))
-                     (dissoc :password)
-                     (as-> row
-                         (assoc row :troles-set [(ix/webrole-list)
-                                                 (ix/webuserwebrole-own-get-rels-set row)]))
-                     ring.util.response/response)))
-
-
-           (POST "/save" request
+                  (-> {}
+                      (assoc :troles-set [(ix/webrole-list) #{}])
+                      ring.util.response/response)))
+            (GET "/find/:id" [id]
                  (friend/authorize
                   roles-set
-                  (cw/error-response-json
-                   (let [{:keys [row user-roles-keys-set]} (request :params)
-                         row (-> row
-                                 ix/print-debug->>>
-                                 (as-> row
-                                     (if (empty? (:password row)) (dissoc row :password)
-                                         (update-in row [:password] creds/hash-bcrypt)))
-                                 ix/print-debug->>>
-                                 ((partial ix/com-save-for-id ix/webuser))
-                                 (dissoc :password))]
-
-                     (when user-roles-keys-set
-                       (println (map keyword user-roles-keys-set))
-                       (ix/webuserwebrole-add-rels-for-keynames row (map keyword user-roles-keys-set)))
-
-                     (ring.util.response/response row)))))
-
-           (POST "/change-password" request
-                 (-> request
-                     :params
-                     (update-in [:password] creds/hash-bcrypt)
-                     (assoc :username (-> request :session :cemerick.friend/identity :current))
-                     ix/webuser-save-for-username
-                     ring.util.response/response
-                     cw/error-response-json))
-
-           (POST "/delete" request
-                 (friend/authorize
-                  roles-set
+                  (-> id Long/parseLong
+                      ((partial ix/com-find ix/webuser))
+                      (dissoc :password)
+                      (as-> row
+                          (assoc row :troles-set [(ix/webrole-list)
+                                                  (ix/webuserwebrole-own-get-rels-set row)]))
+                      ring.util.response/response)))
+
+
+            (POST "/save" request
+                  (friend/authorize
+                   roles-set
+                   (cw/error-response-json
+                    (let [{:keys [row user-roles-keys-set]} (request :params)
+                          row (-> row
+                                  ix/print-debug->>>
+                                  (as-> row
+                                      (if (empty? (:password row)) (dissoc row :password)
+                                          (update-in row [:password] creds/hash-bcrypt)))
+                                  ix/print-debug->>>
+                                  ((partial ix/com-save-for-id ix/webuser))
+                                  (dissoc :password))]
+
+                      (when user-roles-keys-set
+                        (println (map keyword user-roles-keys-set))
+                        (ix/webuserwebrole-add-rels-for-keynames row (map keyword user-roles-keys-set)))
+
+                      (ring.util.response/response row)))))
+
+            (POST "/change-password" request
                   (-> request
                       :params
-                      :id
-                      ((partial ix/com-delete-for-id ix/webuser))
-                      ((fn [_] {:result "OK"}))
+                      (update-in [:password] creds/hash-bcrypt)
+                      (assoc :username (-> request :session :cemerick.friend/identity :current))
+                      ix/webuser-save-for-username
                       ring.util.response/response
-                      cw/error-response-json)))
-
-           )
+                      cw/error-response-json))
+
+            (POST "/delete" request
+                  (friend/authorize
+                   roles-set
+                   (-> request
+                       :params
+                       :id
+                       ((partial ix/com-delete-for-id ix/webuser))
+                       ((fn [_] {:result "OK"}))
+                       ring.util.response/response
+                       cw/error-response-json)))
+
+            )
+   )
   )
 
 ;; END Users reference book

;;------------------------------------------------------------------------------

(defn routes-file* [edit-roles-set]
  (defroutes routes-for-file-upload*

    (POST "/files/list" {{:keys [page page-size fts-query]
                          :or {page 1 page-size 10 fts-query ""}} :params}
          (-> ix/files-select*
              (ix/com-pred-page* (dec page) page-size)

              (as-> query
                  (let [fts-query (clojure.string/trim fts-query)]
                    (if (empty? fts-query)
                      (korma.core/order query :id :desc)
                      (ix/files-pred-search* query fts-query))))

              ix/com-exec
              ring.util.response/response))


    (multipart/wrap-multipart-params
     (POST "/files/upload" request
           (friend/authorize
            edit-roles-set
            (do
              (web-file-upload ix/file-upload (-> request :params :image-uploader))
              (ring.util.response/response "OK") ))))

    (POST "/files/edit" request
          (friend/authorize
           edit-roles-set
           (-> request
               :params
               ix/files-save
               ring.util.response/response)))

    (POST "/files/delete" request
          (friend/authorize
           edit-roles-set
           (-> request
               :params
               ix/files-delete
               ((fn [_] {:result "OK"}))
               ring.util.response/response
               cw/error-response-json)))

    ;; Кэшированный источник файлов для картинок
    (GET "/image/*" {{path :*} :params :as request}
         {:stawebuser 200
          :headers {"Cache-Control" (str "max-age=" (* 60 60 24 7)) }
          :body (clojure.java.io/file (str @ix/files-root-directory "/" path))})

    ;; Источник файлов
    (GET "/file/*" {{path :*} :params :as request}
         (clojure.java.io/file (str @ix/files-root-directory "/" path)))

    ))
;; -----------------------------------------------------------------------------

;;**************************************************************************************************

;;**************************************************************************************************

(defn routes-stext* [edit-roles-set]
  (routes
   (context "/tc/rb/stext" []

            (POST "/list" {{:keys [page page-size fts-query]
                            :or {page 1 page-size 10 fts-query ""}} :params}
                  (-> ix/stext-select*
                      (ix/com-pred-page* (dec page) page-size)

                      (as-> query
                          (let [fts-query (clojure.string/trim fts-query)]
                            (if (empty? fts-query)
                              (korma.core/order query :id :desc)
                              (ix/webdoc-pred-search* query fts-query))))

                      ix/com-exec
                      ((partial map #(dissoc % :password)))
                      ring.util.response/response))

            (GET "/find/:id" [id] (-> id
                                      Long/parseLong
                                      ((partial ix/com-find ix/stext))
                                      ring.util.response/response))

            (POST "/save" request
                  (friend/authorize
                   edit-roles-set
                   (-> request
                       :params
                       :row
                       ((partial ix/com-save-for-id ix/stext))
                       ring.util.response/response
                       cw/error-response-json))

                  ))))
;; END Spec text reference book
;;..................................................................................................



(defn routes-tag* [edit-roles-set]
  (routes
   (context "/tag" []


            (GET "/:id/path-and-chailds" [id]
                 (ring.util.response/response
                  (ix/tag-get-tree-path-and-childs
                   {:id (if (= id "root") nil (Long/parseLong id))})))

            (GET "/tree-as-flat-groups" []
                 (ring.util.response/response
                  (ix/tag-tree-as-flat-groups)))

            ;; TAGS WORK ------------------------------------------------------


            (POST "/save" request
                  (friend/authorize
                   edit-roles-set
                   (-> request
                       :params
                       ix/tag-save
                       ring.util.response/response
                       cw/error-response-json)))

            (POST "/delete" request
                  (friend/authorize
                   edit-roles-set
                   (-> request
                       :params
                       ix/tag-delete
                       ((fn [_] {:result "OK"}))
                       ring.util.response/response
                       cw/error-response-json)))

            ;; TAGS WORK ------------------------------------------------------
            ))
  )

;;**************************************************************************************************
