Delete database.cljs

This commit is contained in:
virvar 2016-03-21 15:20:11 +03:00
parent b21b4ecc1a
commit c95ecf5363
3 changed files with 31 additions and 47 deletions

View File

@ -3,7 +3,7 @@
(:require [cljs.core.async :as async :refer [chan put! <! >!]]
[messenger.state :as state]
[messenger.utils.utils :refer [log toast http-post]]
[messenger.utils.database :as db]))
[messenger.persistence.realm :as realm]))
(def fake-contacts? false)
@ -40,6 +40,9 @@
(js->clj raw-contacts :keywordize-keys true)))}))))
ch))
(defn- get-contacts []
(realm/get-list "Contact"))
(defn load-syng-contacts []
(let [contacts (map (fn [contact]
(merge contact
@ -47,9 +50,29 @@
:datetime "15:30"
:new-messages-count (rand-int 3)
:online (< (rand) 0.5)}))
(db/get-contacts))]
(get-contacts))]
(swap! state/app-state update :contacts-ds
#(clone-with-rows % contacts))))
(defn- create-contact [{:keys [phone-number whisper-identity name photo-path]}]
(realm/create "Contact"
{:phone-number phone-number
:whisper-identity whisper-identity
:name (or name "")
:photo-path (or photo-path "")}))
(defn- contact-exist? [contacts contact]
(some #(= (:phone-number contact) (:phone-number %)) contacts))
(defn- add-contacts [contacts]
(realm/write (fn []
(let [db-contacts (get-contacts)]
(dorun (map (fn [contact]
(if (not (contact-exist? db-contacts contact))
(create-contact contact)
;; TODO else override?
))
contacts))))))
(defn save-syng-contacts [syng-contacts]
(db/add-contacts syng-contacts))
(add-contacts syng-contacts))

View File

@ -75,5 +75,8 @@
(> (.-length (get-by-field schema-name field value))
0))
(comment
)
(defn get-count [objs]
(.-length objs))
(defn get-list [schema-name]
(vals (js->clj (.objects realm schema-name) :keywordize-keys true)))

View File

@ -1,42 +0,0 @@
(ns messenger.utils.database
(:require [messenger.utils.utils :refer [log toast http-post]]
[messenger.persistence.realm :refer [realm]]))
(defn write [f]
(.write realm f))
(defn get-contacts-objects []
(.objects realm "Contact"))
(defn get-contacts []
(vals (js->clj (get-contacts-objects) :keywordize-keys true)))
(defn delete-contacts []
(write (fn []
(.delete realm (get-contacts-objects)))))
(defn filtered [objs query]
(.filtered objs query))
(defn get-count [objs]
(.-length objs))
(defn create-contact [{:keys [phone-number whisper-identity name photo-path]}]
(.create realm "Contact"
(clj->js {:phone-number phone-number
:whisper-identity whisper-identity
:name (or name "")
:photo-path (or photo-path "")})))
(defn contact-exist? [contacts contact]
(some #(= (:phone-number contact) (:phone-number %)) contacts))
(defn add-contacts [contacts]
(write (fn []
(let [db-contacts (get-contacts)]
(dorun (map (fn [contact]
(if (not (contact-exist? db-contacts contact))
(create-contact contact)
;; TODO else override?
))
contacts))))))