summaryrefslogtreecommitdiffstats
path: root/store
diff options
context:
space:
mode:
authorSaturnino Abril <saturnino.abril@gmail.com>2017-04-04 02:34:14 +0900
committerCorey Hulen <corey@hulen.com>2017-04-03 10:34:14 -0700
commit43e795448f62fa86f3dad7940fc2297c44a6ea9c (patch)
treeaec7f651c2177f9cc02824f014bccd81e96c542a /store
parent6b61834ab14e9a4e51c29dd2904a1332c327aae6 (diff)
downloadchat-43e795448f62fa86f3dad7940fc2297c44a6ea9c.tar.gz
chat-43e795448f62fa86f3dad7940fc2297c44a6ea9c.tar.bz2
chat-43e795448f62fa86f3dad7940fc2297c44a6ea9c.zip
APIv4 post /teams/search (#5931)
Diffstat (limited to 'store')
-rw-r--r--store/sql_team_store.go42
-rw-r--r--store/sql_team_store_test.go127
-rw-r--r--store/store.go2
3 files changed, 170 insertions, 1 deletions
diff --git a/store/sql_team_store.go b/store/sql_team_store.go
index 7d843b030..39a39fa0b 100644
--- a/store/sql_team_store.go
+++ b/store/sql_team_store.go
@@ -257,6 +257,48 @@ func (s SqlTeamStore) SearchByName(name string) StoreChannel {
return storeChannel
}
+func (s SqlTeamStore) SearchAll(term string) StoreChannel {
+ storeChannel := make(StoreChannel, 1)
+
+ go func() {
+ result := StoreResult{}
+
+ var teams []*model.Team
+
+ if _, err := s.GetReplica().Select(&teams, "SELECT * FROM Teams WHERE Name LIKE :Term OR DisplayName LIKE :Term", map[string]interface{}{"Term": term + "%"}); err != nil {
+ result.Err = model.NewLocAppError("SqlTeamStore.SearchAll", "store.sql_team.search_all_team.app_error", nil, "term="+term+", "+err.Error())
+ }
+
+ result.Data = teams
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}
+
+func (s SqlTeamStore) SearchOpen(term string) StoreChannel {
+ storeChannel := make(StoreChannel, 1)
+
+ go func() {
+ result := StoreResult{}
+
+ var teams []*model.Team
+
+ if _, err := s.GetReplica().Select(&teams, "SELECT * FROM Teams WHERE Type = 'O' AND (Name LIKE :Term OR DisplayName LIKE :Term)", map[string]interface{}{"Term": term + "%"}); err != nil {
+ result.Err = model.NewLocAppError("SqlTeamStore.SearchOpen", "store.sql_team.search_open_team.app_error", nil, "term="+term+", "+err.Error())
+ }
+
+ result.Data = teams
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}
+
func (s SqlTeamStore) GetAll() StoreChannel {
storeChannel := make(StoreChannel, 1)
diff --git a/store/sql_team_store_test.go b/store/sql_team_store_test.go
index 3aeb14c9c..3599a6d1a 100644
--- a/store/sql_team_store_test.go
+++ b/store/sql_team_store_test.go
@@ -4,9 +4,10 @@
package store
import (
- "github.com/mattermost/platform/model"
"testing"
"time"
+
+ "github.com/mattermost/platform/model"
)
func TestTeamStoreSave(t *testing.T) {
@@ -156,6 +157,130 @@ func TestTeamStoreSearchByName(t *testing.T) {
}
}
+func TestTeamStoreSearchAll(t *testing.T) {
+ Setup()
+
+ o1 := model.Team{}
+ o1.DisplayName = "ADisplayName" + model.NewId()
+ o1.Name = "a" + model.NewId() + "a"
+ o1.Email = model.NewId() + "@nowhere.com"
+ o1.Type = model.TEAM_OPEN
+
+ if err := (<-store.Team().Save(&o1)).Err; err != nil {
+ t.Fatal(err)
+ }
+
+ p2 := model.Team{}
+ p2.DisplayName = "BDisplayName" + model.NewId()
+ p2.Name = "b" + model.NewId() + "b"
+ p2.Email = model.NewId() + "@nowhere.com"
+ p2.Type = model.TEAM_INVITE
+
+ if err := (<-store.Team().Save(&p2)).Err; err != nil {
+ t.Fatal(err)
+ }
+
+ r1 := <-store.Team().SearchAll(o1.Name)
+ if r1.Err != nil {
+ t.Fatal(r1.Err)
+ }
+ if len(r1.Data.([]*model.Team)) != 1 {
+ t.Fatal("should have returned 1 team")
+ }
+ if r1.Data.([]*model.Team)[0].ToJson() != o1.ToJson() {
+ t.Fatal("invalid returned team")
+ }
+
+ r1 = <-store.Team().SearchAll(p2.DisplayName)
+ if r1.Err != nil {
+ t.Fatal(r1.Err)
+ }
+ if len(r1.Data.([]*model.Team)) != 1 {
+ t.Fatal("should have returned 1 team")
+ }
+ if r1.Data.([]*model.Team)[0].ToJson() != p2.ToJson() {
+ t.Fatal("invalid returned team")
+ }
+
+ r1 = <-store.Team().SearchAll("junk")
+ if r1.Err != nil {
+ t.Fatal(r1.Err)
+ }
+ if len(r1.Data.([]*model.Team)) != 0 {
+ t.Fatal("should have not returned a team")
+ }
+}
+
+func TestTeamStoreSearchOpen(t *testing.T) {
+ Setup()
+
+ o1 := model.Team{}
+ o1.DisplayName = "ADisplayName" + model.NewId()
+ o1.Name = "a" + model.NewId() + "a"
+ o1.Email = model.NewId() + "@nowhere.com"
+ o1.Type = model.TEAM_OPEN
+
+ if err := (<-store.Team().Save(&o1)).Err; err != nil {
+ t.Fatal(err)
+ }
+
+ p2 := model.Team{}
+ p2.DisplayName = "BDisplayName" + model.NewId()
+ p2.Name = "b" + model.NewId() + "b"
+ p2.Email = model.NewId() + "@nowhere.com"
+ p2.Type = model.TEAM_INVITE
+
+ if err := (<-store.Team().Save(&p2)).Err; err != nil {
+ t.Fatal(err)
+ }
+
+ r1 := <-store.Team().SearchOpen(o1.Name)
+ if r1.Err != nil {
+ t.Fatal(r1.Err)
+ }
+ if len(r1.Data.([]*model.Team)) != 1 {
+ t.Fatal("should have returned 1 team")
+ }
+ if r1.Data.([]*model.Team)[0].ToJson() != o1.ToJson() {
+ t.Fatal("invalid returned team")
+ }
+
+ r1 = <-store.Team().SearchOpen(o1.DisplayName)
+ if r1.Err != nil {
+ t.Fatal(r1.Err)
+ }
+ if len(r1.Data.([]*model.Team)) != 1 {
+ t.Fatal("should have returned 1 team")
+ }
+ if r1.Data.([]*model.Team)[0].ToJson() != o1.ToJson() {
+ t.Fatal("invalid returned team")
+ }
+
+ r1 = <-store.Team().SearchOpen(p2.Name)
+ if r1.Err != nil {
+ t.Fatal(r1.Err)
+ }
+ if len(r1.Data.([]*model.Team)) != 0 {
+ t.Fatal("should have not returned a team")
+ }
+
+ r1 = <-store.Team().SearchOpen(p2.DisplayName)
+ if r1.Err != nil {
+ t.Fatal(r1.Err)
+ }
+ if len(r1.Data.([]*model.Team)) != 0 {
+ t.Fatal("should have not returned a team")
+ }
+
+ r1 = <-store.Team().SearchOpen("junk")
+ if r1.Err != nil {
+ t.Fatal(r1.Err)
+ }
+ if len(r1.Data.([]*model.Team)) != 0 {
+ t.Fatal("should have not returned a team")
+ }
+}
+
func TestTeamStoreGetByIniviteId(t *testing.T) {
Setup()
diff --git a/store/store.go b/store/store.go
index de70edb99..3e508dfa5 100644
--- a/store/store.go
+++ b/store/store.go
@@ -61,6 +61,8 @@ type TeamStore interface {
Get(id string) StoreChannel
GetByName(name string) StoreChannel
SearchByName(name string) StoreChannel
+ SearchAll(term string) StoreChannel
+ SearchOpen(term string) StoreChannel
GetAll() StoreChannel
GetAllPage(offset int, limit int) StoreChannel
GetAllTeamListing() StoreChannel