package api import ( "encoding/json" "fmt" "io" "log" "net/http" "gittea.marcokittel.de/elio/eliotools/datawriter/internal/database" ) type Container struct { Products map[string]int `json:"products"` Context Context `json:"context"` } type Product struct { Warehouse string `json:"warehouse"` Quantity int `json:"quantity"` Delivery int `json:"delivery_time"` } type OutgoingProducts struct { Products map[string][]Product `json:"products"` } func NewOutgoingProducts() *OutgoingProducts { op := OutgoingProducts{ Products: make(map[string][]Product), } return &op } type Context struct { Country string `json:"country"` State string `json:"state"` } func GetProductApiHandleFunc(nps *database.ProductService) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { return } defer r.Body.Close() data, err := io.ReadAll(r.Body) if err != nil { log.Println(err) return } var payload database.Container err = json.Unmarshal(data, &payload) if err != nil { log.Printf("Could not parse Json: %s", err) return } result, err := nps.FetchData(&payload) if err != nil { //Todo Fehlerhandling log.Println(err) } jsonResult, err := json.Marshal(result) if err != nil { log.Println(err) } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) fmt.Fprintln(w, string(jsonResult)) } }