From d245b29f82a03f1aff966a2fb2100a5703d82f32 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Wed, 25 Jan 2017 09:32:42 -0500 Subject: More app code migration (#5170) * Migrate admin functions into app package * More user function refactoring * Move post functions into app package --- store/sql_post_store.go | 21 ++++++++++++++++++ store/sql_post_store_test.go | 23 +++++++++++++++++++ store/sql_preference_store.go | 22 +++++++++++++++++++ store/sql_preference_store_test.go | 45 ++++++++++++++++++++++++++++++++++++++ store/store.go | 2 ++ 5 files changed, 113 insertions(+) (limited to 'store') diff --git a/store/sql_post_store.go b/store/sql_post_store.go index c1aaee3e6..ed87b01f7 100644 --- a/store/sql_post_store.go +++ b/store/sql_post_store.go @@ -222,6 +222,27 @@ func (s SqlPostStore) Get(id string) StoreChannel { return storeChannel } +func (s SqlPostStore) GetSingle(id string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + var post model.Post + err := s.GetReplica().SelectOne(&post, "SELECT * FROM Posts WHERE Id = :Id AND DeleteAt = 0", map[string]interface{}{"Id": id}) + if err != nil { + result.Err = model.NewLocAppError("SqlPostStore.GetSingle", "store.sql_post.get.app_error", nil, "id="+id+err.Error()) + } + + result.Data = &post + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + type etagPosts struct { Id string UpdateAt int64 diff --git a/store/sql_post_store_test.go b/store/sql_post_store_test.go index eb22979db..e3886c6bc 100644 --- a/store/sql_post_store_test.go +++ b/store/sql_post_store_test.go @@ -63,6 +63,29 @@ func TestPostStoreGet(t *testing.T) { } } +func TestPostStoreGetSingle(t *testing.T) { + Setup() + + o1 := &model.Post{} + o1.ChannelId = model.NewId() + o1.UserId = model.NewId() + o1.Message = "a" + model.NewId() + "b" + + o1 = (<-store.Post().Save(o1)).Data.(*model.Post) + + if r1 := <-store.Post().GetSingle(o1.Id); r1.Err != nil { + t.Fatal(r1.Err) + } else { + if r1.Data.(*model.Post).CreateAt != o1.CreateAt { + t.Fatal("invalid returned post") + } + } + + if err := (<-store.Post().GetSingle("123")).Err; err == nil { + t.Fatal("Missing id should have failed") + } +} + func TestGetEtagCache(t *testing.T) { Setup() o1 := &model.Post{} diff --git a/store/sql_preference_store.go b/store/sql_preference_store.go index 5c46d1328..14a9ff48b 100644 --- a/store/sql_preference_store.go +++ b/store/sql_preference_store.go @@ -348,3 +348,25 @@ func (s SqlPreferenceStore) DeleteCategory(userId string, category string) Store return storeChannel } + +func (s SqlPreferenceStore) DeleteCategoryAndName(category string, name string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + if _, err := s.GetMaster().Exec( + `DELETE FROM + Preferences + WHERE + Name = :Name + AND Category = :Category`, map[string]interface{}{"Name": name, "Category": category}); err != nil { + result.Err = model.NewLocAppError("SqlPreferenceStore.DeleteCategoryAndName", "store.sql_preference.delete.app_error", nil, err.Error()) + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} diff --git a/store/sql_preference_store_test.go b/store/sql_preference_store_test.go index fc1cf5f5b..adcaa8d89 100644 --- a/store/sql_preference_store_test.go +++ b/store/sql_preference_store_test.go @@ -427,3 +427,48 @@ func TestPreferenceDeleteCategory(t *testing.T) { t.Fatal("should've returned no preferences") } } + +func TestPreferenceDeleteCategoryAndName(t *testing.T) { + Setup() + + category := model.NewId() + name := model.NewId() + userId := model.NewId() + userId2 := model.NewId() + + preference1 := model.Preference{ + UserId: userId, + Category: category, + Name: name, + Value: "value1a", + } + + preference2 := model.Preference{ + UserId: userId2, + Category: category, + Name: name, + Value: "value1a", + } + + Must(store.Preference().Save(&model.Preferences{preference1, preference2})) + + if prefs := Must(store.Preference().GetAll(userId)).(model.Preferences); len([]model.Preference(prefs)) != 1 { + t.Fatal("should've returned 1 preference") + } + + if prefs := Must(store.Preference().GetAll(userId2)).(model.Preferences); len([]model.Preference(prefs)) != 1 { + t.Fatal("should've returned 1 preference") + } + + if result := <-store.Preference().DeleteCategoryAndName(category, name); result.Err != nil { + t.Fatal(result.Err) + } + + if prefs := Must(store.Preference().GetAll(userId)).(model.Preferences); len([]model.Preference(prefs)) != 0 { + t.Fatal("should've returned no preferences") + } + + if prefs := Must(store.Preference().GetAll(userId2)).(model.Preferences); len([]model.Preference(prefs)) != 0 { + t.Fatal("should've returned no preferences") + } +} diff --git a/store/store.go b/store/store.go index 730a923c5..48819e2d7 100644 --- a/store/store.go +++ b/store/store.go @@ -127,6 +127,7 @@ type PostStore interface { Save(post *model.Post) StoreChannel Update(newPost *model.Post, oldPost *model.Post) StoreChannel Get(id string) StoreChannel + GetSingle(id string) StoreChannel Delete(postId string, time int64) StoreChannel PermanentDeleteByUser(userId string) StoreChannel GetPosts(channelId string, offset int, limit int, allowFromCache bool) StoreChannel @@ -276,6 +277,7 @@ type PreferenceStore interface { GetAll(userId string) StoreChannel Delete(userId, category, name string) StoreChannel DeleteCategory(userId string, category string) StoreChannel + DeleteCategoryAndName(category string, name string) StoreChannel PermanentDeleteByUser(userId string) StoreChannel IsFeatureEnabled(feature, userId string) StoreChannel } -- cgit v1.2.3-1-g7c22