diff options
Diffstat (limited to 'app/db/in_memory_repository.go')
| -rw-r--r-- | app/db/in_memory_repository.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/app/db/in_memory_repository.go b/app/db/in_memory_repository.go new file mode 100644 index 0000000..5b84dbf --- /dev/null +++ b/app/db/in_memory_repository.go @@ -0,0 +1,40 @@ +package db + +import ( + "github.com/xlgmokha/x/pkg/x" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls" +) + +type inMemoryRepository[T domain.Entity] struct { + items []T +} + +func NewRepository[T domain.Entity]() domain.Repository[T] { + return &inMemoryRepository[T]{ + items: []T{}, + } +} + +func (r *inMemoryRepository[T]) All() []T { + return r.items +} + +func (r *inMemoryRepository[T]) Find(id domain.ID) 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(domain.ID(pls.GenerateULID())) + } + + r.items = append(r.items, item) + return nil +} |
