Skip to content

Commit

Permalink
[#200] Refactor nested (if) to (cond)
Browse files Browse the repository at this point in the history
  • Loading branch information
iperdomo committed Jan 30, 2020
1 parent b1714c1 commit aa5407e
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions api/src/clojure/org/akvo/flow_api/endpoint/sync.clj
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,22 @@
:next-sync-url (next-sync-url api-root alias cursor)})
(header "Cache-Control" "no-cache"))))

(defn get-changes
[req cursor instance-id remote-api]
(let [db (:unilog-db-connection req)
offset (Long/parseLong cursor)]
(cond
(not (unilog/valid-offset? offset db)) (anomaly/bad-request "Invalid cursor" {})
(= offset (unilog/get-cursor db)) no-more-changes
:else (changes-response offset db instance-id remote-api req))))

(defn changes [deps {:keys [alias instance-id params] :as req}]
(let [{:keys [initial cursor next]} (spec/validate-params params-spec params)]
(if (and initial (or cursor next))
(anomaly/bad-request "Invalid parameters" {})
(if (= "true" initial)
(initial-response req)
(if (and next cursor)
(let [db (:unilog-db-connection req)
offset (Long/parseLong cursor)]
(if (unilog/valid-offset? offset db)
(if (= offset (unilog/get-cursor db)) ;; end of the log
no-more-changes
(changes-response offset db instance-id (:remote-api deps) req))
(anomaly/bad-request "Invalid cursor" {})))
(anomaly/bad-request "Invalid parameters" {}))))))
(cond
(and initial (or cursor next)) (anomaly/bad-request "Invalid parameters" {})
(= "true" initial) (initial-response req)
(and next cursor) (get-changes req cursor instance-id (:remote-api deps))
:else (anomaly/bad-request "Invalid parameters" {}))))

(defn endpoint* [deps]
(GET "/sync" req
Expand Down

0 comments on commit aa5407e

Please # to comment.