Refactoring der ProduktService Api. Abhängigkeit via DI hinzufügen.

This commit is contained in:
Marco Kittel 2025-07-18 10:14:23 +02:00
parent 0eed845fc8
commit ffd99d7938
2 changed files with 73 additions and 59 deletions

View File

@ -1,43 +1,15 @@
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/api"
"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"`
}
var (
connectionString = ""
port = ":8080"
@ -54,36 +26,8 @@ func main() {
fmt.Println("Connectionstring fehlt!. Bsp.: <user>:<passwort>@tcp(127.0.0.1:3306)/elio?parseTime=true")
return
}
//Env Variablen nutzen
http.HandleFunc("/api/products", 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))
})
//Dependency Injection
http.HandleFunc("/api/products", api.GetProductApiHandleFunc(nps))
log.Printf("Easy Peasy: Die Party startet auf Port %s\n", port)
log.Printf("Probiers mal damit: %s\n", curlhelp)
log.Fatal(http.ListenAndServe(port, nil))

70
internal/api/products.go Normal file
View File

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