Erweiterung der Produktpflege

This commit is contained in:
2026-08-27 15:31:50 +02:00
parent b2ce536723
commit cdf7a538c1
240 changed files with 8264 additions and 6420 deletions
@@ -0,0 +1,184 @@
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
}
@@ -2,8 +2,8 @@ package repositories
import (
"database/sql"
"simplycpq/models"
"log"
"simplycpq/models"
)
type ProductRepository struct {
@@ -84,7 +84,7 @@ func (pr *ProductRepository) GetProductsByGroupId(groupId int) ([]*models.Produc
for rows.Next() {
product := &models.Product{}
log.Println("Scanning row into product struct") // Debugging line
log.Println("Row data:", rows) // Debugging line
log.Println("Row data:", rows) // Debugging line
err := rows.Scan(&product.ID, &product.Label, &product.ExternalID, &product.Description, &product.ProductGroupID)
if err != nil {
return nil, err
@@ -103,9 +103,11 @@ func (pr *ProductRepository) AddPropertyToProduct(productId int, propertyId int)
}
func (pr *ProductRepository) DeletePropertyFromProduct(productId int, propertyId int) error {
// Implement the logic to delete a property from a product in the database
query := "DELETE FROM product_properties WHERE productId = $1 AND propertyId = $2"
_, err := pr.dbHandler.Exec(query, productId, propertyId)
// remove the property owned by this product, including its values
if _, err := pr.dbHandler.Exec("DELETE FROM values WHERE propertyId = $1", propertyId); err != nil {
return err
}
_, err := pr.dbHandler.Exec("DELETE FROM properties WHERE productId = $1 AND id = $2", productId, propertyId)
return err
}
@@ -291,6 +291,13 @@ func (vr *ValueRepository) DeleteValueFromPropertyId(valueId int, propertyId int
return nil
}
func (vr *ValueRepository) UpdateValue(value *models.Value) error {
// Implement the logic to update the label/description of an existing value
query := `UPDATE values SET label=$1, description=$2 WHERE id=$3 AND propertyid=$4`
_, err := vr.db.Exec(query, value.Label, value.Description, value.ID, value.PropertyID)
return err
}
func (vr *ValueRepository) GetValue(id int) (*models.Value, error) {
// Implement the logic to retrieve the value from the database for the given id
query := `SELECT id, label, description FROM values WHERE id=$1`