From b2ce536723ea87adc50dc3701f81150dccc82b33 Mon Sep 17 00:00:00 2001 From: Thomas Sindt Date: Wed, 26 Aug 2026 23:15:09 +0200 Subject: [PATCH] bom layer: add service and controller for BOM and variant nodes --- buildtime/database/.$BOMStructure.bkp | 184 ++++++++++++++ buildtime/database/BOMStructure | 247 +++++++++++++++++++ buildtime/server/controller/bomController.go | 8 +- buildtime/server/services/bomService.go | 133 ++++++++++ 4 files changed, 568 insertions(+), 4 deletions(-) create mode 100644 buildtime/database/.$BOMStructure.bkp create mode 100644 buildtime/database/BOMStructure create mode 100644 buildtime/server/services/bomService.go diff --git a/buildtime/database/.$BOMStructure.bkp b/buildtime/database/.$BOMStructure.bkp new file mode 100644 index 000000000..a76518626 --- /dev/null +++ b/buildtime/database/.$BOMStructure.bkp @@ -0,0 +1,184 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/buildtime/database/BOMStructure b/buildtime/database/BOMStructure new file mode 100644 index 000000000..66d236945 --- /dev/null +++ b/buildtime/database/BOMStructure @@ -0,0 +1,247 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/buildtime/server/controller/bomController.go b/buildtime/server/controller/bomController.go index 9389b552d..678ba90e9 100644 --- a/buildtime/server/controller/bomController.go +++ b/buildtime/server/controller/bomController.go @@ -11,10 +11,10 @@ import ( ) type BOMController struct { - bomService *services.BOMService + bomService *services.BomService } -func NewBOMController(b *services.BOMService) *BOMController { +func NewBOMController(b *services.BomService) *BOMController { return &BOMController{ bomService: b, } @@ -72,7 +72,7 @@ func (bc *BOMController) CreateVariantNode(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } - err = bc.bomService.CreateVariantNode(&variantNode) + _, err = bc.bomService.CreateVariantNode(&variantNode) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return @@ -117,7 +117,7 @@ func (bc *BOMController) GetVariantNode(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } - variantNode, err := bc.bomService.GetVariantNodeById(variantNodeId) + variantNode, err := bc.bomService.GetVariantNode(variantNodeId) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return diff --git a/buildtime/server/services/bomService.go b/buildtime/server/services/bomService.go new file mode 100644 index 000000000..f21d667c8 --- /dev/null +++ b/buildtime/server/services/bomService.go @@ -0,0 +1,133 @@ +package services + +import ( + "fmt" + "simplycpq/models" + "simplycpq/repositories" +) + +type BomService struct { + bomRepository *repositories.BomRepository +} + +func NewBomService(bomRepo *repositories.BomRepository) *BomService { + return &BomService{ + bomRepository: bomRepo, + } +} + +func (bs *BomService) GetBOMByProductID(productID int) ([]models.BillOfMaterial, error) { + bomData, err := bs.bomRepository.GetBOMByProductID(productID) + if err != nil { + return nil, fmt.Errorf("failed to retrieve BOM data: %w", err) + } + return bomData, nil +} + +func (bs *BomService) CreateBillOfMaterial(bom *models.BillOfMaterial) error { + // Validate the BOM data + if bom.ProductID <= 0 { + return fmt.Errorf("invalid product ID") + } + + // Create the BOM in the repository + err := bs.bomRepository.CreateBillOfMaterial(bom) + if err != nil { + return fmt.Errorf("failed to create BOM: %w", err) + } + return nil +} + +func (bs *BomService) GetBOMProductsByBOMId(bomId int64) ([]models.Product, error) { + products, err := bs.bomRepository.GetBOMProductsByBOMId(bomId) + if err != nil { + return nil, fmt.Errorf("failed to retrieve BOM products: %w", err) + } + return products, nil +} + +func (bs *BomService) GetBOMVariantNodesByBOMId(bomId int64) ([]models.VariantNode, error) { + variantNodes, err := bs.bomRepository.GetBOMVariantNodesByBOMId(bomId) + if err != nil { + return nil, fmt.Errorf("failed to retrieve BOM variant nodes: %w", err) + } + return variantNodes, nil +} + +func (bs *BomService) CreateVariantNode(variantNode *models.VariantNode) (*models.VariantNode, error) { + + // Create the VariantNode in the repository + variantNode, err := bs.bomRepository.CreateVariantNode(variantNode) + if err != nil { + return nil, fmt.Errorf("failed to create VariantNode: %w", err) + } + return variantNode, nil +} + +func (bs *BomService) GetVariantNodeByID(id int64) (*models.VariantNode, error) { + variantNode, err := bs.bomRepository.GetVariantNodeByID(id) + if err != nil { + return nil, fmt.Errorf("failed to retrieve VariantNode: %w", err) + } + return variantNode, nil +} + +func (bs *BomService) UpdateVariantNode(variantNode *models.VariantNode) error { + // Update the VariantNode in the repository + err := bs.bomRepository.UpdateVariantNode(variantNode) + if err != nil { + return fmt.Errorf("failed to update VariantNode: %w", err) + } + return nil +} + +func (bs *BomService) DeleteVariantNode(id int64) error { + // Delete the VariantNode in the repository + err := bs.bomRepository.DeleteVariantNode(id) + if err != nil { + return fmt.Errorf("failed to delete VariantNode: %w", err) + } + return nil +} + +func (bs *BomService) GetVariantNode(parentID int64) ([]models.VariantNode, error) { + variantNodes, err := bs.bomRepository.GetVariantNode(parentID) + if err != nil { + return nil, fmt.Errorf("failed to retrieve VariantNodes: %w", err) + } + return variantNodes, nil +} + +func (bs *BomService) GetVariantNodeProductsByVariantNodeId(parentID int64) ([]models.Product, error) { + products, err := bs.bomRepository.GetVariantNodeProductsByVariantNodeId(parentID) + if err != nil { + return nil, fmt.Errorf("failed to retrieve VariantNode products: %w", err) + } + return products, nil +} + +func (bs *BomService) UpdateBillOfMaterial(bom *models.BillOfMaterial) error { + // Update the BOM in the repository + err := bs.bomRepository.UpdateBillOfMaterial(bom) + if err != nil { + return fmt.Errorf("failed to update BOM: %w", err) + } + return nil +} + +func (bs *BomService) DeleteBillOfMaterial(id int64) error { + // Delete the BOM in the repository + err := bs.bomRepository.DeleteBillOfMaterial(id) + if err != nil { + return fmt.Errorf("failed to delete BOM: %w", err) + } + return nil +} + +func (bs *BomService) GetBillOfMaterial(id int64) (*models.BillOfMaterial, error) { + bom, err := bs.bomRepository.GetBillOfMaterial(id) + if err != nil { + return nil, fmt.Errorf("failed to retrieve BOM: %w", err) + } + return bom, nil +}