diff --git a/pkg/api/api_test.go b/pkg/api/api_test.go index cb62641..584b636 100644 --- a/pkg/api/api_test.go +++ b/pkg/api/api_test.go @@ -13,8 +13,9 @@ import ( ) type mockFirebase struct { - set func(path string, v interface{}) (err error) - get func(path string) (result json.RawMessage, err error) + set func(path string, v interface{}) (err error) + get func(path string) (result json.RawMessage, err error) + filterEqual func(path, field string, value interface{}) (result json.RawMessage, err error) } func (m mockFirebase) Set(path string, v interface{}) (err error) { @@ -23,6 +24,9 @@ func (m mockFirebase) Set(path string, v interface{}) (err error) { func (m mockFirebase) Get(path string) (result json.RawMessage, err error) { return m.get(path) } +func (m mockFirebase) FilterEqual(path, field string, value interface{}) (result json.RawMessage, err error) { + return m.filterEqual(path, field, value) +} type mockSlack struct { oAuthAccess func(code string) (r *slack.OAuthAccessResponse, err error) diff --git a/pkg/firebase/firebase.go b/pkg/firebase/firebase.go index 0a64021..d3bcd08 100644 --- a/pkg/firebase/firebase.go +++ b/pkg/firebase/firebase.go @@ -18,6 +18,7 @@ type Firebase struct { type IFirebase interface { Set(path string, v interface{}) (err error) Get(path string) (result json.RawMessage, err error) + FilterEqual(path, field string, value interface{}) (result json.RawMessage, err error) } // New return new instance of the Firebase @@ -63,3 +64,15 @@ func (f *Firebase) Get(path string) (result json.RawMessage, err error) { return } + +// FilterEqual filter records with field equal to specific value +func (f *Firebase) FilterEqual(path, field string, value interface{}) (result json.RawMessage, err error) { + ref, err := f.realFirebase.Ref(path) + if err != nil { + return + } + + err = ref.OrderBy(field).EqualToValue(value).Value(&result) + + return +} diff --git a/pkg/storage/storage_test.go b/pkg/storage/storage_test.go index 91c6e53..6afcb7b 100644 --- a/pkg/storage/storage_test.go +++ b/pkg/storage/storage_test.go @@ -6,8 +6,9 @@ import ( ) type mockFirebase struct { - set func(path string, v interface{}) (err error) - get func(path string) (result json.RawMessage, err error) + set func(path string, v interface{}) (err error) + get func(path string) (result json.RawMessage, err error) + filterEqual func(path, field string, value interface{}) (result json.RawMessage, err error) } func (m mockFirebase) Set(path string, v interface{}) (err error) { @@ -17,6 +18,10 @@ func (m mockFirebase) Get(path string) (result json.RawMessage, err error) { return m.get(path) } +func (m mockFirebase) FilterEqual(path, field string, value interface{}) (result json.RawMessage, err error) { + return m.filterEqual(path, field, value) +} + func TestNew(t *testing.T) { fm := &mockFirebase{} diff --git a/pkg/storage/trash.go b/pkg/storage/trash.go index b888d92..5358a45 100644 --- a/pkg/storage/trash.go +++ b/pkg/storage/trash.go @@ -31,7 +31,7 @@ func (t *Trash) Set(trash *TrashData) (err error) { // GetNotPublished return all not published records func (t *Trash) GetNotPublished() (finResult map[string]*TrashData, err error) { - result, err := t.firebase.Get(trashPath) + result, err := t.firebase.FilterEqual(trashPath, "published", false) if err != nil { return } diff --git a/pkg/storage/trash_test.go b/pkg/storage/trash_test.go index 6b0909d..425c454 100644 --- a/pkg/storage/trash_test.go +++ b/pkg/storage/trash_test.go @@ -36,7 +36,7 @@ func TestTrashSetErr(t *testing.T) { func TestGetNotPublished(t *testing.T) { fm := &mockFirebase{} - fm.get = func(path string) (result json.RawMessage, err error) { + fm.filterEqual = func(path, field string, value interface{}) (result json.RawMessage, err error) { return []byte(`{"12":{"id":"12","data":"aaa","published":true},"23":{"id":"23","data":"bbb","published":false}}`), nil } @@ -55,7 +55,7 @@ func TestGetNotPublished(t *testing.T) { func TestGetNotPublishedErr(t *testing.T) { fm := &mockFirebase{} - fm.get = func(path string) (result json.RawMessage, err error) { + fm.filterEqual = func(path, field string, value interface{}) (result json.RawMessage, err error) { return []byte(``), errors.New("Oops") }