From 363568b4eb3209adb1b88ceb0d8e455e6d4a1073 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 6 Oct 2017 08:12:10 -0700 Subject: reduce store boiler plate (#7585) --- store/sqlstore/command_store.go | 121 ++++++---------------------------------- 1 file changed, 18 insertions(+), 103 deletions(-) (limited to 'store/sqlstore/command_store.go') diff --git a/store/sqlstore/command_store.go b/store/sqlstore/command_store.go index 8284f889b..d7c53e7e2 100644 --- a/store/sqlstore/command_store.go +++ b/store/sqlstore/command_store.go @@ -45,22 +45,14 @@ func (s SqlCommandStore) CreateIndexesIfNotExists() { } func (s SqlCommandStore) Save(command *model.Command) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if len(command.Id) > 0 { result.Err = model.NewAppError("SqlCommandStore.Save", "store.sql_command.save.saving_overwrite.app_error", nil, "id="+command.Id, http.StatusBadRequest) - storeChannel <- result - close(storeChannel) return } command.PreSave() if result.Err = command.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -69,20 +61,11 @@ func (s SqlCommandStore) Save(command *model.Command) store.StoreChannel { } else { result.Data = command } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandStore) Get(id string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var command model.Command if err := s.GetReplica().SelectOne(&command, "SELECT * FROM Commands WHERE Id = :Id AND DeleteAt = 0", map[string]interface{}{"Id": id}); err != nil { @@ -90,20 +73,11 @@ func (s SqlCommandStore) Get(id string) store.StoreChannel { } result.Data = &command - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandStore) GetByTeam(teamId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var commands []*model.Command if _, err := s.GetReplica().Select(&commands, "SELECT * FROM Commands WHERE TeamId = :TeamId AND DeleteAt = 0", map[string]interface{}{"TeamId": teamId}); err != nil { @@ -111,20 +85,11 @@ func (s SqlCommandStore) GetByTeam(teamId string) store.StoreChannel { } result.Data = commands - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandStore) GetByTrigger(teamId string, trigger string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var command model.Command if err := s.GetReplica().SelectOne(&command, "SELECT * FROM Commands WHERE TeamId = :TeamId AND `Trigger` = :Trigger AND DeleteAt = 0", map[string]interface{}{"TeamId": teamId, "Trigger": trigger}); err != nil { @@ -132,74 +97,38 @@ func (s SqlCommandStore) GetByTrigger(teamId string, trigger string) store.Store } result.Data = &command - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandStore) Delete(commandId string, time int64) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("Update Commands SET DeleteAt = :DeleteAt, UpdateAt = :UpdateAt WHERE Id = :Id", map[string]interface{}{"DeleteAt": time, "UpdateAt": time, "Id": commandId}) if err != nil { result.Err = model.NewAppError("SqlCommandStore.Delete", "store.sql_command.save.delete.app_error", nil, "id="+commandId+", err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandStore) PermanentDeleteByTeam(teamId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("DELETE FROM Commands WHERE TeamId = :TeamId", map[string]interface{}{"TeamId": teamId}) if err != nil { result.Err = model.NewAppError("SqlCommandStore.DeleteByTeam", "store.sql_command.save.delete_perm.app_error", nil, "id="+teamId+", err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandStore) PermanentDeleteByUser(userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("DELETE FROM Commands WHERE CreatorId = :UserId", map[string]interface{}{"UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlCommandStore.DeleteByUser", "store.sql_command.save.delete_perm.app_error", nil, "id="+userId+", err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandStore) Update(cmd *model.Command) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { cmd.UpdateAt = model.GetMillis() if _, err := s.GetMaster().Update(cmd); err != nil { @@ -207,20 +136,11 @@ func (s SqlCommandStore) Update(cmd *model.Command) store.StoreChannel { } else { result.Data = cmd } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandStore) AnalyticsCommandCount(teamId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { query := `SELECT COUNT(*) @@ -238,10 +158,5 @@ func (s SqlCommandStore) AnalyticsCommandCount(teamId string) store.StoreChannel } else { result.Data = c } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } -- cgit v1.2.3-1-g7c22