made the preference store GetCategory method sync (#10847)

* made the preference store GetCategory method sync

* fixed the review comments
Этот коммит содержится в:
Pradeep Murugesan
2019-05-15 15:56:42 +01:00
коммит произвёл Christopher Speller
родитель 2130e9f0b1
Коммит 24a02b4168
6 изменённых файлов: 38 добавлений и 31 удалений

Просмотреть файл

@@ -33,11 +33,10 @@ func ptrBool(b bool) *bool {
} }
func checkPreference(t *testing.T, a *App, userId string, category string, name string, value string) { func checkPreference(t *testing.T, a *App, userId string, category string, name string, value string) {
if res := <-a.Srv.Store.Preference().GetCategory(userId, category); res.Err != nil { if preferences, err := a.Srv.Store.Preference().GetCategory(userId, category); err != nil {
debug.PrintStack() debug.PrintStack()
t.Fatalf("Failed to get preferences for user %v with category %v", userId, category) t.Fatalf("Failed to get preferences for user %v with category %v", userId, category)
} else { } else {
preferences := res.Data.(model.Preferences)
found := false found := false
for _, preference := range preferences { for _, preference := range preferences {
if preference.Name == name { if preference.Name == name {

Просмотреть файл

@@ -19,16 +19,16 @@ func (a *App) GetPreferencesForUser(userId string) (model.Preferences, *model.Ap
} }
func (a *App) GetPreferenceByCategoryForUser(userId string, category string) (model.Preferences, *model.AppError) { func (a *App) GetPreferenceByCategoryForUser(userId string, category string) (model.Preferences, *model.AppError) {
result := <-a.Srv.Store.Preference().GetCategory(userId, category) preferences, err := a.Srv.Store.Preference().GetCategory(userId, category)
if result.Err != nil { if err != nil {
result.Err.StatusCode = http.StatusBadRequest err.StatusCode = http.StatusBadRequest
return nil, result.Err return nil, err
} }
if len(result.Data.(model.Preferences)) == 0 { if len(preferences) == 0 {
err := model.NewAppError("getPreferenceCategory", "api.preference.preferences_category.get.app_error", nil, "", http.StatusNotFound) err := model.NewAppError("getPreferenceCategory", "api.preference.preferences_category.get.app_error", nil, "", http.StatusNotFound)
return nil, err return nil, err
} }
return result.Data.(model.Preferences), nil return preferences, nil
} }
func (a *App) GetPreferenceByCategoryAndNameForUser(userId string, category string, preferenceName string) (*model.Preference, *model.AppError) { func (a *App) GetPreferenceByCategoryAndNameForUser(userId string, category string, preferenceName string) (*model.Preference, *model.AppError) {

Просмотреть файл

@@ -180,23 +180,22 @@ func (s SqlPreferenceStore) Get(userId string, category string, name string) (*m
return preference, nil return preference, nil
} }
func (s SqlPreferenceStore) GetCategory(userId string, category string) store.StoreChannel { func (s SqlPreferenceStore) GetCategory(userId string, category string) (model.Preferences, *model.AppError) {
return store.Do(func(result *store.StoreResult) { var preferences model.Preferences
var preferences model.Preferences
if _, err := s.GetReplica().Select(&preferences, if _, err := s.GetReplica().Select(&preferences,
`SELECT `SELECT
* *
FROM FROM
Preferences Preferences
WHERE WHERE
UserId = :UserId UserId = :UserId
AND Category = :Category`, map[string]interface{}{"UserId": userId, "Category": category}); err != nil { AND Category = :Category`, map[string]interface{}{"UserId": userId, "Category": category}); err != nil {
result.Err = model.NewAppError("SqlPreferenceStore.GetCategory", "store.sql_preference.get_category.app_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlPreferenceStore.GetCategory", "store.sql_preference.get_category.app_error", nil, err.Error(), http.StatusInternalServerError)
} else { }
result.Data = preferences
} return preferences, nil
})
} }
func (s SqlPreferenceStore) GetAll(userId string) store.StoreChannel { func (s SqlPreferenceStore) GetAll(userId string) store.StoreChannel {

Просмотреть файл

@@ -429,8 +429,8 @@ type CommandWebhookStore interface {
type PreferenceStore interface { type PreferenceStore interface {
Save(preferences *model.Preferences) StoreChannel Save(preferences *model.Preferences) StoreChannel
GetCategory(userId string, category string) (model.Preferences, *model.AppError)
Get(userId string, category string, name string) (*model.Preference, *model.AppError) Get(userId string, category string, name string) (*model.Preference, *model.AppError)
GetCategory(userId string, category string) StoreChannel
GetAll(userId string) StoreChannel GetAll(userId string) StoreChannel
Delete(userId, category, name string) StoreChannel Delete(userId, category, name string) StoreChannel
DeleteCategory(userId string, category string) StoreChannel DeleteCategory(userId string, category string) StoreChannel

Просмотреть файл

@@ -119,19 +119,28 @@ func (_m *PreferenceStore) GetAll(userId string) store.StoreChannel {
} }
// GetCategory provides a mock function with given fields: userId, category // GetCategory provides a mock function with given fields: userId, category
func (_m *PreferenceStore) GetCategory(userId string, category string) store.StoreChannel { func (_m *PreferenceStore) GetCategory(userId string, category string) (model.Preferences, *model.AppError) {
ret := _m.Called(userId, category) ret := _m.Called(userId, category)
var r0 store.StoreChannel var r0 model.Preferences
if rf, ok := ret.Get(0).(func(string, string) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string, string) model.Preferences); ok {
r0 = rf(userId, category) r0 = rf(userId, category)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(model.Preferences)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string, string) *model.AppError); ok {
r1 = rf(userId, category)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// IsFeatureEnabled provides a mock function with given fields: feature, userId // IsFeatureEnabled provides a mock function with given fields: feature, userId

Просмотреть файл

@@ -140,18 +140,18 @@ func testPreferenceGetCategory(t *testing.T, ss store.Store) {
store.Must(ss.Preference().Save(&preferences)) store.Must(ss.Preference().Save(&preferences))
if result := <-ss.Preference().GetCategory(userId, category); result.Err != nil { if preferencesByCategory, err := ss.Preference().GetCategory(userId, category); err != nil {
t.Fatal(result.Err) t.Fatal(err)
} else if data := result.Data.(model.Preferences); len(data) != 2 { } else if len(preferencesByCategory) != 2 {
t.Fatal("got the wrong number of preferences") 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 !((preferencesByCategory[0] == preferences[0] && preferencesByCategory[1] == preferences[1]) || (preferencesByCategory[0] == preferences[1] && preferencesByCategory[1] == preferences[0])) {
t.Fatal("got incorrect preferences") t.Fatal("got incorrect preferences")
} }
// make sure getting a missing preference category doesn't fail // make sure getting a missing preference category doesn't fail
if result := <-ss.Preference().GetCategory(model.NewId(), model.NewId()); result.Err != nil { if preferencesByCategory, err := ss.Preference().GetCategory(model.NewId(), model.NewId()); err != nil {
t.Fatal(result.Err) t.Fatal(err)
} else if data := result.Data.(model.Preferences); len(data) != 0 { } else if len(preferencesByCategory) != 0 {
t.Fatal("shouldn't have got any preferences") t.Fatal("shouldn't have got any preferences")
} }
} }