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.
60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
3 years ago
|
package test
|
||
|
|
||
|
import (
|
||
|
// "time"
|
||
|
"caj-larsson/bog/domain"
|
||
|
)
|
||
|
|
||
|
|
||
|
type MockUserAgentRepository struct {
|
||
|
IdIdx map[int64]*domain.UserAgent
|
||
|
NameIdx map[string]*domain.UserAgent
|
||
|
NextId int64
|
||
|
}
|
||
|
|
||
|
|
||
|
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
|
||
|
}
|