Produkte und Properties im Server

This commit is contained in:
2026-08-16 18:22:05 +02:00
parent 5b60189b07
commit 5060fe5a2e
10 changed files with 818 additions and 14 deletions
@@ -0,0 +1,152 @@
package repositories
import (
"database/sql"
"simplycpq/models"
)
type ProductGroupRepository struct {
db *sql.DB
valueRepository *ValueRepository
}
func NewProductGroupRepository(db *sql.DB, valueRepository *ValueRepository) *ProductGroupRepository {
return &ProductGroupRepository{
db: db,
valueRepository: valueRepository,
}
}
func (r *ProductGroupRepository) CreateProductGroup(productGroup *models.ProductGroup) (*models.ProductGroup, error) {
// Insert the product group into the database
query := "INSERT INTO product_groups (label, description, parentgroupid) VALUES($1,$2,$3) RETURNING id"
err := r.db.QueryRow(query, productGroup.Label, productGroup.Description, productGroup.ParentGroupID).Scan(&productGroup.ID)
if err != nil {
return nil, err
}
// get all properties from parent group and copy them to the new group
if productGroup.ParentGroupID != 0 {
properties, err := r.GetPropertiesFromProductGroupId(productGroup.ParentGroupID)
if err != nil {
return nil, err
}
for _, prop := range properties {
_, err := r.valueRepository.createPropertyFromProperty(productGroup.ID, 0, prop)
if err != nil {
return nil, err
}
}
}
return productGroup, nil
}
func (r *ProductGroupRepository) UpdateProductGroup(productGroup *models.ProductGroup) (*models.ProductGroup, error) {
query := "UPDATE product_groups SET label=$1, description=$2, parentgroupid=$3 WHERE id=$4"
_, err := r.db.Exec(query, productGroup.Label, productGroup.Description, productGroup.ParentGroupID, productGroup.ID)
if err != nil {
return nil, err
}
return productGroup, nil
}
func (r *ProductGroupRepository) DeleteProductGroup(id int) error {
query := "DELETE FROM product_groups WHERE id=$1"
_, err := r.db.Exec(query, id)
if err != nil {
return err
}
return nil
}
func (r *ProductGroupRepository) GetProductGroup(id int) (*models.ProductGroup, error) {
query := "SELECT id, label, description, parentgroupid FROM product_groups WHERE id=$1"
row := r.db.QueryRow(query, id)
productGroup := &models.ProductGroup{}
err := row.Scan(&productGroup.ID, &productGroup.Label, &productGroup.Description, &productGroup.ParentGroupID)
if err != nil {
return nil, err
}
return productGroup, nil
}
func (r *ProductGroupRepository) GetProductGroupsByParentId(parentId int) ([]*models.ProductGroup, error) {
query := "SELECT id, label, description, parentgroupid FROM product_groups WHERE parentgroupid=$1"
rows, err := r.db.Query(query, parentId)
if err != nil {
return nil, err
}
defer rows.Close()
var productGroups []*models.ProductGroup
for rows.Next() {
var productGroup models.ProductGroup
err := rows.Scan(&productGroup.ID, &productGroup.Label, &productGroup.Description, &productGroup.ParentGroupID)
if err != nil {
return nil, err
}
productGroups = append(productGroups, &productGroup)
}
if err = rows.Err(); err != nil {
return nil, err
}
return productGroups, nil
}
func (r *ProductGroupRepository) DeleteProductFromProductGroupId(productGroupId int) error {
query := "DELETE FROM products WHERE productgroupid=$1"
_, err := r.db.Exec(query, productGroupId)
if err != nil {
return err
}
return nil
}
func (r *ProductGroupRepository) GetPropertiesFromProductGroupId(productGroupId int) ([]*models.Property, error) {
query := "SELECT id, label, description, propertytypeid, productid, productgroupid FROM properties WHERE productgroupid=$1"
rows, err := r.db.Query(query, productGroupId)
if err != nil {
return nil, err
}
defer rows.Close()
var properties []*models.Property
for rows.Next() {
var property models.Property
err := rows.Scan(&property.ID, &property.Label, &property.Description, &property.PropertyTypeID, &property.ProductID, &property.ProductGroupID)
if err != nil {
return nil, err
}
properties = append(properties, &property)
}
if err = rows.Err(); err != nil {
return nil, err
}
return properties, nil
}
func (r *ProductGroupRepository) DeletePropertyFromProductGroup(productGroupId int, propertyId int) error {
query := "DELETE FROM properties WHERE productgroupid=$1 AND id=$2"
_, err := r.db.Exec(query, productGroupId, propertyId)
if err != nil {
return err
}
return nil
}
func (r *ProductGroupRepository) CreatePropertyFromProductGroup(productGroupId int, property *models.Property) (*models.Property, error) {
query := "INSERT INTO properties (label, description, propertytypeid, productid, productgroupid) VALUES($1,$2,$3,$4,$5) RETURNING id"
err := r.db.QueryRow(query, property.Label, property.Description, property.PropertyTypeID, property.ProductID, productGroupId).Scan(&property.ID)
if err != nil {
return nil, err
}
// copy property values from parent group to new property
if productGroupId != 0 {
err := r.valueRepository.copyValuesFromPropertyTypeToProperty(property.ID, property.PropertyTypeID)
if err != nil {
return nil, err
}
}
return property, nil
}
@@ -0,0 +1,126 @@
package repositories
import (
"database/sql"
"simplycpq/models"
)
type ProductRepository struct {
dbHandler *sql.DB
productGroupRepository *ProductGroupRepository
valueRepository *ValueRepository
}
func NewProductRepository(dbHandler *sql.DB, productGroupRepository *ProductGroupRepository, valueRepository *ValueRepository) *ProductRepository {
return &ProductRepository{
dbHandler: dbHandler,
productGroupRepository: productGroupRepository,
valueRepository: valueRepository,
}
}
func (pr *ProductRepository) CreateProduct(product *models.Product) (*models.Product, error) {
// Implement the logic to insert a new product into the database
query := "INSERT INTO products (label, externalId, description, productGroupId) VALUES ($1, $2, $3, $4) RETURNING id"
err := pr.dbHandler.QueryRow(query, product.Label, product.ExternalID, product.Description, product.ProductGroupID).Scan(&product.ID)
if err != nil {
return nil, err
}
// copy Properties from ProductGroup to Product
properties, err := pr.productGroupRepository.GetPropertiesFromProductGroupId(product.ProductGroupID)
if err != nil {
return nil, err
}
for _, property := range properties {
_, err := pr.valueRepository.createPropertyFromProperty(0, product.ID, property)
if err != nil {
return nil, err
}
}
return product, nil
}
func (pr *ProductRepository) GetProductById(id int) (*models.Product, error) {
// Implement the logic to retrieve a product by its ID from the database
query := "SELECT id, label, externalId, description, productGroupId FROM products WHERE id = $1"
row := pr.dbHandler.QueryRow(query, id)
product := &models.Product{}
err := row.Scan(&product.ID, &product.Label, &product.ExternalID, &product.Description, &product.ProductGroupID)
if err != nil {
return nil, err
}
return product, nil
}
func (pr *ProductRepository) UpdateProduct(product *models.Product) error {
// Implement the logic to update an existing product in the database
query := "UPDATE products SET label = $1, externalId = $2, description = $3, productGroupId = $4 WHERE id = $5"
_, err := pr.dbHandler.Exec(query, product.Label, product.ExternalID, product.Description, product.ProductGroupID, product.ID)
return err
}
func (pr *ProductRepository) DeleteProduct(id int) error {
// Implement the logic to delete a product from the database
query := "DELETE FROM products WHERE id = $1"
_, err := pr.dbHandler.Exec(query, id)
return err
}
func (pr *ProductRepository) GetProductsByGroupId(groupId int) ([]*models.Product, error) {
// Implement the logic to retrieve all products by their group ID from the database
query := "SELECT id, label, externalId, description, productGroupId FROM products WHERE productGroupId = $1"
rows, err := pr.dbHandler.Query(query, groupId)
if err != nil {
return nil, err
}
defer rows.Close()
products := []*models.Product{}
for rows.Next() {
product := &models.Product{}
err := rows.Scan(&product.ID, &product.Label, &product.ExternalID, &product.Description, &product.ProductGroupID)
if err != nil {
return nil, err
}
products = append(products, product)
}
return products, nil
}
func (pr *ProductRepository) AddPropertyToProduct(productId int, propertyId int) error {
// Implement the logic to add a property to a product in the database
query := "INSERT INTO product_properties (productId, propertyId) VALUES ($1, $2)"
_, err := pr.dbHandler.Exec(query, productId, propertyId)
return err
}
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)
return err
}
func (pr *ProductRepository) GetPropertiesFromProduct(productId int) ([]*models.Property, error) {
// Implement the logic to retrieve all properties from a product in the database
query := "SELECT id, label, description, propertyTypeId, productId, productGroupId FROM properties WHERE productId = $1"
rows, err := pr.dbHandler.Query(query, productId)
if err != nil {
return nil, err
}
defer rows.Close()
properties := []*models.Property{}
for rows.Next() {
property := &models.Property{}
err := rows.Scan(&property.ID, &property.Label, &property.Description, &property.PropertyTypeID, &property.ProductID, &property.ProductGroupID)
if err != nil {
return nil, err
}
properties = append(properties, property)
}
return properties, nil
}
@@ -267,6 +267,48 @@ func (vr *ValueRepository) GetValue(id int) (*models.Value, error) {
return &value, nil
}
func (vr *ValueRepository) createPropertyFromPropertyType(productGroupId int, productId int, propertyType *models.PropertyType) (*models.Property, error) {
query := `INSERT INTO properties (label, description, propertytypeid, productid, productgroupid) VALUES ($1, $2, $3, $4, $5) RETURNING id`
var property models.Property
err := vr.db.QueryRow(query, propertyType.Label, propertyType.Description, propertyType.ID, productId, productGroupId).Scan(&property.ID)
if err != nil {
return nil, err
}
property.Label = propertyType.Label
property.Description = propertyType.Description
property.PropertyTypeID = propertyType.ID
property.ProductID = productId
property.ProductGroupID = productGroupId
// Copy values from property type to property
err = vr.copyValuesFromPropertyTypeToProperty(propertyType.ID, property.ID)
if err != nil {
return nil, err
}
return &property, nil
}
func (vr *ValueRepository) createPropertyFromProperty(productGroupId int, productId int, property *models.Property) (*models.Property, error) {
query := `INSERT INTO properties (label, description, propertytypeid, productid, productgroupid) VALUES ($1, $2, $3, $4, $5) RETURNING id`
var newProperty models.Property
err := vr.db.QueryRow(query, property.Label, property.Description, property.PropertyTypeID, productId, productGroupId).Scan(&newProperty.ID)
if err != nil {
return nil, err
}
newProperty.Label = property.Label
newProperty.Description = property.Description
newProperty.PropertyTypeID = property.PropertyTypeID
newProperty.ProductID = productId
newProperty.ProductGroupID = productGroupId
// Copy values from property type to property
err = vr.copyValuesFromPropertyToProperty(property.ID, newProperty.ID)
if err != nil {
return nil, err
}
return &newProperty, nil
}
func (vr *ValueRepository) copyValuesFromPropertyTypeToProperty(propertyTypeId int, propertyId int) error {
// Implement the logic to copy values from a property type to a property
query := `INSERT INTO values (propertyId, label, description) SELECT $1, label, description FROM values WHERE propertytypeid=$2`