bom layer: add service and controller for BOM and variant nodes
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user