134 lines
3.9 KiB
Go
134 lines
3.9 KiB
Go
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) (*models.BillOfMaterial, error) {
|
|
// Validate the BOM data
|
|
if bom.ProductID <= 0 {
|
|
return nil, fmt.Errorf("invalid product ID")
|
|
}
|
|
|
|
// Create the BOM in the repository
|
|
createdBOM, err := bs.bomRepository.CreateBillOfMaterial(bom)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create BOM: %w", err)
|
|
}
|
|
return &createdBOM, nil
|
|
}
|
|
|
|
func (bs *BomService) GetBOMContentByBOMId(bomId int) ([]models.BOMContent, error) {
|
|
contents, err := bs.bomRepository.GetBOMContentByBOMId(bomId)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to retrieve BOM contents: %w", err)
|
|
}
|
|
return contents, nil
|
|
}
|
|
|
|
func (bs *BomService) GetBOMVariantNodesByBOMId(bomId int) ([]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
|
|
variantNode2, err := bs.bomRepository.CreateVariantNode(variantNode)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create VariantNode: %w", err)
|
|
}
|
|
return &variantNode2, nil
|
|
}
|
|
|
|
func (bs *BomService) GetVariantNodeByID(id int) (*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) (*models.VariantNode, error) {
|
|
// Update the VariantNode in the repository
|
|
variantNode2, err := bs.bomRepository.UpdateVariantNode(variantNode)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to update VariantNode: %w", err)
|
|
}
|
|
return &variantNode2, nil
|
|
}
|
|
|
|
func (bs *BomService) DeleteVariantNode(id int) 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(id int) (*models.VariantNode, error) {
|
|
variantNode, err := bs.bomRepository.GetVariantNode(id)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to retrieve VariantNode: %w", err)
|
|
}
|
|
return &variantNode, nil
|
|
}
|
|
|
|
func (bs *BomService) GetVariantNodeProductsByVariantNodeId(id int) ([]models.Product, error) {
|
|
products, err := bs.bomRepository.GetVariantNodeProductsByVariantNodeId(id)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to retrieve VariantNode products: %w", err)
|
|
}
|
|
return products, nil
|
|
}
|
|
|
|
func (bs *BomService) UpdateBillOfMaterial(bom *models.BillOfMaterial) (*models.BillOfMaterial, error) {
|
|
// Update the BOM in the repository
|
|
bom2, err := bs.bomRepository.UpdateBillOfMaterial(bom)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to update BOM: %w", err)
|
|
}
|
|
return &bom2, nil
|
|
}
|
|
|
|
func (bs *BomService) DeleteBillOfMaterial(id int) 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 int) (*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
|
|
}
|