feat: get handlers return object slices

This commit is contained in:
2025-07-20 15:42:43 +02:00
parent 2e16c67321
commit 45712dacf6
11 changed files with 31 additions and 41 deletions

View File

@ -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,

View File

@ -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

View File

@ -3,5 +3,5 @@ package interfaces
import "io"
type Formatter interface {
Format(object any) (io.Reader, error)
Format(object []any) (io.Reader, error)
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}