summaryrefslogtreecommitdiffstats
path: root/store
diff options
context:
space:
mode:
authorChristopher Speller <crspeller@gmail.com>2015-10-16 08:16:40 -0400
committerChristopher Speller <crspeller@gmail.com>2015-10-16 08:16:40 -0400
commit099d8538770271bfc41416b871a899362bf9a086 (patch)
tree103ecc42fe274694d387a4e5969f860767594139 /store
parent269476d694f425764dbe0cccd4eaa3f23956ba61 (diff)
parent327b0b5a2119ae888c812f682b3934061b8f59bf (diff)
downloadchat-099d8538770271bfc41416b871a899362bf9a086.tar.gz
chat-099d8538770271bfc41416b871a899362bf9a086.tar.bz2
chat-099d8538770271bfc41416b871a899362bf9a086.zip
Merge pull request #1079 from hmhealey/initprefs
Added an initial call to get all user preferences on page load
Diffstat (limited to 'store')
-rw-r--r--store/sql_preference_store.go27
-rw-r--r--store/sql_preference_store_test.go48
-rw-r--r--store/store.go1
3 files changed, 76 insertions, 0 deletions
diff --git a/store/sql_preference_store.go b/store/sql_preference_store.go
index dbbe20119..bf6e030bf 100644
--- a/store/sql_preference_store.go
+++ b/store/sql_preference_store.go
@@ -212,3 +212,30 @@ func (s SqlPreferenceStore) GetCategory(userId string, category string) StoreCha
return storeChannel
}
+
+func (s SqlPreferenceStore) GetAll(userId string) StoreChannel {
+ storeChannel := make(StoreChannel)
+
+ go func() {
+ result := StoreResult{}
+
+ var preferences model.Preferences
+
+ if _, err := s.GetReplica().Select(&preferences,
+ `SELECT
+ *
+ FROM
+ Preferences
+ WHERE
+ UserId = :UserId`, map[string]interface{}{"UserId": userId}); err != nil {
+ result.Err = model.NewAppError("SqlPreferenceStore.GetAll", "We encounted an error while finding preferences", err.Error())
+ } else {
+ result.Data = preferences
+ }
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}
diff --git a/store/sql_preference_store_test.go b/store/sql_preference_store_test.go
index 76b1bcb17..e68203cc3 100644
--- a/store/sql_preference_store_test.go
+++ b/store/sql_preference_store_test.go
@@ -144,3 +144,51 @@ func TestPreferenceGetCategory(t *testing.T) {
t.Fatal("shouldn't have got any preferences")
}
}
+
+func TestPreferenceGetAll(t *testing.T) {
+ Setup()
+
+ userId := model.NewId()
+ category := model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW
+ name := model.NewId()
+
+ preferences := model.Preferences{
+ {
+ UserId: userId,
+ Category: category,
+ Name: name,
+ },
+ // same user/category, different name
+ {
+ UserId: userId,
+ Category: category,
+ Name: model.NewId(),
+ },
+ // same user/name, different category
+ {
+ UserId: userId,
+ Category: model.NewId(),
+ Name: name,
+ },
+ // same name/category, different user
+ {
+ UserId: model.NewId(),
+ Category: category,
+ Name: name,
+ },
+ }
+
+ Must(store.Preference().Save(&preferences))
+
+ if result := <-store.Preference().GetAll(userId); result.Err != nil {
+ t.Fatal(result.Err)
+ } else if data := result.Data.(model.Preferences); len(data) != 3 {
+ t.Fatal("got the wrong number of preferences")
+ } else {
+ for i := 0; i < 3; i++ {
+ if data[0] != preferences[i] && data[1] != preferences[i] && data[2] != preferences[i] {
+ t.Fatal("got incorrect preferences")
+ }
+ }
+ }
+}
diff --git a/store/store.go b/store/store.go
index b436a5c40..de335cc2b 100644
--- a/store/store.go
+++ b/store/store.go
@@ -156,4 +156,5 @@ type PreferenceStore interface {
Save(preferences *model.Preferences) StoreChannel
Get(userId string, category string, name string) StoreChannel
GetCategory(userId string, category string) StoreChannel
+ GetAll(userId string) StoreChannel
}