 ;;**************************************************************************************************
 
 (defn routes-ix* [cms-roles-set]
-  (defroutes routes-for-ix-pathes*
-
-    (ANY "/ix/set" request
-         (cw/ix-accoc-parametr request))
-
-    (GET "/ixinfestor" request
-         (friend/authorize
-          cms-roles-set
-          (-> request
-              cwb/page-ixcms-main
-              ring.util.response/response
-              (ring.util.response/header "Content-Type" "text/html; charset=utf-8"))
-          )
-         )
-
-    ))
+  (routes routes-for-ix-pathes*
+
+          (ANY "/ix/set" request
+               (cw/ix-accoc-parametr request))
+
+          (GET "/ixinfestor" request
+               (friend/authorize
+                cms-roles-set
+                (-> request
+                    cwb/page-ixcms-main
+                    ring.util.response/response
+                    (ring.util.response/header "Content-Type" "text/html; charset=utf-8"))
+                )
+               )
+
+          ))
 
 
 

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

(defn routes-stext* [edit-roles-set]
  (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]
  (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 ------------------------------------------------------
           )
  )

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

                             context-path "/tc/rb/webdocs"
                             webdoc-save-fn ix/webdoc-save}
                        :as init-row}]
  (routes
   (context context-path []

            (POST "/bytag" {{:keys [tag-id page page-size fts-query]
                             :or {page 1 page-size 10 fts-query ""}} :params}
                  (do
                    (-> webdoc-select* ;;; SRC
                        (ix/webdoc-pred-search-for-the-child-tree-tags*
                         {:id (if (= tag-id "root") nil tag-id)})

                        (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-pred-page* (dec page) page-size)
                        ix/com-exec
                        korma.db/transaction
                        ring.util.response/response
                        cw/error-response-json)))

            (GET "/edit/new" [] (ring.util.response/response {}))
            (GET "/edit/:id" [id]
                 ;;TODO: Попробовать сделать все в рамках одной транзакции
                 (let [id (Long/parseLong id)]
                   (-> {:webdoc-row (ix/com-find webdoc-entity id)  ;;; SRC
                        }
                       ring.util.response/response
                       cw/error-response-json)))

            (POST "/webdoctags-edit-table" request
                  (-> request
                      :params
                      (ix/webdoctag-tag-tree-as-flat-groups-with-patches :path)
                      ring.util.response/response
                      cw/error-response-json))

            (POST "/save" {{:keys [webdoc-row webdoctag-ids-for-updating swops]} :params}
                  (friend/authorize
                   edit-roles-set
                   (cw/error-response-json
                    (let [new-webdoc-row (when webdoc-row
                                           (-> webdoc-row
                                             ;;; SPEC HANDLER
                                               ;;(update-in-if-not-nil? [:price] bigdec)
                                               ;;(update-in-if-not-nil? [:orderindex] parseLong)
                                               ;;(update-in-if-not-nil? [:showbdate] #(new java.util.Date %))
                                               ;;(update-in-if-not-nil? [:showedate] #(new java.util.Date %))
                                               covertors-fn
                                               webdoc-save-fn
                                               ))]

                      (ring.util.response/response {:result-code 0
                                                    :webdoc-row new-webdoc-row
                                                    :webdoctag-rows (when (and new-webdoc-row
                                                                               webdoctag-ids-for-updating)
                                                                      (->> webdoctag-ids-for-updating
                                                                           (map (partial assoc {} :id))
                                                                           ((partial ix/webdoctag-update-tags new-webdoc-row))
                                                                           doall))
                                                    })))))

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

            (GET "/images-list" request
                 (-> request
                     :params
                     (update-in [:id] parseLong)
                     (ix/files_rel-select-files-by-* :webdoc_id)
                     ix/file-pred-images*
                     ix/com-exec
                     ring.util.response/response
                     cw/error-response-json))

            (GET "/files-list" request
                 (-> request
                     :params
                     (update-in [:id] parseLong)
                     (ix/files_rel-select-files-by-* :webdoc_id)
                     (ix/file-pred-images* :not-image)
                     ix/com-exec
                     ring.util.response/response
                     cw/error-response-json))

            (context "/upload/:id" [id]

                     (multipart/wrap-multipart-params
                      (POST "/image/avatar" request
                            (friend/authorize
                             edit-roles-set
                             (ring.util.response/response
                              (let [id (parseLong id)
                                    [{path :path} _] (web-file-upload
                                                      (partial ix/file-upload-rel-on webdoc-entity :webdoc_id {:id id})
                                                      (-> request :params :file-uploader))]
                                (ix/webdoc-save {:id id :web_title_image path})
                                "OK")))))

                     (multipart/wrap-multipart-params
                      (POST "/image" request
                            (friend/authorize
                             edit-roles-set
                             (ring.util.response/response
                              (do (web-file-upload
                                   (partial ix/file-upload-rel-on webdoc-entity :webdoc_id {:id (parseLong id)})
                                   (-> request :params :image-uploader))
                                  "OK")))))

                     (multipart/wrap-multipart-params
                      (POST "/file" request
                            (friend/authorize
                             edit-roles-set
                             (ring.util.response/response
                              (do
                                (web-file-upload
                                 (partial ix/file-upload-rel-on webdoc-entity :webdoc_id {:id (parseLong id)})
                                 (-> request :params :file-uploader))
                                "OK")))))

                     )

            (POST "/files_rel/delete" {{:keys [webdoc-id file-id]} :params}
                  (friend/authorize
                   edit-roles-set
                   (-> (ix/files_rel-delete :webdoc_id {:id file-id} {:id webdoc-id})
                       ring.util.response/response
                       cw/error-response-json))
                  ))
   )
  )

;; END Products reference book
