summaryrefslogtreecommitdiffstats
path: root/store/sql_preference_store.go
diff options
context:
space:
mode:
authorhmhealey <harrisonmhealey@gmail.com>2015-10-01 10:56:07 -0400
committerhmhealey <harrisonmhealey@gmail.com>2015-10-13 09:42:23 -0400
commitda66599fa39ddbff96b0844fabac161e130a2bc4 (patch)
treeda6ba69abdf1fa846805ef8cca3f1449aa6df15f /store/sql_preference_store.go
parent3229457de0eb6e04fff6ce4fe1466a828be5f6f6 (diff)
downloadchat-da66599fa39ddbff96b0844fabac161e130a2bc4.tar.gz
chat-da66599fa39ddbff96b0844fabac161e130a2bc4.tar.bz2
chat-da66599fa39ddbff96b0844fabac161e130a2bc4.zip
Added Preferences table to store user preferences
Diffstat (limited to 'store/sql_preference_store.go')
-rw-r--r--store/sql_preference_store.go125
1 files changed, 125 insertions, 0 deletions
diff --git a/store/sql_preference_store.go b/store/sql_preference_store.go
new file mode 100644
index 000000000..3e3a91f61
--- /dev/null
+++ b/store/sql_preference_store.go
@@ -0,0 +1,125 @@
+// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package store
+
+import (
+ "github.com/mattermost/platform/model"
+)
+
+type SqlPreferenceStore struct {
+ *SqlStore
+}
+
+func NewSqlPreferenceStore(sqlStore *SqlStore) PreferenceStore {
+ s := &SqlPreferenceStore{sqlStore}
+
+ for _, db := range sqlStore.GetAllConns() {
+ table := db.AddTableWithName(model.Preference{}, "Preferences").SetKeys(false, "UserId", "Category", "Name", "AltId")
+ table.ColMap("UserId").SetMaxSize(26)
+ table.ColMap("Category").SetMaxSize(32)
+ table.ColMap("Name").SetMaxSize(32)
+ table.ColMap("AltId").SetMaxSize(26)
+ table.ColMap("Value").SetMaxSize(128)
+ }
+
+ return s
+}
+
+func (s SqlPreferenceStore) UpgradeSchemaIfNeeded() {
+}
+
+func (s SqlPreferenceStore) CreateIndexesIfNotExists() {
+ s.CreateIndexIfNotExists("idx_preferences_user_id", "Preferences", "UserId")
+ s.CreateIndexIfNotExists("idx_preferences_category", "Preferences", "Category")
+ s.CreateIndexIfNotExists("idx_preferences_name", "Preferences", "Name")
+}
+
+func (s SqlPreferenceStore) Save(preference *model.Preference) StoreChannel {
+ storeChannel := make(StoreChannel)
+
+ go func() {
+ result := StoreResult{}
+
+ if result.Err = preference.IsValid(); result.Err != nil {
+ storeChannel <- result
+ close(storeChannel)
+ return
+ }
+
+ if err := s.GetMaster().Insert(preference); err != nil {
+ if IsUniqueConstraintError(err.Error(), "UserId", "preferences_pkey") {
+ result.Err = model.NewAppError("SqlPreferenceStore.Save", "A preference with that user id, category, name, and alt id already exists",
+ "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error())
+ } else {
+ result.Err = model.NewAppError("SqlPreferenceStore.Save", "We couldn't save the preference",
+ "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error())
+ }
+ } else {
+ result.Data = preference
+ }
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}
+
+func (s SqlPreferenceStore) Update(preference *model.Preference) StoreChannel {
+ storeChannel := make(StoreChannel)
+
+ go func() {
+ result := StoreResult{}
+
+ if result.Err = preference.IsValid(); result.Err != nil {
+ storeChannel <- result
+ close(storeChannel)
+ return
+ }
+
+ if count, err := s.GetMaster().Update(preference); err != nil {
+ result.Err = model.NewAppError("SqlPreferenceStore.Update", "We couldn't update the preference",
+ "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error())
+ } else if count != 1 {
+ result.Err = model.NewAppError("SqlPreferenceStore.Update", "We couldn't update the preference",
+ "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId)
+ } else {
+ result.Data = preference
+ }
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}
+
+func (s SqlPreferenceStore) GetByName(userId string, category string, name string) StoreChannel {
+ storeChannel := make(StoreChannel)
+
+ go func() {
+ result := StoreResult{}
+
+ var preferences []*model.Preference
+
+ if _, err := s.GetReplica().Select(&preferences,
+ `SELECT
+ *
+ FROM
+ Preferences
+ WHERE
+ UserId = :UserId
+ AND Category = :Category
+ AND Name = :Name`, map[string]interface{}{"UserId": userId, "Category": category, "Name": name}); err != nil {
+ result.Err = model.NewAppError("SqlPreferenceStore.GetByName", "We encounted an error while finding preferences", err.Error())
+ } else {
+ result.Data = preferences
+ }
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}