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