summaryrefslogtreecommitdiffstats
path: root/store/sqlstore/command_webhook_store.go
diff options
context:
space:
mode:
authorChris <ccbrown112@gmail.com>2017-10-06 08:12:10 -0700
committerJoram Wilander <jwawilander@gmail.com>2017-10-06 11:12:10 -0400
commit363568b4eb3209adb1b88ceb0d8e455e6d4a1073 (patch)
tree419fb74d8e1a2e91f9f77aa5b873c005bcfbff48 /store/sqlstore/command_webhook_store.go
parent12501673d0c70120eebeac633e5072b2e7a2174d (diff)
downloadchat-363568b4eb3209adb1b88ceb0d8e455e6d4a1073.tar.gz
chat-363568b4eb3209adb1b88ceb0d8e455e6d4a1073.tar.bz2
chat-363568b4eb3209adb1b88ceb0d8e455e6d4a1073.zip
reduce store boiler plate (#7585)
Diffstat (limited to 'store/sqlstore/command_webhook_store.go')
-rw-r--r--store/sqlstore/command_webhook_store.go43
1 files changed, 6 insertions, 37 deletions
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() {