From cad95c67a57168c895acd72f2a8b5b180c11f042 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sat, 2 Apr 2016 11:53:39 +0300 Subject: [PATCH] Reorganize repo code. --- src/uxbox/data/auth.cljs | 2 +- src/uxbox/data/history.cljs | 6 +++--- src/uxbox/data/pages.cljs | 10 +++++----- src/uxbox/data/projects.cljs | 6 +++--- src/uxbox/data/users.cljs | 6 +++--- src/uxbox/repo.cljs | 9 +++++---- src/uxbox/repo/auth.cljs | 8 ++++---- src/uxbox/repo/core.cljs | 4 ++-- src/uxbox/repo/pages.cljs | 18 +++++++++--------- src/uxbox/repo/projects.cljs | 8 ++++---- src/uxbox/repo/users.cljs | 24 ++++++++++++++++++++++++ 11 files changed, 63 insertions(+), 38 deletions(-) create mode 100644 src/uxbox/repo/users.cljs diff --git a/src/uxbox/data/auth.cljs b/src/uxbox/data/auth.cljs index 327b52d98..e45a3d205 100644 --- a/src/uxbox/data/auth.cljs +++ b/src/uxbox/data/auth.cljs @@ -53,7 +53,7 @@ (let [params {:username username :password password :scope "webapp"}] - (->> (rp/do :fetch/token params) + (->> (rp/req :fetch/token params) (rx/catch on-error) (rx/map :payload) (rx/mapcat #(rx/of (logged-in %) diff --git a/src/uxbox/data/history.cljs b/src/uxbox/data/history.cljs index c03678500..a75fd8179 100644 --- a/src/uxbox/data/history.cljs +++ b/src/uxbox/data/history.cljs @@ -39,7 +39,7 @@ (uum/error (tr "errors.fetch-page-history")) (rx/empty))] (let [params {:page id :pinned true}] - (->> (rp/do :fetch/page-history params) + (->> (rp/req :fetch/page-history params) (rx/map on-success) (rx/catch on-failure)))))) @@ -74,7 +74,7 @@ (let [params (merge {:page id :max (or max 15)} (when since {:since since}))] - (->> (rp/do :fetch/page-history params) + (->> (rp/req :fetch/page-history params) (rx/map on-success) (rx/catch on-failure)))))) @@ -211,7 +211,7 @@ (uum/error (tr "errors.page-history-update")) (rx/empty))] (rx/merge - (->> (rp/do :update/page-history item) + (->> (rp/req :update/page-history item) (rx/map on-success) (rx/catch on-failure)) (->> (rx/filter history-updated? s) diff --git a/src/uxbox/data/pages.cljs b/src/uxbox/data/pages.cljs index 181685be7..f200be6ed 100644 --- a/src/uxbox/data/pages.cljs +++ b/src/uxbox/data/pages.cljs @@ -48,7 +48,7 @@ (on-error [err] (js/console.error err) (rx/empty))] - (->> (rp/do :fetch/pages-by-project {:project projectid}) + (->> (rp/req :fetch/pages-by-project {:project projectid}) (rx/map on-loaded) (rx/catch on-error))))) @@ -70,7 +70,7 @@ (rx/empty))] (let [params (-> (into {} this) (assoc :data {}))] - (->> (rp/do :create/page params) + (->> (rp/req :create/page params) (rx/mapcat on-created) (rx/catch on-failed)))))) @@ -108,7 +108,7 @@ (uum/error (tr "errors.page-update")) (rx/empty))] (let [page (stpr/pack-page state id)] - (->> (rp/do :update/page page) + (->> (rp/req :update/page page) (rx/map on-success) (rx/catch on-failure)))))) @@ -166,7 +166,7 @@ (on-failure [e] (uum/error (tr "errors.page-update")) (rx/empty))] - (->> (rp/do :update/page-metadata (into {} this)) + (->> (rp/req :update/page-metadata (into {} this)) (rx/map on-success) (rx/catch on-failure))))) @@ -191,7 +191,7 @@ (on-failure [e] (uum/error (tr "errors.delete-page")) (rx/empty))] - (->> (rp/do :delete/page id) + (->> (rp/req :delete/page id) (rx/map on-success) (rx/tap callback) (rx/filter identity) diff --git a/src/uxbox/data/projects.cljs b/src/uxbox/data/projects.cljs index 47b4c37ca..b1e9e0dd2 100644 --- a/src/uxbox/data/projects.cljs +++ b/src/uxbox/data/projects.cljs @@ -62,7 +62,7 @@ #(reduce stpr/assoc-project % projects)) (on-error [err] (rx/empty))] - (->> (rp/do :fetch/projects) + (->> (rp/req :fetch/projects) (rx/map on-loaded) (rx/catch on-error))))) @@ -83,7 +83,7 @@ :data nil)))) (on-failure [err] (uum/error (tr "errors.create-project")))] - (->> (rp/do :create/project {:name name}) + (->> (rp/req :create/project {:name name}) (rx/mapcat on-success) (rx/catch on-failure))))) @@ -107,7 +107,7 @@ (rs/swap #(stpr/dissoc-project % id))) (on-failure [e] (uum/error (tr "errors.delete-project")))] - (->> (rp/do :delete/project id) + (->> (rp/req :delete/project id) (rx/map on-success) (rx/catch on-failure))))) diff --git a/src/uxbox/data/users.cljs b/src/uxbox/data/users.cljs index c14f02374..a7a5b56e1 100644 --- a/src/uxbox/data/users.cljs +++ b/src/uxbox/data/users.cljs @@ -33,7 +33,7 @@ (letfn [(on-error [err] (uum/error (tr "errors.profile-fetch")) (rx/empty))] - (->> (rp/do :fetch/profile) + (->> (rp/req :fetch/profile) (rx/catch on-error) (rx/map :payload) (rx/map profile-fetched))))) @@ -50,7 +50,7 @@ (letfn [(on-error [err] (uum/error (tr "errors.update-profile")) (rx/empty))] - (->> (rp/do :update/profile data) + (->> (rp/req :update/profile data) (rx/catch on-error) (rx/map :payload) (rx/map profile-fetched))))) @@ -67,7 +67,7 @@ ;; (letfn [(on-error [err] ;; (uum/error (tr "errors.update-password")) ;; (rx/empty))] - ;; (->> (rp/do :update/password data) + ;; (->> (rp/req :update/password data) ;; (rx/catch on-error))))) (js/alert "Not implemented") (rx/empty))) diff --git a/src/uxbox/repo.cljs b/src/uxbox/repo.cljs index f07e055ee..c4cb39438 100644 --- a/src/uxbox/repo.cljs +++ b/src/uxbox/repo.cljs @@ -7,16 +7,17 @@ (ns uxbox.repo "A main interface for access to remote resources." (:refer-clojure :exclude [do]) - (:require [uxbox.repo.core :refer (-do)] + (:require [uxbox.repo.core :refer (request)] [uxbox.repo.auth] + [uxbox.repo.users] [uxbox.repo.projects] [uxbox.repo.pages] [beicon.core :as rx])) -(defn do +(defn req "Perform a side effectfull action accesing remote resources." ([type] - (-do type nil)) + (request type nil)) ([type data] - (-do type data))) + (request type data))) diff --git a/src/uxbox/repo/auth.cljs b/src/uxbox/repo/auth.cljs index 8a4f6027f..a898d5aeb 100644 --- a/src/uxbox/repo/auth.cljs +++ b/src/uxbox/repo/auth.cljs @@ -8,15 +8,15 @@ "A main interface for access to remote resources." (:refer-clojure :exclude [do]) (:require [beicon.core :as rx] - [uxbox.repo.core :refer (-do url send!)] + [uxbox.repo.core :refer (request url send!)] [uxbox.state :as ust])) -(defmethod -do :fetch/profile +(defmethod request :fetch/profile [type _] (let [url (str url "/profile/me")] (send! {:method :get :url url}))) -(defmethod -do :fetch/token +(defmethod request :fetch/token [type data] (let [url (str url "/auth/token")] (send! {:url url @@ -24,7 +24,7 @@ :auth false :body data}))) -(defmethod -do :update/profile +(defmethod request :update/profile [type data] (let [params {:url (str url "/profile/me") :method :put diff --git a/src/uxbox/repo/core.cljs b/src/uxbox/repo/core.cljs index d29c69105..3f152186a 100644 --- a/src/uxbox/repo/core.cljs +++ b/src/uxbox/repo/core.cljs @@ -78,9 +78,9 @@ (rx/map conditional-decode) (rx/mapcat handle-http-status)))) -(defmulti -do +(defmulti request (fn [type data] type)) -(defmethod -do :default +(defmethod request :default [type data] (throw (ex-info (str "No implementation found for " type) {:data data}))) diff --git a/src/uxbox/repo/pages.cljs b/src/uxbox/repo/pages.cljs index 6ded28c94..335f23bd7 100644 --- a/src/uxbox/repo/pages.cljs +++ b/src/uxbox/repo/pages.cljs @@ -7,55 +7,55 @@ (ns uxbox.repo.pages "A main interface for access to remote resources." (:require [beicon.core :as rx] - [uxbox.repo.core :refer (-do url send!)] + [uxbox.repo.core :refer (request url send!)] [uxbox.state :as ust])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Login ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(defmethod -do :fetch/pages +(defmethod request :fetch/pages [type data] (send! {:url (str url "/pages") :method :get})) -(defmethod -do :fetch/pages-by-project +(defmethod request :fetch/pages-by-project [type {:keys [project] :as params}] (let [url (str url "/projects/" project "/pages")] (send! {:method :get :url url}))) -(defmethod -do :fetch/page-history +(defmethod request :fetch/page-history [type {:keys [page] :as params}] (let [url (str url "/pages/" page "/history") query (select-keys params [:max :since :pinned])] (send! {:method :get :url url :query query }))) -(defmethod -do :delete/page +(defmethod request :delete/page [_ id] (let [url (str url "/pages/" id)] (send! {:url url :method :delete}))) -(defmethod -do :create/page +(defmethod request :create/page [type {:keys [id] :as data}] (let [params {:url (str url "/pages") :method :post :body data}] (send! params))) -(defmethod -do :update/page +(defmethod request :update/page [type {:keys [id] :as data}] (let [params {:url (str url "/pages/" id) :method :put :body data}] (send! params))) -(defmethod -do :update/page-history +(defmethod request :update/page-history [type {:keys [id page] :as data}] (let [params {:url (str url "/pages/" page "/history/" id) :method :put :body data}] (send! params))) -(defmethod -do :update/page-metadata +(defmethod request :update/page-metadata [type {:keys [id] :as data}] (let [params {:url (str url "/pages/" id "/metadata") :method :put diff --git a/src/uxbox/repo/projects.cljs b/src/uxbox/repo/projects.cljs index 1d270c4aa..f8d8caf98 100644 --- a/src/uxbox/repo/projects.cljs +++ b/src/uxbox/repo/projects.cljs @@ -7,26 +7,26 @@ (ns uxbox.repo.projects "A main interface for access to remote resources." (:require [beicon.core :as rx] - [uxbox.repo.core :refer (-do url send!)] + [uxbox.repo.core :refer (request url send!)] [uxbox.state :as ust])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Login ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(defmethod -do :fetch/projects +(defmethod request :fetch/projects [type data] (let [url (str url "/projects")] (send! {:url url :method :get}))) -(defmethod -do :create/project +(defmethod request :create/project [_ data] (let [params {:url (str url "/projects") :method :post :body data}] (send! params))) -(defmethod -do :delete/project +(defmethod request :delete/project [_ id] (let [url (str url "/projects/" id)] (send! {:url url :method :delete}))) diff --git a/src/uxbox/repo/users.cljs b/src/uxbox/repo/users.cljs new file mode 100644 index 000000000..8a155ab39 --- /dev/null +++ b/src/uxbox/repo/users.cljs @@ -0,0 +1,24 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) 2016 Andrey Antukh + +(ns uxbox.repo.users + "A main interface for access to remote resources." + (:refer-clojure :exclude [do]) + (:require [beicon.core :as rx] + [uxbox.repo.core :refer (request url send!)] + [uxbox.state :as ust])) + +(defmethod request :fetch/profile + [type _] + (let [url (str url "/profile/me")] + (send! {:method :get :url url}))) + +(defmethod request :update/profile + [type data] + (let [params {:url (str url "/profile/me") + :method :put + :body data}] + (send! params)))