Setup Files hinzugefügt.
This commit is contained in:
parent
c5d27c68c8
commit
0eed845fc8
@ -6,6 +6,7 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/database"
|
||||
)
|
||||
@ -37,13 +38,23 @@ type Context struct {
|
||||
State string `json:"state"`
|
||||
}
|
||||
|
||||
var (
|
||||
connectionString = ""
|
||||
port = ":8080"
|
||||
)
|
||||
|
||||
const (
|
||||
connectionString = "root:eliogeheim@tcp(127.0.0.1:3306)/elio_test?parseTime=true"
|
||||
curlhelp = `curl -X POST localhost:8080/api/products -d '{ "products": { "A6053": 2, "B3009": 1200 }, "context": { "country": "EU", "state": "" } }'`
|
||||
)
|
||||
|
||||
func main() {
|
||||
connectionString := os.Getenv("CONNECTIONSTRING")
|
||||
nps := database.NewProductService(connectionString)
|
||||
if len(connectionString) == 0 {
|
||||
fmt.Println("Connectionstring fehlt!. Bsp.: <user>:<passwort>@tcp(127.0.0.1:3306)/elio?parseTime=true")
|
||||
return
|
||||
}
|
||||
//Env Variablen nutzen
|
||||
dbr := database.NewDatabaseReader(connectionString)
|
||||
http.HandleFunc("/api/products", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
return
|
||||
@ -54,43 +65,17 @@ func main() {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
var foo Container
|
||||
err = json.Unmarshal(data, &foo)
|
||||
var payload database.Container
|
||||
err = json.Unmarshal(data, &payload)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
log.Printf("Could not parse Json: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(foo)
|
||||
result := []OutgoingProducts{}
|
||||
for key, item := range foo.Products {
|
||||
//Checken ob die Felder leer sind / Validitätsprüfung einbauen
|
||||
products, err := dbr.GetProductByProductIdDeliveryCountryAndState(key, foo.Context.Country, foo.Context.State)
|
||||
result, err := nps.FetchData(&payload)
|
||||
if err != nil {
|
||||
//Todo Fehlerhandling
|
||||
log.Println(err)
|
||||
}
|
||||
if len(products) == 0 {
|
||||
continue
|
||||
}
|
||||
gebrauchteProduktAnzahl := item
|
||||
op := NewOutgoingProducts()
|
||||
for _, db_products := range products {
|
||||
if gebrauchteProduktAnzahl <= 0 {
|
||||
continue
|
||||
}
|
||||
if db_products.Amount >= gebrauchteProduktAnzahl {
|
||||
newProduct := Product{Delivery: db_products.DeliveryDays, Quantity: gebrauchteProduktAnzahl, Warehouse: db_products.Warehouse}
|
||||
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}
|
||||
gebrauchteProduktAnzahl -= db_products.Amount
|
||||
op.Products[key] = append(op.Products[key], newProduct)
|
||||
}
|
||||
}
|
||||
result = append(result, *op)
|
||||
fmt.Println(op)
|
||||
}
|
||||
jsonResult, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
@ -99,7 +84,7 @@ func main() {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintln(w, string(jsonResult))
|
||||
})
|
||||
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
mariadb:
|
||||
image: mariadb:10.6
|
||||
@ -11,11 +9,13 @@ services:
|
||||
MYSQL_DATABASE: elio
|
||||
volumes:
|
||||
- mariadb_data:/var/lib/mysql
|
||||
- ./initdb:/docker-entrypoint-initdb.d
|
||||
|
||||
healthcheck:
|
||||
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-p eliogeheim"]
|
||||
test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1","--protocol=socket", "-u", "root", "-peliogeheim"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 3
|
||||
|
||||
volumes:
|
||||
mariadb_data: # Named Volume für Datenpersistenz
|
||||
mariadb_data:
|
||||
|
||||
51
initdb/init.sql
Normal file
51
initdb/init.sql
Normal file
@ -0,0 +1,51 @@
|
||||
-- init.sql
|
||||
CREATE DATABASE IF NOT EXISTS elio_test;
|
||||
CREATE DATABASE IF NOT EXISTS elio;
|
||||
|
||||
|
||||
-- -- Haupttabellen erzeugen
|
||||
-- CREATE TABLE IF NOT EXISTS elio.warehouseproducts (
|
||||
-- id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
-- warehouse char(2) NOT NULL,
|
||||
-- productid VARCHAR(20) NOT NULL,
|
||||
-- amount INT DEFAULT 0,
|
||||
-- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
-- CONSTRAINT location_product_must_be_one UNIQUE (warehouse, productid)
|
||||
-- ) ENGINE=InnoDB;
|
||||
|
||||
-- CREATE TABLE IF NOT EXISTS elio.deliverytimes (
|
||||
-- id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
-- fromcountry varchar(4) NOT NULL,
|
||||
-- tocountry varchar(4) NOT NULL,
|
||||
-- state varchar(4) NULL,
|
||||
-- delivery INT DEFAULT 0,
|
||||
-- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
-- CONSTRAINT delivery_from_to_country_must_be_one UNIQUE (fromcountry, tocountry)
|
||||
-- ) ENGINE=InnoDB;
|
||||
|
||||
-- -- Testtabellen erzeugen
|
||||
-- CREATE TABLE IF NOT EXISTS elio_test.warehouseproducts (
|
||||
-- id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
-- warehouse char(2) NOT NULL,
|
||||
-- productid VARCHAR(20) NOT NULL,
|
||||
-- amount INT DEFAULT 0,
|
||||
-- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
-- CONSTRAINT location_product_must_be_one UNIQUE (warehouse, productid)
|
||||
-- ) ENGINE=InnoDB;
|
||||
|
||||
-- CREATE TABLE IF NOT EXISTS elio_test.deliverytimes (
|
||||
-- id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
-- fromcountry varchar(4) NOT NULL,
|
||||
-- tocountry varchar(4) NOT NULL,
|
||||
-- state varchar(4) NULL,
|
||||
-- delivery INT DEFAULT 0,
|
||||
-- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
-- CONSTRAINT delivery_from_to_country_must_be_one UNIQUE (fromcountry, tocountry)
|
||||
-- ) ENGINE=InnoDB;
|
||||
|
||||
-- Userkram
|
||||
CREATE USER IF NOT EXISTS 'elio'@'%' IDENTIFIED BY 'eliogeheim';
|
||||
CREATE USER IF NOT EXISTS 'elio_test'@'%' IDENTIFIED BY 'eliogeheim';
|
||||
GRANT ALL PRIVILEGES ON elio.* TO 'elio'@'%';
|
||||
GRANT ALL PRIVILEGES ON elio_test.* TO 'elio_test'@'%';
|
||||
FLUSH PRIVILEGES;
|
||||
51
initdb/init_table.sql
Normal file
51
initdb/init_table.sql
Normal file
@ -0,0 +1,51 @@
|
||||
-- init.sql
|
||||
CREATE DATABASE IF NOT EXISTS elio_test;
|
||||
CREATE DATABASE IF NOT EXISTS elio;
|
||||
|
||||
|
||||
-- Haupttabellen erzeugen
|
||||
CREATE TABLE IF NOT EXISTS elio.blablabla (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
warehouse char(2) NOT NULL,
|
||||
productid VARCHAR(20) NOT NULL,
|
||||
amount INT DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
CONSTRAINT location_product_must_be_one UNIQUE (warehouse, productid)
|
||||
) ENGINE=InnoDB;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS elio.deliverytimes (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
fromcountry varchar(4) NOT NULL,
|
||||
tocountry varchar(4) NOT NULL,
|
||||
state varchar(4) NULL,
|
||||
delivery INT DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
CONSTRAINT delivery_from_to_country_must_be_one UNIQUE (fromcountry, tocountry)
|
||||
) ENGINE=InnoDB;
|
||||
|
||||
-- Testtabellen erzeugen
|
||||
CREATE TABLE IF NOT EXISTS elio_test.warehouseproducts (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
warehouse char(2) NOT NULL,
|
||||
productid VARCHAR(20) NOT NULL,
|
||||
amount INT DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
CONSTRAINT location_product_must_be_one UNIQUE (warehouse, productid)
|
||||
) ENGINE=InnoDB;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS elio_test.deliverytimes (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
fromcountry varchar(4) NOT NULL,
|
||||
tocountry varchar(4) NOT NULL,
|
||||
state varchar(4) NULL,
|
||||
delivery INT DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
CONSTRAINT delivery_from_to_country_must_be_one UNIQUE (fromcountry, tocountry)
|
||||
) ENGINE=InnoDB;
|
||||
|
||||
-- Userkram
|
||||
CREATE USER IF NOT EXISTS 'elio'@'%' IDENTIFIED BY 'eliogeheim';
|
||||
CREATE USER IF NOT EXISTS 'elio_test'@'%' IDENTIFIED BY 'eliogeheim';
|
||||
GRANT ALL PRIVILEGES ON elio.* TO 'elio'@'%';
|
||||
GRANT ALL PRIVILEGES ON elio_test.* TO 'elio_test'@'%';
|
||||
FLUSH PRIVILEGES;
|
||||
@ -34,7 +34,10 @@ func (d *DatabaseWriter) connectDB(connectionString string) (*sql.DB, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = db.Ping()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Verbindungspool konfigurieren
|
||||
db.SetMaxOpenConns(40)
|
||||
db.SetMaxIdleConns(25)
|
||||
|
||||
71
internal/database/service.go
Normal file
71
internal/database/service.go
Normal file
@ -0,0 +1,71 @@
|
||||
package database
|
||||
|
||||
import "log"
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
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 NewProductService(connectionString string) *ProductService {
|
||||
dbr := NewDatabaseReader(connectionString)
|
||||
p := ProductService{dbr: dbr}
|
||||
return &p
|
||||
}
|
||||
|
||||
func (p *ProductService) FetchData(payload *Container) ([]OutgoingProducts, error) {
|
||||
result := []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)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
if len(products) == 0 {
|
||||
continue
|
||||
}
|
||||
gebrauchteProduktAnzahl := item
|
||||
op := NewOutgoingProducts()
|
||||
for _, db_products := range products {
|
||||
if gebrauchteProduktAnzahl <= 0 {
|
||||
continue
|
||||
}
|
||||
if db_products.Amount >= gebrauchteProduktAnzahl {
|
||||
newProduct := Product{Delivery: db_products.DeliveryDays, Quantity: gebrauchteProduktAnzahl, Warehouse: db_products.Warehouse}
|
||||
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}
|
||||
gebrauchteProduktAnzahl -= db_products.Amount
|
||||
op.Products[key] = append(op.Products[key], newProduct)
|
||||
}
|
||||
}
|
||||
result = append(result, *op)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
2
starteCsvService
Executable file
2
starteCsvService
Executable file
@ -0,0 +1,2 @@
|
||||
#!/bin/sh
|
||||
CONNECTIONSTRING='root:eliogeheim@tcp(127.0.0.1:3306)/elio?parseTime=true' go run cmd/shell/main.go
|
||||
2
starteDatenbank
Executable file
2
starteDatenbank
Executable file
@ -0,0 +1,2 @@
|
||||
#!/bin/sh
|
||||
exec docker-compose up -d
|
||||
2
starteWebservice
Executable file
2
starteWebservice
Executable file
@ -0,0 +1,2 @@
|
||||
#!/bin/sh
|
||||
CONNECTIONSTRING='elio:eliogeheim@tcp(127.0.0.1:3306)/elio?parseTime=true' go run cmd/websrv/main.go
|
||||
@ -1,6 +1,6 @@
|
||||
country;state;delivery_time
|
||||
CH;;2
|
||||
DE;;2
|
||||
CH;;4
|
||||
DE;;5
|
||||
AT;;2
|
||||
EU;;2
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
country;state;delivery_time
|
||||
CH;;9
|
||||
DE;BW;10
|
||||
AT;;11
|
||||
CH;;3
|
||||
DE;;2
|
||||
AT;;1
|
||||
EU;;4
|
||||
|
||||
|
6
testdaten/2023-11-09T16:10:10+00:00-CH-delivery.csv
Normal file
6
testdaten/2023-11-09T16:10:10+00:00-CH-delivery.csv
Normal file
@ -0,0 +1,6 @@
|
||||
country;state;delivery_time
|
||||
CH;;2
|
||||
DE;BW;3
|
||||
AT;;4
|
||||
EU;;2
|
||||
|
||||
|
6
testdaten/2023-11-09T16:10:10+00:00-EU-delivery.csv
Normal file
6
testdaten/2023-11-09T16:10:10+00:00-EU-delivery.csv
Normal file
@ -0,0 +1,6 @@
|
||||
country;state;delivery_time
|
||||
CH;;4
|
||||
DE;;5
|
||||
AT;;2
|
||||
EU;;2
|
||||
|
||||
|
1002
testdaten/2023-11-09T20:20:17+00:00-AT-stock.csv
Normal file
1002
testdaten/2023-11-09T20:20:17+00:00-AT-stock.csv
Normal file
File diff suppressed because it is too large
Load Diff
1002
testdaten/2023-11-09T20:20:17+00:00-DE-stock.csv
Normal file
1002
testdaten/2023-11-09T20:20:17+00:00-DE-stock.csv
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user