From 45712dacf6492deb00f55c3fc4adcd80fb75a864 Mon Sep 17 00:00:00 2001 From: bdoerfchen Date: Sun, 20 Jul 2025 15:42:43 +0200 Subject: [PATCH] feat: get handlers return object slices --- .vscode/launch.json | 5 +++- cmd/resources.go | 8 +++--- cmd/root.go | 2 -- cmd/verbs.go | 10 +++---- .../{menu/menu.go => dishes/handler.go} | 26 +++++++++---------- core/interfaces/handler.go | 2 +- core/interfaces/output.go | 2 +- core/output/go.go | 4 +-- core/output/json.go | 4 +-- core/output/yaml.go | 4 +-- model/resources/{menu.go => dish.go} | 5 ---- 11 files changed, 31 insertions(+), 41 deletions(-) rename core/handler/{menu/menu.go => dishes/handler.go} (80%) rename model/resources/{menu.go => dish.go} (95%) diff --git a/.vscode/launch.json b/.vscode/launch.json index a49da6a..edf630b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,7 +11,10 @@ "mode": "debug", "program": "${workspaceFolder}/main.go", "args": [ - "get", "menu", "-o", "yaml", "--print-config", "true" + "get", "dishes", + "-o", "yaml", + "-d", "2025-07-21", + "--print-config" ] } ] diff --git a/cmd/resources.go b/cmd/resources.go index b93e26e..3aafc30 100644 --- a/cmd/resources.go +++ b/cmd/resources.go @@ -1,7 +1,7 @@ package cmd import ( - "git.bissendorf.co/bissendorf/unifood/m/v2/core/handler/menu" + "git.bissendorf.co/bissendorf/unifood/m/v2/core/handler/dishes" "git.bissendorf.co/bissendorf/unifood/m/v2/core/interfaces" "git.bissendorf.co/bissendorf/unifood/m/v2/core/services/stwhbclient" "git.bissendorf.co/bissendorf/unifood/m/v2/model/external/stwbremen" @@ -9,10 +9,10 @@ import ( var availableResources = []interfaces.ResourceCommand[any]{ { - Name: "menu", - Aliases: []string{"m"}, + Name: "dishes", + Aliases: []string{"dish", "d"}, Verbs: []interfaces.Verb{interfaces.VerbGet}, - Handler: &menu.MenuHandler{ + Handler: &dishes.DishesHandler{ QueryClient: stwhbclient.New[[]stwbremen.Dish](), }, }, diff --git a/cmd/root.go b/cmd/root.go index 804832e..5f27db3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -37,7 +37,5 @@ func initRootCmd() { logger := jlog.New(slog.LevelDebug) ctx := jlog.ContextWith(context.Background(), logger) - logger.Debug("Register verb commands") rootCmd.AddCommand(getVerbs(ctx, &appConfig)...) - logger.Debug("Verb commands registered successfully") } diff --git a/cmd/verbs.go b/cmd/verbs.go index 6775c9c..8cfbf78 100644 --- a/cmd/verbs.go +++ b/cmd/verbs.go @@ -33,8 +33,8 @@ var verbs = []VerbItem{ return fmt.Errorf("resource does not support GET") } - // Get item - item, err := h.Get(ctx, params) + // Get items + items, err := h.Get(ctx, params) if err != nil { return fmt.Errorf("retrieving item failed: %w", err) } @@ -46,7 +46,7 @@ var verbs = []VerbItem{ } // Format and output - formatted, err := formatter.Format(item) + formatted, err := formatter.Format(items) if err != nil { return fmt.Errorf("failed to format output: %w", err) } @@ -61,8 +61,6 @@ var verbs = []VerbItem{ } func getVerbs(ctx context.Context, config *AppConfig) (commands []*cobra.Command) { - logger := jlog.FromContext(ctx) - for _, v := range verbs { verbCommand := &cobra.Command{ Use: strings.ToLower(string(v.Name)), @@ -115,8 +113,6 @@ func getVerbs(ctx context.Context, config *AppConfig) (commands []*cobra.Command ) } verbCommand.AddCommand(resourceCommand) - - logger.Debug(fmt.Sprintf("Registered %s %s", strings.ToUpper(string(v.Name)), r.Name)) } commands = append(commands, verbCommand) diff --git a/core/handler/menu/menu.go b/core/handler/dishes/handler.go similarity index 80% rename from core/handler/menu/menu.go rename to core/handler/dishes/handler.go index df23836..cd9ff5f 100644 --- a/core/handler/menu/menu.go +++ b/core/handler/dishes/handler.go @@ -1,4 +1,4 @@ -package menu +package dishes import ( "context" @@ -12,7 +12,7 @@ import ( "git.bissendorf.co/bissendorf/unifood/m/v2/util" ) -type MenuHandler struct { +type DishesHandler struct { interfaces.ResourceHandler interfaces.GetHandler @@ -24,7 +24,7 @@ const ( paramLocation = "location" ) -func (h *MenuHandler) Get(ctx context.Context, params params.Container) (any, error) { +func (h *DishesHandler) Get(ctx context.Context, params params.Container) ([]any, error) { // Read parameters p, err := params.GetValue(paramDate) if err != nil { @@ -55,20 +55,18 @@ func (h *MenuHandler) Get(ctx context.Context, params params.Container) (any, er } // Return - return &resources.Menu{ - Location: location.(string), - Dishes: util.Transform(*dishes, func(i *stwbremen.Dish) resources.Dish { - d, err := resources.DishFromDTO(*i) - if err != nil { - return resources.Dish{} - } + return util.Transform(*dishes, func(i *stwbremen.Dish) any { + d, err := resources.DishFromDTO(*i) + if err != nil { + return resources.Dish{} + } + + return *d + }), nil - return *d - }), - }, nil } -func (h *MenuHandler) GetParametersForVerb(verb interfaces.Verb) []params.Registration { +func (h *DishesHandler) GetParametersForVerb(verb interfaces.Verb) []params.Registration { return []params.Registration{ { Name: paramDate, diff --git a/core/interfaces/handler.go b/core/interfaces/handler.go index 40e488f..d92ff54 100644 --- a/core/interfaces/handler.go +++ b/core/interfaces/handler.go @@ -19,7 +19,7 @@ type ResourceHandler interface { } type GetHandler interface { - Get(ctx context.Context, params params.Container) (any, error) + Get(ctx context.Context, params params.Container) ([]any, error) } type Verb string diff --git a/core/interfaces/output.go b/core/interfaces/output.go index 2af3a09..6f22941 100644 --- a/core/interfaces/output.go +++ b/core/interfaces/output.go @@ -3,5 +3,5 @@ package interfaces import "io" type Formatter interface { - Format(object any) (io.Reader, error) + Format(object []any) (io.Reader, error) } diff --git a/core/output/go.go b/core/output/go.go index 4cf80bf..3e73d57 100644 --- a/core/output/go.go +++ b/core/output/go.go @@ -8,6 +8,6 @@ import ( type GoFormatter struct{} -func (f *GoFormatter) Format(object any) (io.Reader, error) { - return strings.NewReader(fmt.Sprintf("%#v", object)), nil +func (f *GoFormatter) Format(objects []any) (io.Reader, error) { + return strings.NewReader(fmt.Sprintf("%#v", objects)), nil } diff --git a/core/output/json.go b/core/output/json.go index 2aa27ec..bff02d7 100644 --- a/core/output/json.go +++ b/core/output/json.go @@ -8,8 +8,8 @@ import ( type JsonFormatter struct{} -func (f *JsonFormatter) Format(object any) (io.Reader, error) { +func (f *JsonFormatter) Format(objects []any) (io.Reader, error) { var buffer = make([]byte, 0, 1024) outputBuffer := bytes.NewBuffer(buffer) - return outputBuffer, json.NewEncoder(outputBuffer).Encode(object) + return outputBuffer, json.NewEncoder(outputBuffer).Encode(objects) } diff --git a/core/output/yaml.go b/core/output/yaml.go index 020dedb..62fbb5c 100644 --- a/core/output/yaml.go +++ b/core/output/yaml.go @@ -9,8 +9,8 @@ import ( type YamlFormatter struct{} -func (f *YamlFormatter) Format(object any) (io.Reader, error) { - buffer, err := yaml.Marshal(object) +func (f *YamlFormatter) Format(objects []any) (io.Reader, error) { + buffer, err := yaml.Marshal(objects) if err != nil { return nil, err } diff --git a/model/resources/menu.go b/model/resources/dish.go similarity index 95% rename from model/resources/menu.go rename to model/resources/dish.go index cfd0e62..ebf8ab2 100644 --- a/model/resources/menu.go +++ b/model/resources/dish.go @@ -10,11 +10,6 @@ import ( "git.bissendorf.co/bissendorf/unifood/m/v2/util" ) -type Menu struct { - Location string - Dishes []Dish -} - func DishFromDTO(dish stwbremen.Dish) (*Dish, error) { date, err := time.Parse(time.DateOnly, dish.Date) if err != nil {