41 lines
836 B
Go
41 lines
836 B
Go
package inmemory
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"git.loyso.art/frx/eway/internal/entity"
|
|
"git.loyso.art/frx/eway/internal/storage"
|
|
)
|
|
|
|
type mapper struct {
|
|
categoryIDByName map[string]int64
|
|
base storage.Repository
|
|
}
|
|
|
|
func NewMapper(ctx context.Context, r storage.Repository) (*mapper, error) {
|
|
categories, err := r.Category().List(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("listing categories: %w", err)
|
|
}
|
|
|
|
catsByName := make(map[string]int64, len(categories))
|
|
for _, category := range categories {
|
|
catsByName[category.Name] = category.ID
|
|
}
|
|
|
|
return &mapper{
|
|
categoryIDByName: catsByName,
|
|
base: r,
|
|
}, nil
|
|
}
|
|
|
|
func (m *mapper) CategoryNameToID(ctx context.Context, name string) (int64, error) {
|
|
out, ok := m.categoryIDByName[name]
|
|
if !ok {
|
|
return 0, entity.ErrNotFound
|
|
}
|
|
|
|
return out, nil
|
|
}
|