summaryrefslogtreecommitdiffstats
path: root/store/sql_command_webhook_store.go
diff options
context:
space:
mode:
authorChris <ccbrown112@gmail.com>2017-09-25 09:11:25 -0500
committerJoram Wilander <jwawilander@gmail.com>2017-09-25 10:11:25 -0400
commit49fe5fbf3db56fc466b8997b182ee135d7a4365d (patch)
tree1252fea09aa3ce899e2e8edb1fb7b42900f50bca /store/sql_command_webhook_store.go
parentb2c5b97601b61f5748b46e4e386134203111ebb0 (diff)
downloadchat-49fe5fbf3db56fc466b8997b182ee135d7a4365d.tar.gz
chat-49fe5fbf3db56fc466b8997b182ee135d7a4365d.tar.bz2
chat-49fe5fbf3db56fc466b8997b182ee135d7a4365d.zip
Move sql store code into store/sqlstore package (#7502)
* move sql store code into store/sqlstore package * move non-sql constants back up to store * fix api test * derp
Diffstat (limited to 'store/sql_command_webhook_store.go')
-rw-r--r--store/sql_command_webhook_store.go124
1 files changed, 0 insertions, 124 deletions
diff --git a/store/sql_command_webhook_store.go b/store/sql_command_webhook_store.go
deleted file mode 100644
index 801950735..000000000
--- a/store/sql_command_webhook_store.go
+++ /dev/null
@@ -1,124 +0,0 @@
-// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
-// See License.txt for license information.
-
-package store
-
-import (
- "database/sql"
- "net/http"
-
- l4g "github.com/alecthomas/log4go"
-
- "github.com/mattermost/mattermost-server/model"
-)
-
-type SqlCommandWebhookStore struct {
- SqlStore
-}
-
-func NewSqlCommandWebhookStore(sqlStore SqlStore) CommandWebhookStore {
- s := &SqlCommandWebhookStore{sqlStore}
-
- for _, db := range sqlStore.GetAllConns() {
- tablec := db.AddTableWithName(model.CommandWebhook{}, "CommandWebhooks").SetKeys(false, "Id")
- tablec.ColMap("Id").SetMaxSize(26)
- tablec.ColMap("CommandId").SetMaxSize(26)
- tablec.ColMap("UserId").SetMaxSize(26)
- tablec.ColMap("ChannelId").SetMaxSize(26)
- tablec.ColMap("RootId").SetMaxSize(26)
- tablec.ColMap("ParentId").SetMaxSize(26)
- }
-
- return s
-}
-
-func (s SqlCommandWebhookStore) CreateIndexesIfNotExists() {
- s.CreateIndexIfNotExists("idx_command_webhook_create_at", "CommandWebhooks", "CreateAt")
-}
-
-func (s SqlCommandWebhookStore) Save(webhook *model.CommandWebhook) StoreChannel {
- storeChannel := make(StoreChannel, 1)
-
- go func() {
- result := 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
- }
-
- if err := s.GetMaster().Insert(webhook); err != nil {
- result.Err = model.NewAppError("SqlCommandWebhookStore.Save", "store.sql_command_webhooks.save.app_error", nil, "id="+webhook.Id+", "+err.Error(), http.StatusInternalServerError)
- } else {
- result.Data = webhook
- }
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
-}
-
-func (s SqlCommandWebhookStore) Get(id string) StoreChannel {
- storeChannel := make(StoreChannel, 1)
-
- go func() {
- result := StoreResult{}
-
- var webhook model.CommandWebhook
-
- exptime := model.GetMillis() - model.COMMAND_WEBHOOK_LIFETIME
- if err := s.GetReplica().SelectOne(&webhook, "SELECT * FROM CommandWebhooks WHERE Id = :Id AND CreateAt > :ExpTime", map[string]interface{}{"Id": id, "ExpTime": exptime}); err != nil {
- result.Err = model.NewAppError("SqlCommandWebhookStore.Get", "store.sql_command_webhooks.get.app_error", nil, "id="+id+", err="+err.Error(), http.StatusInternalServerError)
- if err == sql.ErrNoRows {
- result.Err.StatusCode = http.StatusNotFound
- }
- }
-
- result.Data = &webhook
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
-}
-
-func (s SqlCommandWebhookStore) TryUse(id string, limit int) StoreChannel {
- storeChannel := make(StoreChannel, 1)
-
- go func() {
- result := 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 {
- result.Err = model.NewAppError("SqlCommandWebhookStore.TryUse", "store.sql_command_webhooks.try_use.invalid.app_error", nil, "id="+id, http.StatusBadRequest)
- }
-
- result.Data = id
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
-}
-
-func (s SqlCommandWebhookStore) Cleanup() {
- l4g.Debug("Cleaning up command webhook store.")
- exptime := model.GetMillis() - model.COMMAND_WEBHOOK_LIFETIME
- if _, err := s.GetMaster().Exec("DELETE FROM CommandWebhooks WHERE CreateAt < :ExpTime", map[string]interface{}{"ExpTime": exptime}); err != nil {
- l4g.Error("Unable to cleanup command webhook store.")
- }
-}