193 address + hidden routes mixed

This commit is contained in:
Ivan Grishaev 2018-02-06 17:09:28 +03:00
parent 07bb457dfa
commit 50e3045a2c
1 changed files with 32 additions and 20 deletions

View File

@ -215,33 +215,45 @@
(do (do
(log/debug "/usage-metrics" user) (log/debug "/usage-metrics" user)
(ok (usage-metrics/usage-metrics-by-day)))) (ok (usage-metrics/usage-metrics-by-day))))
(context "/user" [] (context "/user" []
(GET "/" {:keys [params]} (GET "/" {:keys [params]}
:auth-rules authenticated? :auth-rules authenticated?
:current-user user :current-user user
(ok (handle-get-user user (:token params)))) (ok (handle-get-user user (:token params))))
(POST "/address" []
:auth-rules authenticated?
:body-params [user-id :- Long, address :- String]
:summary "Update user address"
(if-not (eth/valid-address? address)
(do
(log/debug "POST /address: invalid input" address)
{:status 400
:body (str "Invalid Ethereum address '" address "'")})
(let [result (users/update-user-address
user-id
address)]
(if (= 1 result)
(ok)
(internal-server-error)))))
(POST "/hidden" [] (POST "/" []
:auth-rules authenticated? :auth-rules authenticated?
:body-params [user-id :- Long, hidden :- Boolean] :current-user user
:summary "(Un)mark a user as being hidden (not visible in rating tables)." :body [body {:user-id s/Int
(db/update! :users {:is_hidden hidden} ["id = ?" user-id]) (s/optional-key :address) s/Str
(ok)) (s/optional-key :is_hidden) s/Bool}]
:summary "Updates user's fields."
(let [{:keys [user-id]} body
fields (select-keys body [:address :is_hidden])]
(when-not (= (:id user) user-id)
(log/debugf "User %s tries to update user's %s fields" (:id user) user-id)
(forbidden! (format "Cannot access a user %s" user-id)))
(when (empty? fields)
(bad-request! "No incoming fields were found."))
(when-let [address (:address fields)]
(when-not (eth/valid-address? address)
(log/debugf "POST /user: Wrong address %s" address)
(bad-request! (format "Invalid Ethereum address: %s" address))))
(db/with-trx
(when-not (db/user-exists? {:user-id user})
(not-found! "No such a user."))
(db/update! :users fields ["id = ?" user-id]))
(ok)))
(GET "/repositories" {:keys [params]} (GET "/repositories" {:keys [params]}
:auth-rules authenticated? :auth-rules authenticated?