initial commit

This commit is contained in:
Gitea
2024-01-24 16:12:16 +03:00
commit 5b5dc2165c
23 changed files with 1846 additions and 0 deletions

View File

@ -0,0 +1,40 @@
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
}