188 lines
4.4 KiB
Go
188 lines
4.4 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/google/uuid"
|
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/api"
|
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/database"
|
|
)
|
|
|
|
var (
|
|
connectionString = ""
|
|
port = ":8080"
|
|
)
|
|
|
|
type (
|
|
//RegistrierungsID -> ProductID -> Product
|
|
ProductStore map[string]map[string]database.Product
|
|
)
|
|
|
|
const (
|
|
curlhelp = `
|
|
Bewerbungsaufgabe von Marco Kittel 2025
|
|
|
|
Produkte abrufen
|
|
curl -X POST localhost:8080/api/products -d '{ "products": { "A6053": 2, "B3009": 1200 }, "context": { "country": "EU", "state": "" } }'
|
|
|
|
Reservierung reservieren
|
|
curl -X POST localhost:8080/api/products/reserve -d '{"id":"ab0d7184-a4ce-4802-897a-d8597335143a"}'
|
|
|
|
Reservierung bestätigen
|
|
curl -X POST localhost:8080/api/products/confirm -d '{"id":"ab0d7184-a4ce-4802-897a-d8597335143a"}'
|
|
|
|
Reservierung abbrechen
|
|
curl -X POST localhost:8080/api/products/abort -d '{"id":"ab0d7184-a4ce-4802-897a-d8597335143a"}'
|
|
|
|
Reservierung freigeben
|
|
curl -X POST localhost:8080/api/products/release -d '{"id":"ab0d7184-a4ce-4802-897a-d8597335143a"}'
|
|
`
|
|
)
|
|
|
|
func main() {
|
|
connectionString := os.Getenv("CONNECTIONSTRING")
|
|
nps := database.NewProductService(connectionString)
|
|
//Hier prüfe ich nach alten Registrierungen und gebe Sie frei.
|
|
nps.Autorelease()
|
|
if len(connectionString) == 0 {
|
|
fmt.Println("Connectionstring fehlt!. Bsp.: <user>:<passwort>@tcp(127.0.0.1:3306)/elio?parseTime=true")
|
|
return
|
|
}
|
|
//Dependency Injection
|
|
http.HandleFunc("/api/products", api.GetProductApiHandleFunc(nps))
|
|
|
|
//Todo schöner machen
|
|
http.HandleFunc("/api/products/reserve", 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
|
|
}
|
|
groupId := uuid.New().String()
|
|
result, err := nps.FetchReservationData(&payload, database.UUID(groupId))
|
|
if err != nil {
|
|
//Todo Fehlerhandling
|
|
log.Println(err)
|
|
}
|
|
if len(result) == 0 {
|
|
w.WriteHeader(http.StatusNoContent)
|
|
fmt.Fprintln(w)
|
|
return
|
|
}
|
|
_, err = nps.ReserviereBestellungen(result, database.UUID(groupId))
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
jsonResult, err := json.Marshal(result[0])
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
fmt.Fprintln(w, string(jsonResult))
|
|
})
|
|
|
|
http.HandleFunc("/api/products/confirm", 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
|
|
}
|
|
payload := struct {
|
|
Id string `json:"id"`
|
|
}{}
|
|
err = json.Unmarshal(data, &payload)
|
|
if err != nil {
|
|
//Todo Fehlerhandling
|
|
log.Println(err)
|
|
}
|
|
err = nps.ConfirmBestellung(database.UUID(payload.Id))
|
|
if err != nil {
|
|
//Todo Fehlerhandling
|
|
log.Println(err)
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
fmt.Fprintln(w)
|
|
return
|
|
})
|
|
|
|
http.HandleFunc("/api/products/release", 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
|
|
}
|
|
payload := struct {
|
|
Id string `json:"id"`
|
|
}{}
|
|
err = json.Unmarshal(data, &payload)
|
|
if err != nil {
|
|
//Todo Fehlerhandling
|
|
log.Println(err)
|
|
}
|
|
err = nps.ReleaseBestellung(database.UUID(payload.Id))
|
|
if err != nil {
|
|
//Todo Fehlerhandling
|
|
log.Println(err)
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
fmt.Fprintln(w)
|
|
return
|
|
})
|
|
|
|
http.HandleFunc("/api/products/abort", 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
|
|
}
|
|
payload := struct {
|
|
Id string `json:"id"`
|
|
}{}
|
|
err = json.Unmarshal(data, &payload)
|
|
if err != nil {
|
|
//Todo Fehlerhandling
|
|
log.Println(err)
|
|
}
|
|
err = nps.AbortBestellung(database.UUID(payload.Id))
|
|
if err != nil {
|
|
//Todo Fehlerhandling
|
|
log.Println(err)
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
fmt.Fprintln(w)
|
|
return
|
|
})
|
|
|
|
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))
|
|
}
|