72 lines
2.0 KiB
Go
72 lines
2.0 KiB
Go
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
|
|
}
|