Refactoring der ProduktService Api. Abhängigkeit via DI hinzufügen.
This commit is contained in:
parent
0eed845fc8
commit
ffd99d7938
|
|
@ -1,43 +1,15 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/api"
|
||||||
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/database"
|
"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 (
|
var (
|
||||||
connectionString = ""
|
connectionString = ""
|
||||||
port = ":8080"
|
port = ":8080"
|
||||||
|
|
@ -54,36 +26,8 @@ func main() {
|
||||||
fmt.Println("Connectionstring fehlt!. Bsp.: <user>:<passwort>@tcp(127.0.0.1:3306)/elio?parseTime=true")
|
fmt.Println("Connectionstring fehlt!. Bsp.: <user>:<passwort>@tcp(127.0.0.1:3306)/elio?parseTime=true")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//Env Variablen nutzen
|
//Dependency Injection
|
||||||
http.HandleFunc("/api/products", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/api/products", api.GetProductApiHandleFunc(nps))
|
||||||
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))
|
|
||||||
})
|
|
||||||
log.Printf("Easy Peasy: Die Party startet auf Port %s\n", port)
|
log.Printf("Easy Peasy: Die Party startet auf Port %s\n", port)
|
||||||
log.Printf("Probiers mal damit: %s\n", curlhelp)
|
log.Printf("Probiers mal damit: %s\n", curlhelp)
|
||||||
log.Fatal(http.ListenAndServe(port, nil))
|
log.Fatal(http.ListenAndServe(port, nil))
|
||||||
|
|
|
||||||
|
|
@ -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))
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue