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_webhook_store.go | 43 +++++---------------------------- 1 file changed, 6 insertions(+), 37 deletions(-) (limited to 'store/sqlstore/command_webhook_store.go') diff --git a/store/sqlstore/command_webhook_store.go b/store/sqlstore/command_webhook_store.go index dc1ad0732..40fa8577c 100644 --- a/store/sqlstore/command_webhook_store.go +++ b/store/sqlstore/command_webhook_store.go @@ -38,22 +38,14 @@ func (s SqlCommandWebhookStore) CreateIndexesIfNotExists() { } func (s SqlCommandWebhookStore) Save(webhook *model.CommandWebhook) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if len(webhook.Id) > 0 { result.Err = model.NewAppError("SqlCommandWebhookStore.Save", "store.sql_command_webhooks.save.existing.app_error", nil, "id="+webhook.Id, http.StatusBadRequest) - storeChannel <- result - close(storeChannel) return } webhook.PreSave() if result.Err = webhook.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -62,20 +54,11 @@ func (s SqlCommandWebhookStore) Save(webhook *model.CommandWebhook) store.StoreC } else { result.Data = webhook } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandWebhookStore) Get(id string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var webhook model.CommandWebhook exptime := model.GetMillis() - model.COMMAND_WEBHOOK_LIFETIME @@ -87,20 +70,11 @@ func (s SqlCommandWebhookStore) Get(id string) store.StoreChannel { } result.Data = &webhook - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandWebhookStore) TryUse(id string, limit int) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if sqlResult, err := s.GetMaster().Exec("UPDATE CommandWebhooks SET UseCount = UseCount + 1 WHERE Id = :Id AND UseCount < :UseLimit", map[string]interface{}{"Id": id, "UseLimit": limit}); err != nil { result.Err = model.NewAppError("SqlCommandWebhookStore.TryUse", "store.sql_command_webhooks.try_use.app_error", nil, "id="+id+", err="+err.Error(), http.StatusInternalServerError) } else if rows, _ := sqlResult.RowsAffected(); rows == 0 { @@ -108,12 +82,7 @@ func (s SqlCommandWebhookStore) TryUse(id string, limit int) store.StoreChannel } result.Data = id - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandWebhookStore) Cleanup() { -- cgit v1.2.3-1-g7c22