diff options
author | hmhealey <harrisonmhealey@gmail.com> | 2015-10-13 11:52:17 -0400 |
---|---|---|
committer | hmhealey <harrisonmhealey@gmail.com> | 2015-10-13 12:29:31 -0400 |
commit | 2a39e8dbfab8506b09d0d030f87cac4c079b975a (patch) | |
tree | dd2ca36ca7ef73a2cf8eda11d6a62848d7d29a79 /store | |
parent | 5cecf1afcd1d8e561a5b2d840e5bd1b588a74a27 (diff) | |
download | chat-2a39e8dbfab8506b09d0d030f87cac4c079b975a.tar.gz chat-2a39e8dbfab8506b09d0d030f87cac4c079b975a.tar.bz2 chat-2a39e8dbfab8506b09d0d030f87cac4c079b975a.zip |
Removed Preference.AltId
Diffstat (limited to 'store')
-rw-r--r-- | store/sql_preference_store.go | 51 | ||||
-rw-r--r-- | store/sql_preference_store_test.go | 75 | ||||
-rw-r--r-- | store/store.go | 3 |
3 files changed, 89 insertions, 40 deletions
diff --git a/store/sql_preference_store.go b/store/sql_preference_store.go index 04bb5e4ee..59c52d888 100644 --- a/store/sql_preference_store.go +++ b/store/sql_preference_store.go @@ -17,11 +17,10 @@ func NewSqlPreferenceStore(sqlStore *SqlStore) PreferenceStore { s := &SqlPreferenceStore{sqlStore} for _, db := range sqlStore.GetAllConns() { - table := db.AddTableWithName(model.Preference{}, "Preferences").SetKeys(false, "UserId", "Category", "Name", "AltId") + table := db.AddTableWithName(model.Preference{}, "Preferences").SetKeys(false, "UserId", "Category", "Name") table.ColMap("UserId").SetMaxSize(26) table.ColMap("Category").SetMaxSize(32) table.ColMap("Name").SetMaxSize(32) - table.ColMap("AltId").SetMaxSize(26) table.ColMap("Value").SetMaxSize(128) } @@ -49,7 +48,7 @@ func (s SqlPreferenceStore) Save(preferences *model.Preferences) StoreChannel { result.Err = model.NewAppError("SqlPreferenceStore.Save", "Unable to open transaction to save preferences", err.Error()) } else { for _, preference := range *preferences { - if upsertResult := s.save(transaction, preference); upsertResult.Err != nil { + if upsertResult := s.save(transaction, &preference); upsertResult.Err != nil { result = upsertResult break } @@ -87,7 +86,6 @@ func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *mode "UserId": preference.UserId, "Category": preference.Category, "Name": preference.Name, - "AltId": preference.AltId, "Value": preference.Value, } @@ -95,9 +93,9 @@ func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *mode if _, err := transaction.Exec( `INSERT INTO Preferences - (UserId, Category, Name, AltId, Value) + (UserId, Category, Name, Value) VALUES - (:UserId, :Category, :Name, :AltId, :Value) + (:UserId, :Category, :Name, :Value) ON DUPLICATE KEY UPDATE Value = :Value`, params); err != nil { result.Err = model.NewAppError("SqlPreferenceStore.save", "We encountered an error while updating preferences", err.Error()) @@ -112,8 +110,7 @@ func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *mode WHERE UserId = :UserId AND Category = :Category - AND Name = :Name - AND AltId = :AltId`, params) + AND Name = :Name`, params) if err != nil { result.Err = model.NewAppError("SqlPreferenceStore.save", "We encountered an error while updating preferences", err.Error()) return result @@ -137,11 +134,11 @@ func (s SqlPreferenceStore) insert(transaction *gorp.Transaction, preference *mo if err := transaction.Insert(preference); err != nil { if IsUniqueConstraintError(err.Error(), "UserId", "preferences_pkey") { - result.Err = model.NewAppError("SqlPreferenceStore.insert", "A preference with that user id, category, name, and alt id already exists", - "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) + result.Err = model.NewAppError("SqlPreferenceStore.insert", "A preference with that user id, category, and name already exists", + "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", "+err.Error()) } else { result.Err = model.NewAppError("SqlPreferenceStore.insert", "We couldn't save the preference", - "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) + "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", "+err.Error()) } } @@ -153,13 +150,13 @@ func (s SqlPreferenceStore) update(transaction *gorp.Transaction, preference *mo if _, err := transaction.Update(preference); err != nil { result.Err = model.NewAppError("SqlPreferenceStore.update", "We couldn't update the preference", - "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) + "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", "+err.Error()) } return result } -func (s SqlPreferenceStore) GetByName(userId string, category string, name string) StoreChannel { +func (s SqlPreferenceStore) Get(userId string, category string, name string) StoreChannel { storeChannel := make(StoreChannel) go func() { @@ -178,6 +175,34 @@ func (s SqlPreferenceStore) GetByName(userId string, category string, name strin AND Name = :Name`, map[string]interface{}{"UserId": userId, "Category": category, "Name": name}); err != nil { result.Err = model.NewAppError("SqlPreferenceStore.GetByName", "We encounted an error while finding preferences", err.Error()) } else { + result.Data = preferences[0] + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + +func (s SqlPreferenceStore) GetCategory(userId string, category 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 + AND Category = :Category`, map[string]interface{}{"UserId": userId, "Category": category}); err != nil { + result.Err = model.NewAppError("SqlPreferenceStore.GetCategory", "We encounted an error while finding preferences", err.Error()) + } else { result.Data = preferences } diff --git a/store/sql_preference_store_test.go b/store/sql_preference_store_test.go index 6d5ec0ecd..1feda01d9 100644 --- a/store/sql_preference_store_test.go +++ b/store/sql_preference_store_test.go @@ -16,14 +16,14 @@ func TestPreferenceSave(t *testing.T) { preferences := model.Preferences{ { UserId: id, - Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNELS, - Name: model.PREFERENCE_NAME_SHOW, + Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW, + Name: model.NewId(), Value: "value1a", }, { UserId: id, - Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNELS, - Name: model.PREFERENCE_NAME_TEST, + Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW, + Name: model.NewId(), Value: "value1b", }, } @@ -32,9 +32,7 @@ func TestPreferenceSave(t *testing.T) { } for _, preference := range preferences { - if data := Must(store.Preference().GetByName(preference.UserId, preference.Category, preference.Name)).(model.Preferences); len(data) != 1 { - t.Fatal("got incorrect number of preferences after first Save") - } else if *preference != *data[0] { + if data := Must(store.Preference().Get(preference.UserId, preference.Category, preference.Name)).(model.Preference); preference != data { t.Fatal("got incorrect preference after first Save") } } @@ -46,66 +44,91 @@ func TestPreferenceSave(t *testing.T) { } for _, preference := range preferences { - if data := Must(store.Preference().GetByName(preference.UserId, preference.Category, preference.Name)).(model.Preferences); len(data) != 1 { - t.Fatal("got incorrect number of preferences after second Save") - } else if *preference != *data[0] { + if data := Must(store.Preference().Get(preference.UserId, preference.Category, preference.Name)).(model.Preference); preference != data { t.Fatal("got incorrect preference after second Save") } } } -func TestPreferenceGetByName(t *testing.T) { +func TestPreferenceGet(t *testing.T) { Setup() userId := model.NewId() - category := model.PREFERENCE_CATEGORY_DIRECT_CHANNELS - name := model.PREFERENCE_NAME_SHOW - altId := model.NewId() + category := model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW + name := model.PREFERENCE_NAME_TEST preferences := model.Preferences{ { UserId: userId, Category: category, Name: name, - AltId: altId, }, - // same user/category/name, different alt id + { + UserId: userId, + Category: category, + Name: model.NewId(), + }, + { + UserId: userId, + Category: model.PREFERENCE_CATEGORY_TEST, + Name: name, + }, + { + UserId: model.NewId(), + Category: category, + Name: name, + }, + } + + Must(store.Preference().Save(&preferences)) + + if result := <-store.Preference().Get(userId, category, name); result.Err != nil { + t.Fatal(result.Err) + } else if data := result.Data.(model.Preference); data != preferences[0] { + t.Fatal("got incorrect preference") + } +} + +func TestPreferenceGetCategory(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, - AltId: model.NewId(), }, - // same user/category/alt id, different name + // same user/category, different name { UserId: userId, Category: category, - Name: model.PREFERENCE_NAME_TEST, - AltId: altId, + Name: model.NewId(), }, - // same user/name/alt id, different category + // same user/name, different category { UserId: userId, Category: model.PREFERENCE_CATEGORY_TEST, Name: name, - AltId: altId, }, - // same name/category/alt id, different user + // same name/category, different user { UserId: model.NewId(), Category: category, Name: name, - AltId: altId, }, } Must(store.Preference().Save(&preferences)) - if result := <-store.Preference().GetByName(userId, category, name); result.Err != nil { + if result := <-store.Preference().GetCategory(userId, category); result.Err != nil { t.Fatal(result.Err) } else if data := result.Data.(model.Preferences); len(data) != 2 { t.Fatal("got the wrong number of preferences") - } else if !((*data[0] == *preferences[0] && *data[1] == *preferences[1]) || (*data[0] == *preferences[1] && *data[1] == *preferences[0])) { + } else if !((data[0] == preferences[0] && data[1] == preferences[1]) || (data[0] == preferences[1] && data[1] == preferences[0])) { t.Fatal("got incorrect preferences") } } diff --git a/store/store.go b/store/store.go index 9d9fd6489..6e1614ccb 100644 --- a/store/store.go +++ b/store/store.go @@ -153,5 +153,6 @@ type WebhookStore interface { type PreferenceStore interface { Save(preferences *model.Preferences) StoreChannel - GetByName(userId string, category string, name string) StoreChannel + Get(userId string, category string, name string) StoreChannel + GetCategory(userId string, category string) StoreChannel } |