summaryrefslogtreecommitdiff
path: root/pkg/db
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/db')
-rw-r--r--pkg/db/entity.go7
-rw-r--r--pkg/db/in_memory_repository.go39
-rw-r--r--pkg/db/in_memory_repository_test.go (renamed from pkg/db/repository_test.go)19
-rw-r--r--pkg/db/repository.go36
4 files changed, 65 insertions, 36 deletions
diff --git a/pkg/db/entity.go b/pkg/db/entity.go
new file mode 100644
index 0000000..1dcf4c3
--- /dev/null
+++ b/pkg/db/entity.go
@@ -0,0 +1,7 @@
+package db
+
+type Entity interface {
+ GetID() string
+ SetID(id string) error
+ Validate() error
+}
diff --git a/pkg/db/in_memory_repository.go b/pkg/db/in_memory_repository.go
new file mode 100644
index 0000000..5859c0b
--- /dev/null
+++ b/pkg/db/in_memory_repository.go
@@ -0,0 +1,39 @@
+package db
+
+import (
+ "github.com/xlgmokha/x/pkg/x"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls"
+)
+
+type inMemoryRepository[T Entity] struct {
+ items []T
+}
+
+func NewRepository[T Entity]() Repository[T] {
+ return &inMemoryRepository[T]{
+ items: []T{},
+ }
+}
+
+func (r *inMemoryRepository[T]) All() []T {
+ return r.items
+}
+
+func (r *inMemoryRepository[T]) Find(id string) T {
+ return x.Find(r.All(), func(item T) bool {
+ return item.GetID() == id
+ })
+}
+
+func (r *inMemoryRepository[T]) Save(item T) error {
+ if err := item.Validate(); err != nil {
+ return err
+ }
+
+ if item.GetID() == "" {
+ item.SetID(pls.GenerateULID())
+ }
+
+ r.items = append(r.items, item)
+ return nil
+}
diff --git a/pkg/db/repository_test.go b/pkg/db/in_memory_repository_test.go
index bb788d2..382a656 100644
--- a/pkg/db/repository_test.go
+++ b/pkg/db/in_memory_repository_test.go
@@ -8,7 +8,7 @@ import (
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/domain"
)
-func TestRepository(t *testing.T) {
+func TestInMemoryRepository(t *testing.T) {
storage := NewRepository[*domain.Sparkle]()
t.Run("Save", func(t *testing.T) {
@@ -30,4 +30,21 @@ func TestRepository(t *testing.T) {
assert.Equal(t, "because", sparkles[0].Reason)
})
})
+
+ t.Run("Find", func(t *testing.T) {
+ t.Run("when the entity exists", func(t *testing.T) {
+ sparkle, err := domain.NewSparkle("@tanuki for testing this func")
+ require.NoError(t, err)
+ require.NoError(t, storage.Save(sparkle))
+
+ result := storage.Find(sparkle.ID)
+ require.NotNil(t, result)
+ require.Equal(t, sparkle, result)
+ })
+
+ t.Run("when the entity does not exist", func(t *testing.T) {
+ result := storage.Find("unknown")
+ require.Nil(t, result)
+ })
+ })
}
diff --git a/pkg/db/repository.go b/pkg/db/repository.go
index 79c7ae3..397eee7 100644
--- a/pkg/db/repository.go
+++ b/pkg/db/repository.go
@@ -1,41 +1,7 @@
package db
-import "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls"
-
-type Entity interface {
- GetID() string
- SetID(id string) error
- Validate() error
-}
-
type Repository[T Entity] interface {
All() []T
+ Find(string) T
Save(T) error
}
-
-type inMemoryRepository[T Entity] struct {
- items []T
-}
-
-func NewRepository[T Entity]() Repository[T] {
- return &inMemoryRepository[T]{
- items: []T{},
- }
-}
-
-func (r *inMemoryRepository[T]) All() []T {
- return r.items
-}
-
-func (r *inMemoryRepository[T]) Save(item T) error {
- if err := item.Validate(); err != nil {
- return err
- }
-
- if item.GetID() == "" {
- item.SetID(pls.GenerateULID())
- }
-
- r.items = append(r.items, item)
- return nil
-}