initial commit
This commit is contained in:
40
internal/storage/inmemory/mapper.go
Normal file
40
internal/storage/inmemory/mapper.go
Normal 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
|
||||
}
|
||||
8
internal/storage/storage.go
Normal file
8
internal/storage/storage.go
Normal file
@ -0,0 +1,8 @@
|
||||
package storage
|
||||
|
||||
import "git.loyso.art/frx/eway/internal/entity"
|
||||
|
||||
type Repository interface {
|
||||
Category() entity.CategoryRepository
|
||||
GoodsItem() entity.GoodsItemRepository
|
||||
}
|
||||
Reference in New Issue
Block a user