Removing unnecesary StoreResult usages to simplify the store code (#11519)
* Removing unnecesary StoreResult usages to simplify the store code * Fixing tests
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
8858b15e7e
Коммит
a9fc3581ff
@@ -63,8 +63,8 @@ func (s SqlPreferenceStore) Save(preferences *model.Preferences) *model.AppError
|
|||||||
|
|
||||||
defer finalizeTransaction(transaction)
|
defer finalizeTransaction(transaction)
|
||||||
for _, preference := range *preferences {
|
for _, preference := range *preferences {
|
||||||
if upsertResult := s.save(transaction, &preference); upsertResult.Err != nil {
|
if upsertErr := s.save(transaction, &preference); upsertErr != nil {
|
||||||
return upsertResult.Err
|
return upsertErr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,13 +75,11 @@ func (s SqlPreferenceStore) Save(preferences *model.Preferences) *model.AppError
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *model.Preference) store.StoreResult {
|
func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *model.Preference) *model.AppError {
|
||||||
result := store.StoreResult{}
|
|
||||||
|
|
||||||
preference.PreUpdate()
|
preference.PreUpdate()
|
||||||
|
|
||||||
if result.Err = preference.IsValid(); result.Err != nil {
|
if err := preference.IsValid(); err != nil {
|
||||||
return result
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
params := map[string]interface{}{
|
params := map[string]interface{}{
|
||||||
@@ -100,8 +98,9 @@ func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *mode
|
|||||||
(:UserId, :Category, :Name, :Value)
|
(:UserId, :Category, :Name, :Value)
|
||||||
ON DUPLICATE KEY UPDATE
|
ON DUPLICATE KEY UPDATE
|
||||||
Value = :Value`, params); err != nil {
|
Value = :Value`, params); err != nil {
|
||||||
result.Err = model.NewAppError("SqlPreferenceStore.save", "store.sql_preference.save.updating.app_error", nil, err.Error(), http.StatusInternalServerError)
|
return model.NewAppError("SqlPreferenceStore.save", "store.sql_preference.save.updating.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
} else if s.DriverName() == model.DATABASE_DRIVER_POSTGRES {
|
} else if s.DriverName() == model.DATABASE_DRIVER_POSTGRES {
|
||||||
// postgres has no way to upsert values until version 9.5 and trying inserting and then updating causes transactions to abort
|
// postgres has no way to upsert values until version 9.5 and trying inserting and then updating causes transactions to abort
|
||||||
count, err := transaction.SelectInt(
|
count, err := transaction.SelectInt(
|
||||||
@@ -114,47 +113,37 @@ func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *mode
|
|||||||
AND Category = :Category
|
AND Category = :Category
|
||||||
AND Name = :Name`, params)
|
AND Name = :Name`, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.Err = model.NewAppError("SqlPreferenceStore.save", "store.sql_preference.save.updating.app_error", nil, err.Error(), http.StatusInternalServerError)
|
return model.NewAppError("SqlPreferenceStore.save", "store.sql_preference.save.updating.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if count == 1 {
|
if count == 1 {
|
||||||
result = s.update(transaction, preference)
|
return s.update(transaction, preference)
|
||||||
} else {
|
|
||||||
result = s.insert(transaction, preference)
|
|
||||||
}
|
}
|
||||||
} else {
|
return s.insert(transaction, preference)
|
||||||
result.Err = model.NewAppError("SqlPreferenceStore.save", "store.sql_preference.save.missing_driver.app_error", nil, "Failed to update preference because of missing driver", http.StatusNotImplemented)
|
}
|
||||||
|
return model.NewAppError("SqlPreferenceStore.save", "store.sql_preference.save.missing_driver.app_error", nil, "Failed to update preference because of missing driver", http.StatusNotImplemented)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
func (s SqlPreferenceStore) insert(transaction *gorp.Transaction, preference *model.Preference) *model.AppError {
|
||||||
}
|
|
||||||
|
|
||||||
func (s SqlPreferenceStore) insert(transaction *gorp.Transaction, preference *model.Preference) store.StoreResult {
|
|
||||||
result := store.StoreResult{}
|
|
||||||
|
|
||||||
if err := transaction.Insert(preference); err != nil {
|
if err := transaction.Insert(preference); err != nil {
|
||||||
if IsUniqueConstraintError(err, []string{"UserId", "preferences_pkey"}) {
|
if IsUniqueConstraintError(err, []string{"UserId", "preferences_pkey"}) {
|
||||||
result.Err = model.NewAppError("SqlPreferenceStore.insert", "store.sql_preference.insert.exists.app_error", nil,
|
return model.NewAppError("SqlPreferenceStore.insert", "store.sql_preference.insert.exists.app_error", nil,
|
||||||
"user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", "+err.Error(), http.StatusBadRequest)
|
"user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", "+err.Error(), http.StatusBadRequest)
|
||||||
} else {
|
}
|
||||||
result.Err = model.NewAppError("SqlPreferenceStore.insert", "store.sql_preference.insert.save.app_error", nil,
|
return model.NewAppError("SqlPreferenceStore.insert", "store.sql_preference.insert.save.app_error", nil,
|
||||||
"user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", "+err.Error(), http.StatusInternalServerError)
|
"user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", "+err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
func (s SqlPreferenceStore) update(transaction *gorp.Transaction, preference *model.Preference) *model.AppError {
|
||||||
}
|
|
||||||
|
|
||||||
func (s SqlPreferenceStore) update(transaction *gorp.Transaction, preference *model.Preference) store.StoreResult {
|
|
||||||
result := store.StoreResult{}
|
|
||||||
|
|
||||||
if _, err := transaction.Update(preference); err != nil {
|
if _, err := transaction.Update(preference); err != nil {
|
||||||
result.Err = model.NewAppError("SqlPreferenceStore.update", "store.sql_preference.update.app_error", nil,
|
return model.NewAppError("SqlPreferenceStore.update", "store.sql_preference.update.app_error", nil,
|
||||||
"user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", "+err.Error(), http.StatusInternalServerError)
|
"user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", "+err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SqlPreferenceStore) Get(userId string, category string, name string) (*model.Preference, *model.AppError) {
|
func (s SqlPreferenceStore) Get(userId string, category string, name string) (*model.Preference, *model.AppError) {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user