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

64
internal/core/base.go Normal file
View File

@ -0,0 +1,64 @@
package core
import (
"context"
"fmt"
"time"
"github.com/rs/zerolog"
)
type BaseAction interface{}
type Action[Q, R any] interface {
BaseAction
Do(context.Context, Q) (R, error)
}
type ActionDecorator[Q, R any, A Action[Q, R]] interface {
Action[Q, R]
}
type baseAction struct {
env *Env
}
func newBaseAction(env *Env) baseAction {
return baseAction{
env: env,
}
}
func applyDecorators[Q, R any, A Action[Q, R]](action A) ActionDecorator[Q, R, A] {
return logActionDecorator[Q, R, A]{
action: action,
}
}
type logActionDecorator[Q, R any, A Action[Q, R]] struct {
action Action[Q, R]
}
func (d logActionDecorator[Q, R, A]) Do(ctx context.Context, params Q) (result R, err error) {
actionName := getTypeName[A]()
start := time.Now()
log := zerolog.Ctx(ctx).With().Str("action_name", actionName).Logger()
ctx = log.WithContext(ctx)
result, err = d.action.Do(ctx, params)
elapsed := time.Since(start)
if err != nil {
log.Warn().Err(err).Dur("elapsed", elapsed).Msg("action failed")
}
log.Info().Dur("elapsed", elapsed).Msg("action successed")
return result, err
}
func getTypeName[T any]() string {
var t T
out := fmt.Sprintf("%T", t)
return out
}

View File

@ -0,0 +1,45 @@
package core
import (
"context"
"fmt"
"git.loyso.art/frx/eway/internal/entity"
)
type CreateGoodsItemParams struct {
GoodsItems []entity.GoodsItem
}
type CreateGoodsItemResult struct {
GoodsItems []entity.GoodsItem
}
type createGoodsItemAction struct {
baseAction
}
type CreateGoodsItemAction Action[CreateGoodsItemParams, CreateGoodsItemResult]
func NewCreateGoodsItemAction(
env *Env,
) ActionDecorator[CreateGoodsItemParams, CreateGoodsItemResult, CreateGoodsItemAction] {
ba := newBaseAction(env)
action := &createGoodsItemAction{
baseAction: ba,
}
return applyDecorators(action)
}
func (a *createGoodsItemAction) Do(
ctx context.Context,
params CreateGoodsItemParams,
) (result CreateGoodsItemResult, err error) {
result.GoodsItems, err = a.env.repository.GoodsItem().UpsertMany(ctx, params.GoodsItems...)
if err != nil {
return result, fmt.Errorf("upserting items: %w", err)
}
return result, nil
}

11
internal/core/env.go Normal file
View File

@ -0,0 +1,11 @@
package core
import (
"git.loyso.art/frx/eway/internal/entity"
"git.loyso.art/frx/eway/internal/storage"
)
type Env struct {
repository storage.Repository
mapper entity.Mapper
}

View File

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

View File

@ -0,0 +1,38 @@
package core
import (
"context"
"fmt"
"git.loyso.art/frx/eway/internal/entity"
)
type ListCategoriesParams struct{}
type ListCategoriesResult struct {
Categories []entity.Category
}
type listCategoriesAction struct {
baseAction
}
type ListCategoriesAction Action[ListCategoriesParams, ListCategoriesResult]
func NewListCategoriesAction(env *Env) ActionDecorator[ListCategoriesParams, ListCategoriesResult, ListCategoriesAction] {
ba := newBaseAction(env)
action := &listCategoriesAction{
baseAction: ba,
}
return applyDecorators(action)
}
func (l *listCategoriesAction) Do(ctx context.Context, params ListCategoriesParams) (result ListCategoriesResult, err error) {
result.Categories, err = l.env.repository.Category().List(ctx)
if err != nil {
return result, fmt.Errorf("listing: %w", err)
}
return result, nil
}