Files
simply_cpq/buildtime/server/repositories/bomRepository.go
T
2026-08-27 15:31:50 +02:00

185 lines
5.5 KiB
Go

package repositories
import (
"database/sql"
"fmt"
"simplycpq/models"
)
type BomRepository struct {
db *sql.DB
productRepository *ProductRepository
}
func NewBomRepository(db *sql.DB, pr *ProductRepository) *BomRepository {
return &BomRepository{db: db, productRepository: pr}
}
func (r *BomRepository) GetBOMByProductID(productID int) (models.BillOfMaterial, error) {
var bom models.BillOfMaterial
query := "SELECT id, label, description, parentBOMId FROM boms WHERE productId = $1"
err := r.db.QueryRow(query, productID).Scan(&bom.ID, &bom.Label, &bom.Description, &bom.ParentBOMID)
if err != nil {
if err == sql.ErrNoRows {
return bom, fmt.Errorf("bom with product id %d not found", productID)
}
return bom, err
}
return bom, nil
}
func (r *BomRepository) GetBOMContentByBOMId(bomID int) ([]models.BOMContent, error) {
var contents []models.BOMContent
query := "SELECT id, productId, min, max FROM bom_contents WHERE bomId = $1"
rows, err := r.db.Query(query, bomID)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var content models.BOMContent
err := rows.Scan(&content.ID, &content.ProductID, &content.Min, &content.Max)
if err != nil {
return nil, err
}
contents = append(contents, content)
}
if err = rows.Err(); err != nil {
return nil, err
}
return contents, nil
}
func (r *BomRepository) GetBOMVariantNodesByBOMId(bomID int) ([]models.VariantNode, error) {
var variantNodes []models.VariantNode
query := "SELECT id, label, description FROM variant_nodes WHERE bomId = $1"
rows, err := r.db.Query(query, bomID)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var variantNode models.VariantNode
err := rows.Scan(&variantNode.ID, &variantNode.Label, &variantNode.Description)
if err != nil {
return nil, err
}
variantNodes = append(variantNodes, variantNode)
}
if err = rows.Err(); err != nil {
return nil, err
}
return variantNodes, nil
}
func (r *BomRepository) CreateBillOfMaterial(bom *models.BillOfMaterial) (models.BillOfMaterial, error) {
query := "INSERT INTO boms (productId, label, description, parentBOMId) VALUES ($1, $2, $3, $4) RETURNING id"
err := r.db.QueryRow(query, bom.ProductID, bom.Label, bom.Description, bom.ParentBOMID).Scan(&bom.ID)
if err != nil {
return models.BillOfMaterial{}, err
}
return *bom, nil
}
func (r *BomRepository) CreateVariantNode(node *models.VariantNode) (models.VariantNode, error) {
query := "INSERT INTO variantNode (label, description, bomId) VALUES ($1, $2, $3) RETURNING id"
err := r.db.QueryRow(query, node.Label, node.Description, node.BOMID).Scan(&node.ID)
if err != nil {
return models.VariantNode{}, err
}
return *node, nil
}
func (r *BomRepository) GetVariantNodeByID(id int) (models.VariantNode, error) {
var node models.VariantNode
query := "SELECT id, label, description, bomId FROM variantNodes WHERE id = $1"
err := r.db.QueryRow(query, id).Scan(&node.ID, &node.Label, &node.Description, &node.BOMID)
if err != nil {
if err == sql.ErrNoRows {
return models.VariantNode{}, fmt.Errorf("variant node with id %d not found", id)
}
return models.VariantNode{}, err
}
return node, nil
}
func (r *BomRepository) UpdateVariantNode(node *models.VariantNode) (models.VariantNode, error) {
query := "UPDATE variantNode SET label = $1, description = $2 WHERE id = $3"
_, err := r.db.Exec(query, node.Label, node.Description, node.ID)
if err != nil {
return models.VariantNode{}, err
}
return *node, nil
}
func (r *BomRepository) DeleteVariantNode(id int) error {
query := "DELETE FROM variantNode WHERE id = $1"
_, err := r.db.Exec(query, id)
return err
}
func (r *BomRepository) GetVariantNode(id int) (models.VariantNode, error) {
return r.GetVariantNodeByID(id)
}
func (r *BomRepository) GetVariantNodeProductsByVariantNodeId(variantNodeID int) ([]models.Product, error) {
var products []models.Product
query := "SELECT productId from variantNodeContent WHERE variantNodeId = $1"
rows, err := r.db.Query(query, variantNodeID)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var productID int
if err := rows.Scan(&productID); err != nil {
return nil, err
}
product, err := r.productRepository.GetProductById(productID)
if err != nil {
return nil, err
}
if product == nil {
continue
}
products = append(products, *product)
}
if err = rows.Err(); err != nil {
return nil, err
}
return products, nil
}
func (r *BomRepository) UpdateBillOfMaterial(bom *models.BillOfMaterial) (models.BillOfMaterial, error) {
query := "UPDATE billOfMaterial SET productId = $1, label = $2, description = $3, parentBOMId = $4 WHERE id = $5"
_, err := r.db.Exec(query, bom.ProductID, bom.Label, bom.Description, bom.ParentBOMID, bom.ID)
if err != nil {
return models.BillOfMaterial{}, err
}
return *bom, nil
}
func (r *BomRepository) DeleteBillOfMaterial(id int) error {
query := "DELETE FROM billOfMaterial WHERE id = $1"
_, err := r.db.Exec(query, id)
return err
}
func (r *BomRepository) GetBillOfMaterial(id int) (models.BillOfMaterial, error) {
var bom models.BillOfMaterial
query := "SELECT id, productId, label, description, parentBOMId FROM billOfMaterial WHERE id = $1"
err := r.db.QueryRow(query, id).Scan(&bom.ID, &bom.ProductID, &bom.Label, &bom.Description, &bom.ParentBOMID)
if err != nil {
if err == sql.ErrNoRows {
return models.BillOfMaterial{}, fmt.Errorf("bill of material with id %d not found", id)
}
return models.BillOfMaterial{}, err
}
return bom, nil
}