finishing products API

This commit is contained in:
2026-08-17 20:12:24 +02:00
parent 989cda7036
commit a72b0fdd4b
7 changed files with 484 additions and 30 deletions
@@ -211,6 +211,30 @@ func (vr *ValueRepository) GetValuesFromPropertyTypeId(propertyTypeId int) ([]*m
return values, nil
}
func (vr *ValueRepository) CreatePropertyFromPropertyTypeId(propertyTypeId int, productGroupId int, productId int) (models.Property, error) {
// Implement the logic to create a property from the given propertyTypeId
query := `INSERT INTO properties(label, description, propertytypeid) SELECT label, description, id FROM propertytypes WHERE id=$1 RETURNING id, label, description`
var property models.Property
err := vr.db.QueryRow(query, propertyTypeId).Scan(&property.ID, &property.Label, &property.Description)
if err != nil {
return models.Property{}, err
}
property.PropertyTypeID = propertyTypeId
property.ProductGroupID = productGroupId
property.ProductID = productId
err = vr.db.QueryRow(`UPDATE properties SET productgroupid=$1, productid=$2 WHERE id=$3 RETURNING id`, productGroupId, productId, property.ID).Scan(&property.ID)
if err != nil {
return models.Property{}, err
}
err = vr.copyValuesFromPropertyTypeToProperty(propertyTypeId, property.ID)
if err != nil {
return models.Property{}, err
}
return property, nil
}
func (vr *ValueRepository) GetValuesFromPropertyId(propertyId int) ([]*models.Value, error) {
// Implement the logic to retrieve the values from the database for the given propertyId
query := `SELECT id, label, description, propertyId FROM values WHERE propertyId=$1`
@@ -245,10 +269,10 @@ func (vr *ValueRepository) CreateValueForPropertyId(propertyId int, value *model
return value, nil
}
func (vr *ValueRepository) DeleteValueFromPropertyId(id int) error {
// Implement the logic to delete the value from the database for the given id
query := `DELETE FROM values WHERE id=$1`
_, err := vr.db.Exec(query, id)
func (vr *ValueRepository) DeleteValueFromPropertyId(valueId int, propertyId int) error {
// Implement the logic to delete the value from the database for the given valueId and propertyId
query := `DELETE FROM values WHERE id=$1 AND propertyId=$2`
_, err := vr.db.Exec(query, valueId, propertyId)
if err != nil {
return err
}
@@ -311,7 +335,7 @@ func (vr *ValueRepository) createPropertyFromProperty(productGroupId int, produc
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`
query := `INSERT INTO values (propertyId, label, description) SELECT $1, label, description FROM values WHERE propertyid=$2`
_, err := vr.db.Exec(query, propertyId, propertyTypeId)
if err != nil {
return err
@@ -328,3 +352,61 @@ func (vr *ValueRepository) copyValuesFromPropertyToProperty(propertyIdFrom int,
}
return nil
}
func (vr *ValueRepository) CreateTerminalPropertyForProductGroupId(productGroupId int, propertyTypeId int, value string) error {
// Create a new property for the given productGroupId and propertyTypeId
propertyType, err := vr.GetPropertyType(propertyTypeId)
if err != nil {
return err
}
query := `INSERT INTO properties(label, description, propertytypeid, productgroupid, value) VALUES($1, $2, $3, $4) RETURNING id`
var property models.Property
err = vr.db.QueryRow(query, propertyType.Label, propertyType.Description, propertyTypeId, productGroupId).Scan(&property.ID)
if err != nil {
return err
}
err = vr.db.QueryRow(`INSERT INTO values(propertyid, label, description) VALUES($1, $2, $3) RETURNING id`, property.ID, value, "").Scan(&property.ID)
if err != nil {
return err
}
return nil
}
func (vr *ValueRepository) CreateTerminalPropertyForProductId(productId int, propertyTypeId int, value string) error {
// Create a new property for the given productGroupId and propertyTypeId
propertyType, err := vr.GetPropertyType(propertyTypeId)
if err != nil {
return err
}
query := `INSERT INTO properties(label, description, propertytypeid, productid, value) VALUES($1, $2, $3, $4, $5) RETURNING id`
var property models.Property
err = vr.db.QueryRow(query, propertyType.Label, propertyType.Description, propertyTypeId, productId, value).Scan(&property.ID)
if err != nil {
return err
}
err = vr.db.QueryRow(`INSERT INTO values(propertyid, label, description) VALUES($1, $2, $3) RETURNING id`, property.ID, value, "").Scan(&property.ID)
if err != nil {
return err
}
return nil
}
func (vr *ValueRepository) SetTerminalValueForProperty(propertyId int, value string) error {
// Implement the logic to set a terminal value for a property
// remove all values from the property
_, err := vr.db.Exec(`DELETE FROM values WHERE propertyid=$1`, propertyId)
if err != nil {
return err
}
// add the new value to the property
_, err = vr.db.Exec(`INSERT INTO values(propertyid, label, description) VALUES($1, $2, $3)`, propertyId, value, "")
if err != nil {
return err
}
return nil
}