Aufräumen: DatenbankBackupSkript hinzugefügt
This commit is contained in:
parent
3ac88bb796
commit
586159a56d
|
|
@ -35,7 +35,7 @@ func main() {
|
||||||
|
|
||||||
defer cancel()
|
defer cancel()
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
db := database.NewDatabaseWriter(connectionstr)
|
db := database.NewDatabaseWriter(ctx, connectionstr)
|
||||||
ds := dataservice.NewDataService(ctx)
|
ds := dataservice.NewDataService(ctx)
|
||||||
ds.AddListener(db)
|
ds.AddListener(db)
|
||||||
wg.Add(ds.ListenerCount())
|
wg.Add(ds.ListenerCount())
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import (
|
||||||
|
|
||||||
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/api"
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/api"
|
||||||
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/database"
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/database"
|
||||||
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -21,7 +22,7 @@ var (
|
||||||
|
|
||||||
type (
|
type (
|
||||||
//RegistrierungsID -> ProductID -> Product
|
//RegistrierungsID -> ProductID -> Product
|
||||||
ProductStore map[string]map[string]database.Product
|
ProductStore map[string]map[string]models.Product
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/database"
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/database"
|
||||||
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetAbortApiHandleFunc(nps *database.ProductService) http.HandlerFunc {
|
func GetAbortApiHandleFunc(nps *database.ProductService) http.HandlerFunc {
|
||||||
|
|
@ -29,7 +30,7 @@ func GetAbortApiHandleFunc(nps *database.ProductService) http.HandlerFunc {
|
||||||
//Todo Fehlerhandling
|
//Todo Fehlerhandling
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
err = nps.AbortBestellung(database.UUID(payload.Id))
|
err = nps.AbortBestellung(models.UUID(payload.Id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//Todo Fehlerhandling
|
//Todo Fehlerhandling
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/database"
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/database"
|
||||||
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetConfirmReservationApiHandleFunc(nps *database.ProductService) http.HandlerFunc {
|
func GetConfirmReservationApiHandleFunc(nps *database.ProductService) http.HandlerFunc {
|
||||||
|
|
@ -29,7 +30,7 @@ func GetConfirmReservationApiHandleFunc(nps *database.ProductService) http.Handl
|
||||||
//Todo Fehlerhandling
|
//Todo Fehlerhandling
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
err = nps.ConfirmBestellung(database.UUID(payload.Id))
|
err = nps.ConfirmBestellung(models.UUID(payload.Id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//Todo Fehlerhandling
|
//Todo Fehlerhandling
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/database"
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/database"
|
||||||
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetProductApiHandleFunc(nps *database.ProductService) http.HandlerFunc {
|
func GetProductApiHandleFunc(nps *database.ProductService) http.HandlerFunc {
|
||||||
|
|
@ -21,7 +22,7 @@ func GetProductApiHandleFunc(nps *database.ProductService) http.HandlerFunc {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var payload database.Container
|
var payload models.Container
|
||||||
err = json.Unmarshal(data, &payload)
|
err = json.Unmarshal(data, &payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Could not parse Json: %s", err)
|
log.Printf("Could not parse Json: %s", err)
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/database"
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/database"
|
||||||
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetReleaseReservationApiHandleFunc(nps *database.ProductService) http.HandlerFunc {
|
func GetReleaseReservationApiHandleFunc(nps *database.ProductService) http.HandlerFunc {
|
||||||
|
|
@ -29,7 +30,7 @@ func GetReleaseReservationApiHandleFunc(nps *database.ProductService) http.Handl
|
||||||
//Todo Fehlerhandling
|
//Todo Fehlerhandling
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
err = nps.ReleaseBestellung(database.UUID(payload.Id))
|
err = nps.ReleaseBestellung(models.UUID(payload.Id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//Todo Fehlerhandling
|
//Todo Fehlerhandling
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import (
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/database"
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/database"
|
||||||
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetProductReservationApiHandleFunc(nps *database.ProductService) http.HandlerFunc {
|
func GetProductReservationApiHandleFunc(nps *database.ProductService) http.HandlerFunc {
|
||||||
|
|
@ -22,14 +23,14 @@ func GetProductReservationApiHandleFunc(nps *database.ProductService) http.Handl
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var payload database.Container
|
var payload models.Container
|
||||||
err = json.Unmarshal(data, &payload)
|
err = json.Unmarshal(data, &payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Could not parse Json: %s", err)
|
log.Printf("Could not parse Json: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
groupId := uuid.New().String()
|
groupId := uuid.New().String()
|
||||||
result, err := nps.FetchReservationData(&payload, database.UUID(groupId))
|
result, err := nps.FetchReservationData(&payload, models.UUID(groupId))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//Todo Fehlerhandling
|
//Todo Fehlerhandling
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
|
@ -39,7 +40,7 @@ func GetProductReservationApiHandleFunc(nps *database.ProductService) http.Handl
|
||||||
fmt.Fprintln(w)
|
fmt.Fprintln(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err = nps.ReserviereBestellungen(result, database.UUID(groupId))
|
_, err = nps.ReserviereBestellungen(result, models.UUID(groupId))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import (
|
||||||
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/models"
|
||||||
"gittea.marcokittel.de/elio/eliotools/eliofile"
|
"gittea.marcokittel.de/elio/eliotools/eliofile"
|
||||||
"gittea.marcokittel.de/elio/eliotools/logger"
|
"gittea.marcokittel.de/elio/eliotools/logger"
|
||||||
)
|
)
|
||||||
|
|
@ -25,8 +26,6 @@ type DatabaseReader struct {
|
||||||
DatabaseWriter
|
DatabaseWriter
|
||||||
}
|
}
|
||||||
|
|
||||||
type UUID string
|
|
||||||
|
|
||||||
// Das ist ein Golang Design Flaw... Context so zu übergeben, bitte nicht hauen
|
// Das ist ein Golang Design Flaw... Context so zu übergeben, bitte nicht hauen
|
||||||
func NewDatabaseReader(ctx context.Context, connectionString string) *DatabaseReader {
|
func NewDatabaseReader(ctx context.Context, connectionString string) *DatabaseReader {
|
||||||
return &DatabaseReader{*NewDatabaseWriter(ctx, connectionString)}
|
return &DatabaseReader{*NewDatabaseWriter(ctx, connectionString)}
|
||||||
|
|
@ -175,15 +174,7 @@ func (d *DatabaseWriter) UpdateOrInsertDelivery(fromcountry string, tocountry st
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProductDelivery struct {
|
func (d *DatabaseReader) GetProductByProductIdDeliveryCountryAndState(prod_id, delivery_country, delivery_country_state string) ([]models.ProductDelivery, error) {
|
||||||
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) {
|
|
||||||
ctx, cancel := context.WithTimeout(d.ctx, 100*time.Millisecond)
|
ctx, cancel := context.WithTimeout(d.ctx, 100*time.Millisecond)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
stmt := `
|
stmt := `
|
||||||
|
|
@ -209,9 +200,9 @@ func (d *DatabaseReader) GetProductByProductIdDeliveryCountryAndState(prod_id, d
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
var products []ProductDelivery
|
var products []models.ProductDelivery
|
||||||
for rows.Next() {
|
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 {
|
if err := rows.Scan(&pd.Id, &pd.Warehouse, &pd.Amount, &pd.DeliveryDays, &pd.DeliveryId); err != nil {
|
||||||
return products, err
|
return products, err
|
||||||
}
|
}
|
||||||
|
|
@ -220,7 +211,7 @@ func (d *DatabaseReader) GetProductByProductIdDeliveryCountryAndState(prod_id, d
|
||||||
return products, nil
|
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)
|
ctx, cancel := context.WithTimeout(d.ctx, 100*time.Millisecond)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
stmt := `
|
stmt := `
|
||||||
|
|
@ -240,7 +231,7 @@ func (d *DatabaseReader) GetReservationStateById(productId UUID) (string, error)
|
||||||
return status, nil
|
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)
|
ctx, cancel := context.WithTimeout(d.ctx, 100*time.Millisecond)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
stmt := `
|
stmt := `
|
||||||
|
|
@ -260,7 +251,7 @@ func (d *DatabaseReader) GetReservationItemsByGroupId(groupId UUID) (string, err
|
||||||
return status, nil
|
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)
|
ctx, cancel := context.WithTimeout(d.ctx, 100*time.Millisecond)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
_, err := d.db.ExecContext(ctx, "UPDATE reservations SET Status=? WHERE id=?", status, Id)
|
_, 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
|
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)
|
ctx, cancel := context.WithTimeout(d.ctx, 100*time.Millisecond)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
_, err := d.db.ExecContext(ctx, "UPDATE reservations SET Status=? WHERE reservationGroupId=?", status, groupId)
|
_, 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DatabaseWriter) AbortReservation(Id UUID) error {
|
func (d *DatabaseWriter) AbortReservation(Id models.UUID) error {
|
||||||
return d.updateReservationState(Id, "ABORTED")
|
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")
|
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")
|
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")
|
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")
|
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")
|
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()
|
newUUID := uuid.New().String()
|
||||||
_, err := d.db.Exec("INSERT INTO reservations (id, deliveryId, warehouseId, amount, status, reservationGroupId) VALUES (?,?,?,?,'RESERVED', ?)", newUUID, deliveryId, warehouseId, amount, groupId)
|
_, err := d.db.Exec("INSERT INTO reservations (id, deliveryId, warehouseId, amount, status, reservationGroupId) VALUES (?,?,?,?,'RESERVED', ?)", newUUID, deliveryId, warehouseId, amount, groupId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return UUID(newUUID), nil
|
return models.UUID(newUUID), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DatabaseWriter) HandleData(ctx context.Context, data eliofile.CountryCsvData) error {
|
func (db *DatabaseWriter) HandleData(ctx context.Context, data eliofile.CountryCsvData) error {
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Unit Tests sollten niemals auf der Produktivdatenbank stattfinden.
|
// Unit Tests sollten niemals auf der Produktivdatenbank stattfinden.
|
||||||
|
|
@ -133,7 +134,7 @@ func TestInsertStatement(t *testing.T) {
|
||||||
}
|
}
|
||||||
groupId := uuid.New().String()
|
groupId := uuid.New().String()
|
||||||
for _, item := range data {
|
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 {
|
if err != nil {
|
||||||
t.Errorf("Das Einfügen einer neuen Reservierung darf nicht fehlschlagen! %s", err)
|
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()
|
groupId := uuid.New().String()
|
||||||
for _, item := range data {
|
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 {
|
if err != nil {
|
||||||
t.Errorf("Das Einfügen einer neuen Reservierung darf nicht fehlschlagen! %s", err)
|
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()
|
groupId := uuid.New().String()
|
||||||
for _, item := range data {
|
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 {
|
if err != nil {
|
||||||
t.Errorf("Das Einfügen einer neuen Reservierung darf nicht fehlschlagen! %s", err)
|
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()
|
groupId := uuid.New().String()
|
||||||
for _, item := range data {
|
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 {
|
if err != nil {
|
||||||
t.Errorf("Das Einfügen einer neuen Reservierung darf nicht fehlschlagen! %s", err)
|
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) {
|
func TestFetchReservationData(t *testing.T) {
|
||||||
payloadStr := `{ "products": { "A6053": 30 }, "context": { "country": "EU", "state": "" }}`
|
payloadStr := `{ "products": { "A6053": 30 }, "context": { "country": "EU", "state": "" }}`
|
||||||
var payload Container
|
var payload models.Container
|
||||||
err := json.Unmarshal([]byte(payloadStr), &payload)
|
err := json.Unmarshal([]byte(payloadStr), &payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Dieser Fehler beim Konvertieren der JSON Payload sollte nicht passieren! \n%s", err)
|
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) {
|
func TestFetchDataReservation(t *testing.T) {
|
||||||
payloadStr := `{ "products": { "A6053": 30 }, "context": { "country": "EU", "state": "" }}`
|
payloadStr := `{ "products": { "A6053": 30 }, "context": { "country": "EU", "state": "" }}`
|
||||||
|
|
||||||
var payload Container
|
var payload models.Container
|
||||||
err := json.Unmarshal([]byte(payloadStr), &payload)
|
err := json.Unmarshal([]byte(payloadStr), &payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Dieser Fehler beim Konvertieren der JSON Payload sollte nicht passieren! \n%s", err)
|
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()
|
ctx := context.Background()
|
||||||
ps := NewProductService(ctx, connectionString)
|
ps := NewProductService(ctx, connectionString)
|
||||||
groupId := uuid.New().String()
|
groupId := uuid.New().String()
|
||||||
op, err := ps.FetchReservationData(&payload, UUID(groupId))
|
op, err := ps.FetchReservationData(&payload, models.UUID(groupId))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Das Datafetchen und umwandeln in eine Map muss sauber funktionieren! %s", err)
|
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) {
|
func TestFetchDataReservationVarition2(t *testing.T) {
|
||||||
payloadStr := `{ "products": { "A6053": 24 }, "context": { "country": "EU", "state": "" }}`
|
payloadStr := `{ "products": { "A6053": 24 }, "context": { "country": "EU", "state": "" }}`
|
||||||
|
|
||||||
var payload Container
|
var payload models.Container
|
||||||
err := json.Unmarshal([]byte(payloadStr), &payload)
|
err := json.Unmarshal([]byte(payloadStr), &payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Dieser Fehler beim Konvertieren der JSON Payload sollte nicht passieren! \n%s", err)
|
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) {
|
func TestFetchDataReservationVarition3(t *testing.T) {
|
||||||
payloadStr := `{ "products": { "A6053": 5 }, "context": { "country": "AT", "state": "" }}`
|
payloadStr := `{ "products": { "A6053": 5 }, "context": { "country": "AT", "state": "" }}`
|
||||||
|
|
||||||
var payload Container
|
var payload models.Container
|
||||||
err := json.Unmarshal([]byte(payloadStr), &payload)
|
err := json.Unmarshal([]byte(payloadStr), &payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Dieser Fehler beim Konvertieren der JSON Payload sollte nicht passieren! \n%s", err)
|
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) {
|
func TestOutgoingJsonString(t *testing.T) {
|
||||||
payloadStr := `{ "products": { "A6053": 5 }, "context": { "country": "AT", "state": "" }}`
|
payloadStr := `{ "products": { "A6053": 5 }, "context": { "country": "AT", "state": "" }}`
|
||||||
|
|
||||||
var payload Container
|
var payload models.Container
|
||||||
err := json.Unmarshal([]byte(payloadStr), &payload)
|
err := json.Unmarshal([]byte(payloadStr), &payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Dieser Fehler beim Konvertieren der JSON Payload sollte nicht passieren! \n%s", err)
|
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": "" }}`
|
payloadStr := `{ "products": { "A6053": 5 }, "context": { "country": "AT", "state": "" }}`
|
||||||
|
|
||||||
var payload Container
|
var payload models.Container
|
||||||
err = json.Unmarshal([]byte(payloadStr), &payload)
|
err = json.Unmarshal([]byte(payloadStr), &payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Dieser Fehler beim Konvertieren der JSON Payload sollte nicht passieren! \n%s", err)
|
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()
|
groupId := uuid.New().String()
|
||||||
ps := NewProductService(ctx, connectionString)
|
ps := NewProductService(ctx, connectionString)
|
||||||
op, err := ps.FetchReservationData(&payload, UUID(groupId))
|
op, err := ps.FetchReservationData(&payload, models.UUID(groupId))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Das Datafetchen und umwandeln in eine Map muss sauber funktionieren! %s", err)
|
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 {
|
if fmt.Sprint(op) != valTocompare {
|
||||||
t.Errorf("Mist, sollte sein %s ist aber %s", valTocompare, fmt.Sprint(op))
|
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 {
|
if err != nil {
|
||||||
t.Errorf("Das Reservieren ist fehlgeschlagen! \n%s", err)
|
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 {
|
for _, uuid := range *uuids {
|
||||||
status, err := ps.dbr.GetReservationStateById(uuid)
|
status, err := ps.dbr.GetReservationStateById(uuid)
|
||||||
if err != nil || status != "RELEASED" {
|
if err != nil || status != "RELEASED" {
|
||||||
|
|
@ -378,7 +379,7 @@ func TestOutgoingReservationInDatabase(t *testing.T) {
|
||||||
|
|
||||||
}
|
}
|
||||||
jsonResult, err := json.Marshal(op)
|
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)
|
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 {
|
if string(jsonResult) != jsonStrForApiResponse {
|
||||||
t.Errorf("Mist, sollte sein %s ist aber %s", string(jsonResult), jsonStrForApiResponse)
|
t.Errorf("Mist, sollte sein %s ist aber %s", string(jsonResult), jsonStrForApiResponse)
|
||||||
|
|
|
||||||
|
|
@ -5,43 +5,24 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/models"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ProductService struct {
|
type ProductService struct {
|
||||||
dbr *DatabaseReader
|
dbr *DatabaseReader
|
||||||
}
|
}
|
||||||
type Container struct {
|
|
||||||
Products map[string]int `json:"products"`
|
|
||||||
Context Context `json:"context"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Product struct {
|
func NewOutgoingProducts() *models.OutgoingProducts {
|
||||||
Warehouse string `json:"warehouse"`
|
op := models.OutgoingProducts{
|
||||||
Quantity int `json:"quantity"`
|
Products: make(map[string][]models.Product),
|
||||||
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),
|
|
||||||
}
|
}
|
||||||
return &op
|
return &op
|
||||||
}
|
}
|
||||||
func NewOutgoingReservationsProducts(groupId UUID) *OutgoingReservationProducts {
|
func NewOutgoingReservationsProducts(groupId models.UUID) *models.OutgoingReservationProducts {
|
||||||
op := OutgoingReservationProducts{
|
op := models.OutgoingReservationProducts{
|
||||||
Id: groupId,
|
Id: groupId,
|
||||||
Products: make(map[string][]Product),
|
Products: make(map[string][]models.Product),
|
||||||
}
|
}
|
||||||
return &op
|
return &op
|
||||||
}
|
}
|
||||||
|
|
@ -72,8 +53,8 @@ func (p *ProductService) Autorelease() {
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ProductService) ReserviereBestellungen(op []OutgoingReservationProducts, groupId UUID) (*[]UUID, error) {
|
func (p *ProductService) ReserviereBestellungen(op []models.OutgoingReservationProducts, groupId models.UUID) (*[]models.UUID, error) {
|
||||||
var uuids []UUID
|
var uuids []models.UUID
|
||||||
for _, hsmp := range op {
|
for _, hsmp := range op {
|
||||||
for _, prod := range hsmp.Products {
|
for _, prod := range hsmp.Products {
|
||||||
for _, concreteProduct := range prod {
|
for _, concreteProduct := range prod {
|
||||||
|
|
@ -92,20 +73,20 @@ func (p *ProductService) ReserviereBestellungen(op []OutgoingReservationProducts
|
||||||
return &uuids, nil
|
return &uuids, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ProductService) ConfirmBestellung(groudId UUID) error {
|
func (p *ProductService) ConfirmBestellung(groudId models.UUID) error {
|
||||||
return p.dbr.ConfirmReservationGroup(groudId)
|
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)
|
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)
|
return p.dbr.ReleaseReservationGroup(groudId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ProductService) FetchData(payload *Container) ([]OutgoingProducts, error) {
|
func (p *ProductService) FetchData(payload *models.Container) ([]models.OutgoingProducts, error) {
|
||||||
result := []OutgoingProducts{}
|
result := []models.OutgoingProducts{}
|
||||||
for key, item := range payload.Products {
|
for key, item := range payload.Products {
|
||||||
//Checken ob die Felder leer sind / Validitätsprüfung einbauen
|
//Checken ob die Felder leer sind / Validitätsprüfung einbauen
|
||||||
products, err := p.dbr.GetProductByProductIdDeliveryCountryAndState(key, payload.Context.Country, payload.Context.State)
|
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
|
continue
|
||||||
}
|
}
|
||||||
if db_products.Amount >= gebrauchteProduktAnzahl {
|
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
|
gebrauchteProduktAnzahl = 0
|
||||||
op.Products[key] = append(op.Products[key], newProduct)
|
op.Products[key] = append(op.Products[key], newProduct)
|
||||||
} else if db_products.Amount < gebrauchteProduktAnzahl {
|
} 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
|
gebrauchteProduktAnzahl -= db_products.Amount
|
||||||
op.Products[key] = append(op.Products[key], newProduct)
|
op.Products[key] = append(op.Products[key], newProduct)
|
||||||
}
|
}
|
||||||
|
|
@ -136,8 +117,8 @@ func (p *ProductService) FetchData(payload *Container) ([]OutgoingProducts, erro
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ProductService) FetchReservationData(payload *Container, groupId UUID) ([]OutgoingReservationProducts, error) {
|
func (p *ProductService) FetchReservationData(payload *models.Container, groupId models.UUID) ([]models.OutgoingReservationProducts, error) {
|
||||||
result := []OutgoingReservationProducts{}
|
result := []models.OutgoingReservationProducts{}
|
||||||
for key, item := range payload.Products {
|
for key, item := range payload.Products {
|
||||||
//Checken ob die Felder leer sind / Validitätsprüfung einbauen
|
//Checken ob die Felder leer sind / Validitätsprüfung einbauen
|
||||||
products, err := p.dbr.GetProductByProductIdDeliveryCountryAndState(key, payload.Context.Country, payload.Context.State)
|
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
|
continue
|
||||||
}
|
}
|
||||||
if db_products.Amount >= gebrauchteProduktAnzahl {
|
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
|
gebrauchteProduktAnzahl = 0
|
||||||
op.Products[key] = append(op.Products[key], newProduct)
|
op.Products[key] = append(op.Products[key], newProduct)
|
||||||
} else if db_products.Amount < gebrauchteProduktAnzahl {
|
} 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
|
gebrauchteProduktAnzahl -= db_products.Amount
|
||||||
op.Products[key] = append(op.Products[key], newProduct)
|
op.Products[key] = append(op.Products[key], newProduct)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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"`
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
Loading…
Reference in New Issue