Setup Files hinzugefügt.
This commit is contained in:
parent
c5d27c68c8
commit
0eed845fc8
@ -6,6 +6,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/database"
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/database"
|
||||||
)
|
)
|
||||||
@ -37,13 +38,23 @@ type Context struct {
|
|||||||
State string `json:"state"`
|
State string `json:"state"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
connectionString = ""
|
||||||
|
port = ":8080"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
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() {
|
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
|
//Env Variablen nutzen
|
||||||
dbr := database.NewDatabaseReader(connectionString)
|
|
||||||
http.HandleFunc("/api/products", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/api/products", func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != "POST" {
|
if r.Method != "POST" {
|
||||||
return
|
return
|
||||||
@ -54,42 +65,16 @@ func main() {
|
|||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var foo Container
|
var payload database.Container
|
||||||
err = json.Unmarshal(data, &foo)
|
err = json.Unmarshal(data, &payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Printf("Could not parse Json: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
result, err := nps.FetchData(&payload)
|
||||||
fmt.Println(foo)
|
if err != nil {
|
||||||
result := []OutgoingProducts{}
|
//Todo Fehlerhandling
|
||||||
for key, item := range foo.Products {
|
log.Println(err)
|
||||||
//Checken ob die Felder leer sind / Validitätsprüfung einbauen
|
|
||||||
products, err := dbr.GetProductByProductIdDeliveryCountryAndState(key, foo.Context.Country, foo.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)
|
|
||||||
fmt.Println(op)
|
|
||||||
}
|
}
|
||||||
jsonResult, err := json.Marshal(result)
|
jsonResult, err := json.Marshal(result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -99,7 +84,7 @@ func main() {
|
|||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
fmt.Fprintln(w, string(jsonResult))
|
fmt.Fprintln(w, string(jsonResult))
|
||||||
})
|
})
|
||||||
|
log.Printf("Easy Peasy: Die Party startet auf Port %s\n", port)
|
||||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
log.Printf("Probiers mal damit: %s\n", curlhelp)
|
||||||
|
log.Fatal(http.ListenAndServe(port, nil))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
version: '3.8'
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
mariadb:
|
mariadb:
|
||||||
image: mariadb:10.6
|
image: mariadb:10.6
|
||||||
@ -11,11 +9,13 @@ services:
|
|||||||
MYSQL_DATABASE: elio
|
MYSQL_DATABASE: elio
|
||||||
volumes:
|
volumes:
|
||||||
- mariadb_data:/var/lib/mysql
|
- mariadb_data:/var/lib/mysql
|
||||||
|
- ./initdb:/docker-entrypoint-initdb.d
|
||||||
|
|
||||||
healthcheck:
|
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
|
interval: 5s
|
||||||
timeout: 3s
|
timeout: 3s
|
||||||
retries: 3
|
retries: 3
|
||||||
|
|
||||||
volumes:
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
err = db.Ping()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
// Verbindungspool konfigurieren
|
// Verbindungspool konfigurieren
|
||||||
db.SetMaxOpenConns(40)
|
db.SetMaxOpenConns(40)
|
||||||
db.SetMaxIdleConns(25)
|
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
|
country;state;delivery_time
|
||||||
CH;;2
|
CH;;4
|
||||||
DE;;2
|
DE;;5
|
||||||
AT;;2
|
AT;;2
|
||||||
EU;;2
|
EU;;2
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
country;state;delivery_time
|
country;state;delivery_time
|
||||||
CH;;9
|
CH;;3
|
||||||
DE;BW;10
|
DE;;2
|
||||||
AT;;11
|
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