You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bog/infrastructure/memory/namespace/repository.go

62 lines
1.3 KiB
Go

package namespace
import (
// "time"
"caj-larsson/bog/dataswamp/namespace"
)
type Repository struct {
IdIdx map[int64]*namespace.Namespace
NameIdx map[string]*namespace.Namespace
NextId int64
}
func NewRepository() *Repository {
r := new(Repository)
r.NextId = 0
r.IdIdx = make(map[int64]*namespace.Namespace)
r.NameIdx = make(map[string]*namespace.Namespace)
return r
}
func (r *Repository) Create(ns namespace.Namespace) (*namespace.Namespace, error) {
r.NextId += 1
ns.ID = r.NextId
r.IdIdx[ns.ID] = &ns
r.NameIdx[ns.Name] = &ns
return &ns, nil
}
func (r *Repository) All() ([]namespace.Namespace, error) {
ns := make([]namespace.Namespace, 0, len(r.IdIdx))
for _, value := range r.IdIdx {
ns = append(ns, *value)
}
return ns, nil
}
func (r *Repository) GetByName(name string) (*namespace.Namespace, error) {
ns, exists := r.NameIdx[name]
if exists {
return ns, nil
}
return nil, namespace.ErrNotExists
}
func (r *Repository) Update(id int64, ns namespace.Namespace) (*namespace.Namespace, error) {
original := *r.IdIdx[id]
ns.ID = id
r.IdIdx[id] = &ns
r.NameIdx[original.Name] = &ns
return &ns, nil
}
func (r *Repository) Delete(id int64) error {
original := *r.IdIdx[id]
delete(r.NameIdx, original.Name)
delete(r.IdIdx, original.ID)
return nil
}