add filters to cli and client

This commit is contained in:
Aleksandr Trushkin
2023-12-14 20:40:35 +03:00
parent 30c361eb34
commit e80d5145cf
3 changed files with 181 additions and 18 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"log/slog"
"strconv"
"strings"
"git.loyso.art/frx/kurious/internal/common/client/sravni"
"git.loyso.art/frx/kurious/internal/common/errors"
@ -35,9 +36,17 @@ func setupAPICommand(ctx context.Context) cli.Command {
WithOption(offsetOption).
WithAction(newListProductAction(ctx))
})
apiEducationFilterCount := buildCLICommand(func() cli.Command {
return cli.NewCommand("filter_count", "Loads counts of returned entities").
WithOption(learningTypeOpt).
WithOption(courseThematic).
WithOption(learningSelectionOpt).
WithAction(newProductsFilterCountAction(ctx))
})
apiEducation := cli.NewCommand("education", "Education related category").
WithCommand(apiEducationListProducts)
WithCommand(apiEducationListProducts).
WithCommand(apiEducationFilterCount)
return cli.NewCommand("api", "Interaction with API").
WithCommand(apiEducation)
@ -127,7 +136,7 @@ func (a *listProductsAction) parse(args []string, options map[string]string) err
func (a *listProductsAction) handle() error {
params := sravni.ListEducationProductsParams{
LearningType: a.params.learningType,
CoursesThematics: a.params.courseThematic,
CoursesThematics: []string{a.params.courseThematic},
Limit: a.params.limit,
Offset: a.params.offset,
}
@ -140,3 +149,65 @@ func (a *listProductsAction) handle() error {
return nil
}
type productsFilterCountActionParams struct {
learningType string
courseThematic []string
learningSelectionType string
}
type productsFilterCountAction struct {
*baseAction
client sravni.Client
params productsFilterCountActionParams
}
func newProductsFilterCountAction(ctx context.Context) cli.Action {
action := &productsFilterCountAction{
baseAction: newBaseAction(ctx),
}
return asCLIAction(action)
}
func (a *productsFilterCountAction) parse(args []string, options map[string]string) error {
err := a.baseAction.parse(args, options)
if err != nil {
return err
}
var ok bool
a.params.learningType, ok = options[learningTypeOptName]
if !ok {
return errors.SimpleError(learningTypeOptName + " is empty")
}
a.params.courseThematic = strings.Split(options[courseThematicOptName], ",")
a.params.learningSelectionType = options[learningTypeSelectionOptName]
client, err := makeSravniClient(a.ctx, a.log, options)
if err != nil {
return fmt.Errorf("making sravni client: %w", err)
}
a.client = client
return nil
}
func (a *productsFilterCountAction) handle() error {
params := sravni.ListEducationProductsParams{
LearningType: a.params.learningType,
CoursesThematics: a.params.courseThematic,
}
result, err := a.client.ListEducationalProductsFilterCount(a.ctx, params)
if err != nil {
return fmt.Errorf("listing education products: %w", err)
}
a.log.InfoContext(a.ctx, "list education products result", slog.Any("result", result))
return nil
}