package db import ( "gitlab.com/mokhax/sparkled/pkg/domain" "gitlab.com/mokhax/sparkled/pkg/pls" ) type Repository interface { All() []*domain.Sparkle Each(func(*domain.Sparkle)) Save(*domain.Sparkle) error } type inMemoryRepository struct { sparkles []*domain.Sparkle } func NewRepository() Repository { return &inMemoryRepository{ sparkles: []*domain.Sparkle{}, } } func (r *inMemoryRepository) All() []*domain.Sparkle { return r.sparkles } func (r *inMemoryRepository) Each(visitor func(item *domain.Sparkle)) { for _, item := range r.All() { visitor(item) } } func (r *inMemoryRepository) Save(item *domain.Sparkle) error { if err := item.Validate(); err != nil { return err } if item.ID == "" { item.ID = pls.GenerateULID() } r.sparkles = append(r.sparkles, item) return nil }