blob: 5b84dbf65f26cee3a2e7daf4b36fcd57453c83d9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
}
|