Propertytypegroups-Tree

This commit is contained in:
2026-09-15 19:27:04 +02:00
parent 33acfb0bfc
commit dcb6295091
12 changed files with 220 additions and 20 deletions
@@ -2,6 +2,7 @@ package repositories
import (
"database/sql"
"log"
"xcpq_editor/internal/models"
)
@@ -80,6 +81,7 @@ func (pr *Property_Repository) GetPropertyById(id int) (*models.Property, error)
// Implement the logic to retrieve a property by its ID from the database
query := `SELECT id, name, description, parent_property_id, image_url FROM properties.properties WHERE id = $1`
property := &models.Property{}
log.Printf("Repository: Retrieving property with ID %d", id)
var parent_property_id int
err := pr.db.QueryRow(query, id).Scan(&property.Id, &property.Name, &property.Description, &parent_property_id, &property.Image_Url)
if err != nil {
@@ -89,6 +91,7 @@ func (pr *Property_Repository) GetPropertyById(id int) (*models.Property, error)
parent_property, err := pr.GetPropertyById(parent_property_id)
if err == nil {
property.Parent_Property = parent_property
log.Printf("Repository: Appending parent property with ID %d to property with ID %d", parent_property.Id, property.Id)
}
} else {
property.Parent_Property = nil
@@ -128,3 +131,45 @@ func (pr *Property_Repository) DeleteProperty(id int) error {
_, err = pr.db.Exec(query, id)
return err
}
func (pr *Property_Repository) GetPropertyByPropertyType(property_type *models.Property_Type) (*models.Property, error) {
// Implement the logic to retrieve a property by its property type ID from the database
query := `SELECT id, name, description, parent_property_id, products_groups_id, products_id, image_url
FROM properties.properties
WHERE property_type_id = $1`
property := &models.Property{}
property.Property_Type = property_type
log.Printf("Repository: Retrieving property for property type with ID %d", property_type.Id)
var parent_property_id int
var products_groups_id int
var products_id int
err := pr.db.QueryRow(query, property_type.Id).Scan(&property.Id, &property.Name, &property.Description, &parent_property_id, &products_groups_id, &products_id, &property.Image_Url)
if err != nil {
return nil, err
}
log.Printf("Repository: Retrieved property with ID %d for property type with ID %d", property.Id, property_type.Id)
if parent_property_id != 0 {
parent_property, err := pr.GetPropertyById(parent_property_id)
if err == nil {
property.Parent_Property = parent_property
log.Printf("Repository: Appending parent property with ID %d to property with ID %d", parent_property.Id, property.Id)
}
} else {
property.Parent_Property = nil
}
valuePtrs, err := pr.vr.GetValuesFromPropertyId(property.Id)
if err != nil {
return property, err
}
property.Values = make([]models.Value, 0, len(valuePtrs))
for _, value := range valuePtrs {
if value != nil {
property.Values = append(property.Values, *value)
}
}
return property, nil
}
@@ -2,6 +2,7 @@ package repositories
import (
"database/sql"
"log"
"xcpq_editor/internal/models"
)
@@ -56,7 +57,12 @@ func (ptr *Property_Type_Repository) GetPropertyTypeById(id int) (*models.Proper
if err != nil {
return nil, err
}
property, err := ptr.pr.GetPropertyById(property_type.Id)
log.Printf("Repository: Retrieved property type with ID %d", property_type.Id)
property, err := ptr.pr.GetPropertyByPropertyType(property_type)
if err != nil {
return nil, err
}
log.Printf("Repository: Retrieved property with ID %d for property type with ID %d", property.Id, property_type.Id)
property_type.Property = *property
return property_type, nil
}
@@ -2,6 +2,7 @@ package repositories
import (
"database/sql"
"log"
"xcpq_editor/internal/models"
)
@@ -44,9 +45,9 @@ func (ptrg *Property_Types_Group_Repository) GetPropertyTypesGroupById(id int) (
if err != nil {
return nil, err
}
log.Printf("Repository: Retrieved property types group with ID %d", id)
// get all childs
queryChilds := "SELECT id FROM properties.property_types_groups WHERE parent_property_types_groups_id = $1"
queryChilds := "SELECT id FROM properties.property_types_groups WHERE parent_property_type_group_id = $1"
rows, err := ptrg.db.Query(queryChilds, id)
if err != nil {
return nil, err
@@ -59,15 +60,18 @@ func (ptrg *Property_Types_Group_Repository) GetPropertyTypesGroupById(id int) (
if err != nil {
return nil, err
}
log.Printf("Repository: Retrieved child property types group with ID %d", id)
child, err := ptrg.GetPropertyTypesGroupById(id)
if err != nil {
return nil, err
}
log.Printf("Repository: Appending child property types group with ID %d to parent with ID %d", child.Id, property_types_group.Id)
property_types_group.Property_Types_Groups = append(property_types_group.Property_Types_Groups, *child)
}
log.Printf("Repository: Retrieved all child property types groups for parent with ID %d", property_types_group.Id)
// get all property
queryProperties := "SELECT id FROM property_types WHERE property_types_group = $1"
queryProperties := "SELECT id FROM properties.property_types WHERE property_types_groups_id = $1"
rowsProperties, err := ptrg.db.Query(queryProperties, id)
if err != nil {
return nil, err
@@ -80,13 +84,15 @@ func (ptrg *Property_Types_Group_Repository) GetPropertyTypesGroupById(id int) (
if err != nil {
return nil, err
}
log.Printf("Repository: Retrieved property type with ID %d", id)
property, err := ptrg.property_Type_Repository.GetPropertyTypeById(id)
if err != nil {
return nil, err
}
log.Printf("Repository: Appending property type with ID %d to property types group with ID %d", property.Id, property_types_group.Id)
property_types_group.Property_Types = append(property_types_group.Property_Types, *property)
}
log.Printf("Repository: Retrieved all property types for property types group with ID %d", property_types_group.Id)
return property_types_group, nil
}
@@ -103,7 +109,7 @@ func (ptrg *Property_Types_Group_Repository) DeletePropertyTypesGroup(id int) er
_, err := ptrg.db.Exec(query, id)
// delete all child property types groups
queryChilds := "SELECT id FROM properties.property_types_groups WHERE parent_property_types_groups_id = $1"
queryChilds := "SELECT id FROM properties.property_types_groups WHERE parent_property_type_group_id = $1"
rows, err := ptrg.db.Query(queryChilds, id)
if err != nil {
return err
@@ -123,7 +129,7 @@ func (ptrg *Property_Types_Group_Repository) DeletePropertyTypesGroup(id int) er
}
// delete all property types in this group
queryProperties := "SELECT id FROM properties.property_types WHERE property_types_group = $1"
queryProperties := "SELECT id FROM properties.property_types WHERE property_types_groups_id = $1"
rowsProperties, err := ptrg.db.Query(queryProperties, id)
if err != nil {
return err
@@ -144,3 +150,31 @@ func (ptrg *Property_Types_Group_Repository) DeletePropertyTypesGroup(id int) er
return err
}
func (ptrg *Property_Types_Group_Repository) GetRootPropertyTypesGroups() ([]*models.Property_Types_Group, error) {
query := "SELECT id FROM properties.property_types_groups WHERE parent_property_type_group_id = 0"
rows, err := ptrg.db.Query(query)
if err != nil {
log.Printf("Error querying root property types groups: %v", err)
return nil, err
}
defer rows.Close()
var rootGroups []*models.Property_Types_Group
for rows.Next() {
id := 0
err := rows.Scan(&id)
if err != nil {
return nil, err
}
log.Printf("Loading root property types group with ID: %d", id)
group, err := ptrg.GetPropertyTypesGroupById(id)
if err != nil {
log.Printf("Error loading root property types group with ID: %d: %v", id, err)
return nil, err
}
rootGroups = append(rootGroups, group)
}
log.Printf("Loaded %d root property types groups", len(rootGroups))
return rootGroups, nil
}