Files
eway/internal/core/getcategory.go
2024-01-24 16:12:16 +03:00

50 lines
1022 B
Go

package core
import (
"context"
"fmt"
"git.loyso.art/frx/eway/internal/entity"
)
type GetCategoryParams struct {
ID int64
Name string
}
type GetCategoryResult struct {
Category entity.Category
}
type GetCategoryAction Action[GetCategoryParams, GetCategoryResult]
type getCategoryAction struct {
baseAction
}
func NewGetCategoryAction(env *Env) ActionDecorator[GetCategoryParams, GetCategoryResult, GetCategoryAction] {
ba := newBaseAction(env)
action := &getCategoryAction{
baseAction: ba,
}
return applyDecorators(action)
}
func (a *getCategoryAction) Do(ctx context.Context, params GetCategoryParams) (result GetCategoryResult, err error) {
id := params.ID
if params.Name != "" {
id, err = a.env.mapper.CategoryNameToID(ctx, params.Name)
if err != nil {
return result, fmt.Errorf("resolving category id: %w", err)
}
}
result.Category, err = a.env.repository.Category().Get(ctx, id)
if err != nil {
return result, fmt.Errorf("getting category: %w", err)
}
return result, nil
}