diff --git a/cmd/shell/main.go b/cmd/shell/main.go index 551a513..afd9193 100644 --- a/cmd/shell/main.go +++ b/cmd/shell/main.go @@ -35,7 +35,7 @@ func main() { defer cancel() var wg sync.WaitGroup - db := database.NewDatabaseWriter(connectionstr) + db := database.NewDatabaseWriter(ctx, connectionstr) ds := dataservice.NewDataService(ctx) ds.AddListener(db) wg.Add(ds.ListenerCount()) diff --git a/cmd/websrv/main.go b/cmd/websrv/main.go index 77a5257..567904d 100644 --- a/cmd/websrv/main.go +++ b/cmd/websrv/main.go @@ -12,6 +12,7 @@ import ( "gittea.marcokittel.de/elio/eliotools/datawriter/internal/api" "gittea.marcokittel.de/elio/eliotools/datawriter/internal/database" + "gittea.marcokittel.de/elio/eliotools/datawriter/internal/models" ) var ( @@ -21,7 +22,7 @@ var ( type ( //RegistrierungsID -> ProductID -> Product - ProductStore map[string]map[string]database.Product + ProductStore map[string]map[string]models.Product ) const ( diff --git a/internal/api/abortproducts.go b/internal/api/abortproducts.go index a714800..f45ff9f 100644 --- a/internal/api/abortproducts.go +++ b/internal/api/abortproducts.go @@ -8,6 +8,7 @@ import ( "net/http" "gittea.marcokittel.de/elio/eliotools/datawriter/internal/database" + "gittea.marcokittel.de/elio/eliotools/datawriter/internal/models" ) func GetAbortApiHandleFunc(nps *database.ProductService) http.HandlerFunc { @@ -29,7 +30,7 @@ func GetAbortApiHandleFunc(nps *database.ProductService) http.HandlerFunc { //Todo Fehlerhandling log.Println(err) } - err = nps.AbortBestellung(database.UUID(payload.Id)) + err = nps.AbortBestellung(models.UUID(payload.Id)) if err != nil { //Todo Fehlerhandling log.Println(err) diff --git a/internal/api/confirmproducts.go b/internal/api/confirmproducts.go index f4a8c13..d47d48a 100644 --- a/internal/api/confirmproducts.go +++ b/internal/api/confirmproducts.go @@ -8,6 +8,7 @@ import ( "net/http" "gittea.marcokittel.de/elio/eliotools/datawriter/internal/database" + "gittea.marcokittel.de/elio/eliotools/datawriter/internal/models" ) func GetConfirmReservationApiHandleFunc(nps *database.ProductService) http.HandlerFunc { @@ -29,7 +30,7 @@ func GetConfirmReservationApiHandleFunc(nps *database.ProductService) http.Handl //Todo Fehlerhandling log.Println(err) } - err = nps.ConfirmBestellung(database.UUID(payload.Id)) + err = nps.ConfirmBestellung(models.UUID(payload.Id)) if err != nil { //Todo Fehlerhandling log.Println(err) diff --git a/internal/api/fetchproducts.go b/internal/api/fetchproducts.go index f268fe0..8102f2f 100644 --- a/internal/api/fetchproducts.go +++ b/internal/api/fetchproducts.go @@ -8,6 +8,7 @@ import ( "net/http" "gittea.marcokittel.de/elio/eliotools/datawriter/internal/database" + "gittea.marcokittel.de/elio/eliotools/datawriter/internal/models" ) func GetProductApiHandleFunc(nps *database.ProductService) http.HandlerFunc { @@ -21,7 +22,7 @@ func GetProductApiHandleFunc(nps *database.ProductService) http.HandlerFunc { log.Println(err) return } - var payload database.Container + var payload models.Container err = json.Unmarshal(data, &payload) if err != nil { log.Printf("Could not parse Json: %s", err) diff --git a/internal/api/releaseproducts.go b/internal/api/releaseproducts.go index dcfb235..151d843 100644 --- a/internal/api/releaseproducts.go +++ b/internal/api/releaseproducts.go @@ -8,6 +8,7 @@ import ( "net/http" "gittea.marcokittel.de/elio/eliotools/datawriter/internal/database" + "gittea.marcokittel.de/elio/eliotools/datawriter/internal/models" ) func GetReleaseReservationApiHandleFunc(nps *database.ProductService) http.HandlerFunc { @@ -29,7 +30,7 @@ func GetReleaseReservationApiHandleFunc(nps *database.ProductService) http.Handl //Todo Fehlerhandling log.Println(err) } - err = nps.ReleaseBestellung(database.UUID(payload.Id)) + err = nps.ReleaseBestellung(models.UUID(payload.Id)) if err != nil { //Todo Fehlerhandling log.Println(err) diff --git a/internal/api/reserveproducts.go b/internal/api/reserveproducts.go index 890378e..4fce4e4 100644 --- a/internal/api/reserveproducts.go +++ b/internal/api/reserveproducts.go @@ -9,6 +9,7 @@ import ( "github.com/google/uuid" "gittea.marcokittel.de/elio/eliotools/datawriter/internal/database" + "gittea.marcokittel.de/elio/eliotools/datawriter/internal/models" ) func GetProductReservationApiHandleFunc(nps *database.ProductService) http.HandlerFunc { @@ -22,14 +23,14 @@ func GetProductReservationApiHandleFunc(nps *database.ProductService) http.Handl log.Println(err) return } - var payload database.Container + var payload models.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)) + result, err := nps.FetchReservationData(&payload, models.UUID(groupId)) if err != nil { //Todo Fehlerhandling log.Println(err) @@ -39,7 +40,7 @@ func GetProductReservationApiHandleFunc(nps *database.ProductService) http.Handl fmt.Fprintln(w) return } - _, err = nps.ReserviereBestellungen(result, database.UUID(groupId)) + _, err = nps.ReserviereBestellungen(result, models.UUID(groupId)) if err != nil { log.Println(err) } diff --git a/internal/database/database.go b/internal/database/database.go index 058f39c..d126668 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -11,6 +11,7 @@ import ( _ "github.com/go-sql-driver/mysql" "github.com/google/uuid" + "gittea.marcokittel.de/elio/eliotools/datawriter/internal/models" "gittea.marcokittel.de/elio/eliotools/eliofile" "gittea.marcokittel.de/elio/eliotools/logger" ) @@ -25,8 +26,6 @@ type DatabaseReader struct { DatabaseWriter } -type UUID string - // Das ist ein Golang Design Flaw... Context so zu übergeben, bitte nicht hauen func NewDatabaseReader(ctx context.Context, connectionString string) *DatabaseReader { return &DatabaseReader{*NewDatabaseWriter(ctx, connectionString)} @@ -175,15 +174,7 @@ func (d *DatabaseWriter) UpdateOrInsertDelivery(fromcountry string, tocountry st return nil } -type ProductDelivery struct { - Id int //Warehouse Id - Warehouse string - Amount int - DeliveryDays int - DeliveryId int -} - -func (d *DatabaseReader) GetProductByProductIdDeliveryCountryAndState(prod_id, delivery_country, delivery_country_state string) ([]ProductDelivery, error) { +func (d *DatabaseReader) GetProductByProductIdDeliveryCountryAndState(prod_id, delivery_country, delivery_country_state string) ([]models.ProductDelivery, error) { ctx, cancel := context.WithTimeout(d.ctx, 100*time.Millisecond) defer cancel() stmt := ` @@ -209,9 +200,9 @@ func (d *DatabaseReader) GetProductByProductIdDeliveryCountryAndState(prod_id, d return nil, err } defer rows.Close() - var products []ProductDelivery + var products []models.ProductDelivery for rows.Next() { - var pd ProductDelivery + var pd models.ProductDelivery if err := rows.Scan(&pd.Id, &pd.Warehouse, &pd.Amount, &pd.DeliveryDays, &pd.DeliveryId); err != nil { return products, err } @@ -220,7 +211,7 @@ func (d *DatabaseReader) GetProductByProductIdDeliveryCountryAndState(prod_id, d return products, nil } -func (d *DatabaseReader) GetReservationStateById(productId UUID) (string, error) { +func (d *DatabaseReader) GetReservationStateById(productId models.UUID) (string, error) { ctx, cancel := context.WithTimeout(d.ctx, 100*time.Millisecond) defer cancel() stmt := ` @@ -240,7 +231,7 @@ func (d *DatabaseReader) GetReservationStateById(productId UUID) (string, error) return status, nil } -func (d *DatabaseReader) GetReservationItemsByGroupId(groupId UUID) (string, error) { +func (d *DatabaseReader) GetReservationItemsByGroupId(groupId models.UUID) (string, error) { ctx, cancel := context.WithTimeout(d.ctx, 100*time.Millisecond) defer cancel() stmt := ` @@ -260,7 +251,7 @@ func (d *DatabaseReader) GetReservationItemsByGroupId(groupId UUID) (string, err return status, nil } -func (d *DatabaseWriter) updateReservationState(Id UUID, status string) error { +func (d *DatabaseWriter) updateReservationState(Id models.UUID, status string) error { ctx, cancel := context.WithTimeout(d.ctx, 100*time.Millisecond) defer cancel() _, err := d.db.ExecContext(ctx, "UPDATE reservations SET Status=? WHERE id=?", status, Id) @@ -270,7 +261,7 @@ func (d *DatabaseWriter) updateReservationState(Id UUID, status string) error { return nil } -func (d *DatabaseWriter) updateReservationStateByGroupId(groupId UUID, status string) error { +func (d *DatabaseWriter) updateReservationStateByGroupId(groupId models.UUID, status string) error { ctx, cancel := context.WithTimeout(d.ctx, 100*time.Millisecond) defer cancel() _, err := d.db.ExecContext(ctx, "UPDATE reservations SET Status=? WHERE reservationGroupId=?", status, groupId) @@ -280,37 +271,37 @@ func (d *DatabaseWriter) updateReservationStateByGroupId(groupId UUID, status st return nil } -func (d *DatabaseWriter) AbortReservation(Id UUID) error { +func (d *DatabaseWriter) AbortReservation(Id models.UUID) error { return d.updateReservationState(Id, "ABORTED") } -func (d *DatabaseWriter) ConfirmReservation(Id UUID) error { +func (d *DatabaseWriter) ConfirmReservation(Id models.UUID) error { return d.updateReservationState(Id, "CONFIRMED") } -func (d *DatabaseWriter) ReleaseReservation(Id UUID) error { +func (d *DatabaseWriter) ReleaseReservation(Id models.UUID) error { return d.updateReservationState(Id, "RELEASED") } -func (d *DatabaseWriter) AbortReservationGroup(groupId UUID) error { +func (d *DatabaseWriter) AbortReservationGroup(groupId models.UUID) error { return d.updateReservationStateByGroupId(groupId, "ABORTED") } -func (d *DatabaseWriter) ConfirmReservationGroup(groupId UUID) error { +func (d *DatabaseWriter) ConfirmReservationGroup(groupId models.UUID) error { return d.updateReservationStateByGroupId(groupId, "CONFIRMED") } -func (d *DatabaseWriter) ReleaseReservationGroup(groupId UUID) error { +func (d *DatabaseWriter) ReleaseReservationGroup(groupId models.UUID) error { return d.updateReservationStateByGroupId(groupId, "RELEASED") } -func (d *DatabaseWriter) ReserveReservationItem(deliveryId, warehouseId, amount int, groupId UUID) (UUID, error) { +func (d *DatabaseWriter) ReserveReservationItem(deliveryId, warehouseId, amount int, groupId models.UUID) (models.UUID, error) { newUUID := uuid.New().String() _, err := d.db.Exec("INSERT INTO reservations (id, deliveryId, warehouseId, amount, status, reservationGroupId) VALUES (?,?,?,?,'RESERVED', ?)", newUUID, deliveryId, warehouseId, amount, groupId) if err != nil { return "", err } - return UUID(newUUID), nil + return models.UUID(newUUID), nil } func (db *DatabaseWriter) HandleData(ctx context.Context, data eliofile.CountryCsvData) error { diff --git a/internal/database/database_test.go b/internal/database/database_test.go index d0c97b0..d9825a0 100644 --- a/internal/database/database_test.go +++ b/internal/database/database_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/google/uuid" + "gittea.marcokittel.de/elio/eliotools/datawriter/internal/models" ) // Unit Tests sollten niemals auf der Produktivdatenbank stattfinden. @@ -133,7 +134,7 @@ func TestInsertStatement(t *testing.T) { } groupId := uuid.New().String() for _, item := range data { - uuid, err := dbr.DatabaseWriter.ReserveReservationItem(item.DeliveryId, item.Id, item.Amount, UUID(groupId)) + uuid, err := dbr.DatabaseWriter.ReserveReservationItem(item.DeliveryId, item.Id, item.Amount, models.UUID(groupId)) if err != nil { t.Errorf("Das Einfügen einer neuen Reservierung darf nicht fehlschlagen! %s", err) } @@ -153,7 +154,7 @@ func TestAbortReservationStatement(t *testing.T) { } groupId := uuid.New().String() for _, item := range data { - uuid, err := dbr.DatabaseWriter.ReserveReservationItem(item.DeliveryId, item.Id, item.Amount, UUID(groupId)) + uuid, err := dbr.DatabaseWriter.ReserveReservationItem(item.DeliveryId, item.Id, item.Amount, models.UUID(groupId)) if err != nil { t.Errorf("Das Einfügen einer neuen Reservierung darf nicht fehlschlagen! %s", err) } @@ -180,7 +181,7 @@ func TestConfirmReservationStatement(t *testing.T) { } groupId := uuid.New().String() for _, item := range data { - uuid, err := dbr.DatabaseWriter.ReserveReservationItem(item.DeliveryId, item.Id, item.Amount, UUID(groupId)) + uuid, err := dbr.DatabaseWriter.ReserveReservationItem(item.DeliveryId, item.Id, item.Amount, models.UUID(groupId)) if err != nil { t.Errorf("Das Einfügen einer neuen Reservierung darf nicht fehlschlagen! %s", err) } @@ -207,7 +208,7 @@ func TestReleasedReservationStatement(t *testing.T) { } groupId := uuid.New().String() for _, item := range data { - uuid, err := dbr.DatabaseWriter.ReserveReservationItem(item.DeliveryId, item.Id, item.Amount, UUID(groupId)) + uuid, err := dbr.DatabaseWriter.ReserveReservationItem(item.DeliveryId, item.Id, item.Amount, models.UUID(groupId)) if err != nil { t.Errorf("Das Einfügen einer neuen Reservierung darf nicht fehlschlagen! %s", err) } @@ -227,7 +228,7 @@ func TestReleasedReservationStatement(t *testing.T) { func TestFetchReservationData(t *testing.T) { payloadStr := `{ "products": { "A6053": 30 }, "context": { "country": "EU", "state": "" }}` - var payload Container + var payload models.Container err := json.Unmarshal([]byte(payloadStr), &payload) if err != nil { t.Errorf("Dieser Fehler beim Konvertieren der JSON Payload sollte nicht passieren! \n%s", err) @@ -243,7 +244,7 @@ func TestFetchReservationData(t *testing.T) { func TestFetchDataReservation(t *testing.T) { payloadStr := `{ "products": { "A6053": 30 }, "context": { "country": "EU", "state": "" }}` - var payload Container + var payload models.Container err := json.Unmarshal([]byte(payloadStr), &payload) if err != nil { t.Errorf("Dieser Fehler beim Konvertieren der JSON Payload sollte nicht passieren! \n%s", err) @@ -252,7 +253,7 @@ func TestFetchDataReservation(t *testing.T) { ctx := context.Background() ps := NewProductService(ctx, connectionString) groupId := uuid.New().String() - op, err := ps.FetchReservationData(&payload, UUID(groupId)) + op, err := ps.FetchReservationData(&payload, models.UUID(groupId)) if err != nil { t.Errorf("Das Datafetchen und umwandeln in eine Map muss sauber funktionieren! %s", err) } @@ -265,7 +266,7 @@ func TestFetchDataReservation(t *testing.T) { func TestFetchDataReservationVarition2(t *testing.T) { payloadStr := `{ "products": { "A6053": 24 }, "context": { "country": "EU", "state": "" }}` - var payload Container + var payload models.Container err := json.Unmarshal([]byte(payloadStr), &payload) if err != nil { t.Errorf("Dieser Fehler beim Konvertieren der JSON Payload sollte nicht passieren! \n%s", err) @@ -286,7 +287,7 @@ func TestFetchDataReservationVarition2(t *testing.T) { func TestFetchDataReservationVarition3(t *testing.T) { payloadStr := `{ "products": { "A6053": 5 }, "context": { "country": "AT", "state": "" }}` - var payload Container + var payload models.Container err := json.Unmarshal([]byte(payloadStr), &payload) if err != nil { t.Errorf("Dieser Fehler beim Konvertieren der JSON Payload sollte nicht passieren! \n%s", err) @@ -307,7 +308,7 @@ func TestFetchDataReservationVarition3(t *testing.T) { func TestOutgoingJsonString(t *testing.T) { payloadStr := `{ "products": { "A6053": 5 }, "context": { "country": "AT", "state": "" }}` - var payload Container + var payload models.Container err := json.Unmarshal([]byte(payloadStr), &payload) if err != nil { t.Errorf("Dieser Fehler beim Konvertieren der JSON Payload sollte nicht passieren! \n%s", err) @@ -342,7 +343,7 @@ func TestOutgoingReservationInDatabase(t *testing.T) { } payloadStr := `{ "products": { "A6053": 5 }, "context": { "country": "AT", "state": "" }}` - var payload Container + var payload models.Container err = json.Unmarshal([]byte(payloadStr), &payload) if err != nil { t.Errorf("Dieser Fehler beim Konvertieren der JSON Payload sollte nicht passieren! \n%s", err) @@ -350,7 +351,7 @@ func TestOutgoingReservationInDatabase(t *testing.T) { } groupId := uuid.New().String() ps := NewProductService(ctx, connectionString) - op, err := ps.FetchReservationData(&payload, UUID(groupId)) + op, err := ps.FetchReservationData(&payload, models.UUID(groupId)) if err != nil { t.Errorf("Das Datafetchen und umwandeln in eine Map muss sauber funktionieren! %s", err) } @@ -358,7 +359,7 @@ func TestOutgoingReservationInDatabase(t *testing.T) { if fmt.Sprint(op) != valTocompare { t.Errorf("Mist, sollte sein %s ist aber %s", valTocompare, fmt.Sprint(op)) } - uuids, err := ps.ReserviereBestellungen(op, UUID(groupId)) + uuids, err := ps.ReserviereBestellungen(op, models.UUID(groupId)) if err != nil { t.Errorf("Das Reservieren ist fehlgeschlagen! \n%s", err) } @@ -369,7 +370,7 @@ func TestOutgoingReservationInDatabase(t *testing.T) { } } - ps.dbr.ReleaseReservationGroup(UUID(groupId)) + ps.dbr.ReleaseReservationGroup(models.UUID(groupId)) for _, uuid := range *uuids { status, err := ps.dbr.GetReservationStateById(uuid) if err != nil || status != "RELEASED" { @@ -378,7 +379,7 @@ func TestOutgoingReservationInDatabase(t *testing.T) { } jsonResult, err := json.Marshal(op) - jsonGuid := fmt.Sprintf("\"id\":\"%s\"", UUID(groupId)) + jsonGuid := fmt.Sprintf("\"id\":\"%s\"", models.UUID(groupId)) jsonStrForApiResponse := fmt.Sprintf(`[{%s,"products":{"A6053":[{"warehouse":"AT","quantity":4,"delivery_time":1},{"warehouse":"DE","quantity":1,"delivery_time":2}]}}]`, jsonGuid) if string(jsonResult) != jsonStrForApiResponse { t.Errorf("Mist, sollte sein %s ist aber %s", string(jsonResult), jsonStrForApiResponse) diff --git a/internal/database/service.go b/internal/database/service.go index b11f064..63e3585 100644 --- a/internal/database/service.go +++ b/internal/database/service.go @@ -5,43 +5,24 @@ import ( "log" "time" + "gittea.marcokittel.de/elio/eliotools/datawriter/internal/models" "golang.org/x/net/context" ) type ProductService struct { dbr *DatabaseReader } -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"` - WhdId int `json:"-"` - DeliveryId int `json:"-"` -} - -type OutgoingProducts struct { - Products map[string][]Product `json:"products"` -} -type OutgoingReservationProducts struct { - Id UUID `json:"id"` - Products map[string][]Product `json:"products"` -} - -func NewOutgoingProducts() *OutgoingProducts { - op := OutgoingProducts{ - Products: make(map[string][]Product), +func NewOutgoingProducts() *models.OutgoingProducts { + op := models.OutgoingProducts{ + Products: make(map[string][]models.Product), } return &op } -func NewOutgoingReservationsProducts(groupId UUID) *OutgoingReservationProducts { - op := OutgoingReservationProducts{ +func NewOutgoingReservationsProducts(groupId models.UUID) *models.OutgoingReservationProducts { + op := models.OutgoingReservationProducts{ Id: groupId, - Products: make(map[string][]Product), + Products: make(map[string][]models.Product), } return &op } @@ -72,8 +53,8 @@ func (p *ProductService) Autorelease() { }() } -func (p *ProductService) ReserviereBestellungen(op []OutgoingReservationProducts, groupId UUID) (*[]UUID, error) { - var uuids []UUID +func (p *ProductService) ReserviereBestellungen(op []models.OutgoingReservationProducts, groupId models.UUID) (*[]models.UUID, error) { + var uuids []models.UUID for _, hsmp := range op { for _, prod := range hsmp.Products { for _, concreteProduct := range prod { @@ -92,20 +73,20 @@ func (p *ProductService) ReserviereBestellungen(op []OutgoingReservationProducts return &uuids, nil } -func (p *ProductService) ConfirmBestellung(groudId UUID) error { +func (p *ProductService) ConfirmBestellung(groudId models.UUID) error { return p.dbr.ConfirmReservationGroup(groudId) } -func (p *ProductService) AbortBestellung(groudId UUID) error { +func (p *ProductService) AbortBestellung(groudId models.UUID) error { return p.dbr.AbortReservationGroup(groudId) } -func (p *ProductService) ReleaseBestellung(groudId UUID) error { +func (p *ProductService) ReleaseBestellung(groudId models.UUID) error { return p.dbr.ReleaseReservationGroup(groudId) } -func (p *ProductService) FetchData(payload *Container) ([]OutgoingProducts, error) { - result := []OutgoingProducts{} +func (p *ProductService) FetchData(payload *models.Container) ([]models.OutgoingProducts, error) { + result := []models.OutgoingProducts{} for key, item := range payload.Products { //Checken ob die Felder leer sind / Validitätsprüfung einbauen products, err := p.dbr.GetProductByProductIdDeliveryCountryAndState(key, payload.Context.Country, payload.Context.State) @@ -122,11 +103,11 @@ func (p *ProductService) FetchData(payload *Container) ([]OutgoingProducts, erro continue } if db_products.Amount >= gebrauchteProduktAnzahl { - newProduct := Product{Delivery: db_products.DeliveryDays, Quantity: gebrauchteProduktAnzahl, Warehouse: db_products.Warehouse, WhdId: db_products.Id, DeliveryId: db_products.DeliveryId} + newProduct := models.Product{Delivery: db_products.DeliveryDays, Quantity: gebrauchteProduktAnzahl, Warehouse: db_products.Warehouse, WhdId: db_products.Id, DeliveryId: db_products.DeliveryId} gebrauchteProduktAnzahl = 0 op.Products[key] = append(op.Products[key], newProduct) } else if db_products.Amount < gebrauchteProduktAnzahl { - newProduct := Product{Delivery: db_products.DeliveryDays, Quantity: db_products.Amount, Warehouse: db_products.Warehouse, WhdId: db_products.Id, DeliveryId: db_products.DeliveryId} + newProduct := models.Product{Delivery: db_products.DeliveryDays, Quantity: db_products.Amount, Warehouse: db_products.Warehouse, WhdId: db_products.Id, DeliveryId: db_products.DeliveryId} gebrauchteProduktAnzahl -= db_products.Amount op.Products[key] = append(op.Products[key], newProduct) } @@ -136,8 +117,8 @@ func (p *ProductService) FetchData(payload *Container) ([]OutgoingProducts, erro return result, nil } -func (p *ProductService) FetchReservationData(payload *Container, groupId UUID) ([]OutgoingReservationProducts, error) { - result := []OutgoingReservationProducts{} +func (p *ProductService) FetchReservationData(payload *models.Container, groupId models.UUID) ([]models.OutgoingReservationProducts, error) { + result := []models.OutgoingReservationProducts{} for key, item := range payload.Products { //Checken ob die Felder leer sind / Validitätsprüfung einbauen products, err := p.dbr.GetProductByProductIdDeliveryCountryAndState(key, payload.Context.Country, payload.Context.State) @@ -154,11 +135,11 @@ func (p *ProductService) FetchReservationData(payload *Container, groupId UUID) continue } if db_products.Amount >= gebrauchteProduktAnzahl { - newProduct := Product{Delivery: db_products.DeliveryDays, Quantity: gebrauchteProduktAnzahl, Warehouse: db_products.Warehouse, WhdId: db_products.Id, DeliveryId: db_products.DeliveryId} + newProduct := models.Product{Delivery: db_products.DeliveryDays, Quantity: gebrauchteProduktAnzahl, Warehouse: db_products.Warehouse, WhdId: db_products.Id, DeliveryId: db_products.DeliveryId} gebrauchteProduktAnzahl = 0 op.Products[key] = append(op.Products[key], newProduct) } else if db_products.Amount < gebrauchteProduktAnzahl { - newProduct := Product{Delivery: db_products.DeliveryDays, Quantity: db_products.Amount, Warehouse: db_products.Warehouse, WhdId: db_products.Id, DeliveryId: db_products.DeliveryId} + newProduct := models.Product{Delivery: db_products.DeliveryDays, Quantity: db_products.Amount, Warehouse: db_products.Warehouse, WhdId: db_products.Id, DeliveryId: db_products.DeliveryId} gebrauchteProduktAnzahl -= db_products.Amount op.Products[key] = append(op.Products[key], newProduct) } diff --git a/internal/models/models.go b/internal/models/models.go new file mode 100644 index 0000000..0cd9deb --- /dev/null +++ b/internal/models/models.go @@ -0,0 +1,37 @@ +package models + +type UUID string + +type ProductDelivery struct { + Id int //Warehouse Id + Warehouse string + Amount int + DeliveryDays int + DeliveryId int +} + +type Container struct { + Products map[string]int `json:"products"` + Context Context `json:"context"` +} + +type Context struct { + Country string `json:"country"` + State string `json:"state"` +} + +type Product struct { + Warehouse string `json:"warehouse"` + Quantity int `json:"quantity"` + Delivery int `json:"delivery_time"` + WhdId int `json:"-"` + DeliveryId int `json:"-"` +} + +type OutgoingProducts struct { + Products map[string][]Product `json:"products"` +} +type OutgoingReservationProducts struct { + Id UUID `json:"id"` + Products map[string][]Product `json:"products"` +} diff --git a/starteDatenbankBackup b/starteDatenbankBackup new file mode 100755 index 0000000..a637207 --- /dev/null +++ b/starteDatenbankBackup @@ -0,0 +1,2 @@ +#!/bin/sh +docker exec -it 4b8b8ae54287 /usr/bin/mysqldump -u root -p'eliogeheim' --all-databases --single-transaction | gzip > ~/backup-mariadb-$(date +\%Y\%m\%d).sql.gz