Update user address

This commit is contained in:
kagel 2016-08-23 02:27:49 +03:00
parent 8cd7f7705c
commit 601465ad39
8 changed files with 125 additions and 48 deletions

View File

@ -4,6 +4,30 @@ html, body {
padding-top: 0px; padding-top: 0px;
} }
.app {
width: 100%;
padding: 0;
margin: 0;
max-width: 100%;
}
.container {
padding-top: 20px;
}
.profile-settings {
padding-top: 20px;
.form-group {
padding-top: 20px;
}
button {
margin-top: 20px;
}
}
.profile-link { .profile-link {
display: inline-block; display: inline-block;
padding-top: .425rem; padding-top: .425rem;

View File

@ -13,7 +13,7 @@ WHERE id = :id;
-- :name update-user-address! :! :n -- :name update-user-address! :! :n
UPDATE users UPDATE users
SET address = :address SET address = :address
WHERE id = :id; WHERE login = :login;
-- :name get-user :? :1 -- :name get-user :? :1
-- :doc retrieve a user given the login. -- :doc retrieve a user given the login.

View File

@ -24,3 +24,8 @@
[login] [login]
(jdbc/with-db-connection [con-db *db*] (jdbc/with-db-connection [con-db *db*]
(some? (db/get-user con-db {:login login})))) (some? (db/get-user con-db {:login login}))))
(defn update-user-address
[login address]
(jdbc/with-db-connection [con-db *db*]
(db/update-user-address! con-db {:login login :address address})))

View File

@ -4,7 +4,8 @@
[schema.core :as s] [schema.core :as s]
[compojure.api.meta :refer [restructure-param]] [compojure.api.meta :refer [restructure-param]]
[buddy.auth.accessrules :refer [restrict]] [buddy.auth.accessrules :refer [restrict]]
[buddy.auth :refer [authenticated?]])) [buddy.auth :refer [authenticated?]]
[commiteth.db.users :as users]))
(defn access-error [_ _] (defn access-error [_ _]
(unauthorized {:error "unauthorized"})) (unauthorized {:error "unauthorized"}))
@ -24,43 +25,21 @@
(defapi service-routes (defapi service-routes
{:swagger {:ui "/swagger-ui" {:swagger {:ui "/swagger-ui"
:spec "/swagger.json" :spec "/swagger.json"
:data {:info {:version "1.0.0" :data {:info {:version "0.1"
:title "Sample API" :title "commitETH API"
:description "Sample Services"}}}} :description "commitETH API"}}}}
(GET "/authenticated" [] (GET "/authenticated" []
:auth-rules authenticated? :auth-rules authenticated?
:current-user user :current-user user
(ok {:user user})) (ok {:user user}))
(context "/api" [] (context "/api" []
:tags ["thingie"] (POST "/user/address" []
:auth-rules authenticated?
(GET "/plus" [] :body-params [user :- String, address :- String]
:return Long :summary "Update user address"
:query-params [x :- Long, {y :- Long 1}] (let [result (users/update-user-address user address)]
:summary "x+y with query-parameters. y defaults to 1." (if (= 1 result)
(ok (+ x y))) (ok)
(internal-server-error))))))
(POST "/minus" []
:return Long
:body-params [x :- Long, y :- Long]
:summary "x-y with body-parameters."
(ok (- x y)))
(GET "/times/:x/:y" []
:return Long
:path-params [x :- Long, y :- Long]
:summary "x*y with path-parameters"
(ok (* x y)))
(POST "/divide" []
:return Double
:form-params [x :- Long, y :- Long]
:summary "x/y with form-parameters"
(ok (/ x y)))
(GET "/power" []
:return Long
:header-params [x :- Long, y :- Long]
:summary "x^y with header-parameters"
(ok (long (Math/pow x y))))))

View File

@ -8,7 +8,8 @@
[ajax.core :refer [GET POST]] [ajax.core :refer [GET POST]]
[commiteth.ajax :refer [load-interceptors!]] [commiteth.ajax :refer [load-interceptors!]]
[commiteth.handlers] [commiteth.handlers]
[commiteth.subscriptions]) [commiteth.subscriptions]
[reagent.core :as reagent])
(:import goog.History)) (:import goog.History))
(defn nav-link [uri title page collapsed?] (defn nav-link [uri title page collapsed?]
@ -21,8 +22,9 @@
(defn login-link [] (defn login-link []
(let [user (rf/subscribe [:user])] (let [user (rf/subscribe [:user])]
(if-let [login (:login @user)] (if-let [login (:login @user)]
[:li.pull-right [:li.pull-right.p
[:span.profile-link (str "Logged in as " login)] [:span.profile-link "Logged in as "
[:a {:href "/#/profile"} login]]
[:a.btn.btn-primary.btn-sm {:href "/logout"} "Logout"]] [:a.btn.btn-primary.btn-sm {:href "/logout"} "Logout"]]
[:li.pull-right [:li.pull-right
[:a.btn.btn-social.btn-github [:a.btn.btn-social.btn-github
@ -43,17 +45,53 @@
[login-link]]]])) [login-link]]]]))
(defn home-page [] (defn home-page []
[:div.container [:div
[:div.jumbotron [:h3 "Welcome to commitETH"]])
[:h3 "Welcome to commitETH"]]])
(defn input [{:keys [value-path]}]
(let [val (reagent/atom nil)
save #(let [v (-> @val str clojure.string/trim)]
(when (seq v)
(rf/dispatch [:assoc-in value-path v])))]
(fn [props]
[:input.form-control
(merge props {:type "text"
:value @val
:auto-focus true
:on-blur save
:on-change #(reset! val (-> % .-target .-value))})])))
(defn save-address
[login address]
(fn [_]
(rf/dispatch [:save-user-address login address])))
(defn address-settings []
(let [user (rf/subscribe [:user])
login (:login @user)
address (rf/subscribe [:address])]
(fn []
[:div.form-group
[:label "Address"]
[input {:placeholder "Address"
:value-path [:address]}]
[:button.btn.btn-primary.btn-lg
{:on-click (save-address login @address)}
"Save"]])))
(defn profile-page []
[:div.profile-settings
[:h1 "Profile"]
[address-settings]])
(def pages (def pages
{:home #'home-page}) {:home #'home-page
:profile #'profile-page})
(defn page [] (defn page []
[:div [:div.app
[navbar] [navbar]
[(pages @(rf/subscribe [:page]))]]) [:div.container [(pages @(rf/subscribe [:page]))]]])
;; ------------------------- ;; -------------------------
;; Routes ;; Routes
@ -61,6 +99,8 @@
(secretary/defroute "/" [] (secretary/defroute "/" []
(rf/dispatch [:set-active-page :home])) (rf/dispatch [:set-active-page :home]))
(secretary/defroute "/profile" []
(rf/dispatch [:set-active-page :profile]))
;; ------------------------- ;; -------------------------
;; History ;; History

View File

@ -1,5 +1,6 @@
(ns commiteth.db) (ns commiteth.db)
(def default-db (def default-db
{:page :home {:page :home
:user nil}) :user nil
:address nil})

View File

@ -1,12 +1,18 @@
(ns commiteth.handlers (ns commiteth.handlers
(:require [commiteth.db :as db] (:require [commiteth.db :as db]
[re-frame.core :refer [dispatch reg-event-db]])) [re-frame.core :refer [dispatch reg-event-db]]
[ajax.core :refer [GET POST]]))
(reg-event-db (reg-event-db
:initialize-db :initialize-db
(fn [_ _] (fn [_ _]
db/default-db)) db/default-db))
(reg-event-db
:assoc-in
(fn [db [_ path value]]
(assoc-in db path value)))
(reg-event-db (reg-event-db
:set-active-page :set-active-page
(fn [db [_ page]] (fn [db [_ page]]
@ -16,3 +22,20 @@
:set-active-user :set-active-user
(fn [db [_ user]] (fn [db [_ user]]
(assoc db :user user))) (assoc db :user user)))
(reg-event-db
:set-user-address
(fn [db [_ address]]
(assoc db :address address)))
(defn save-user-address [params]
(POST "/api/user/address"
{:headers {"Accept" "application/transit+json"}
:params params
:handler #(println %)}))
(reg-event-db
:save-user-address
(fn [db [_ user address]]
(save-user-address {:user user :address address})
db))

View File

@ -10,3 +10,8 @@
:user :user
(fn [db _] (fn [db _]
(:user db))) (:user db)))
(reg-sub
:address
(fn [db _]
(:address db)))