From dcb629509188b992f4655d7a9316d7cc486c5a01 Mon Sep 17 00:00:00 2001 From: tsindt Date: Tue, 15 Sep 2026 19:27:04 +0200 Subject: [PATCH] Propertytypegroups-Tree --- xcpq/buildtime/xcpq_editor/cmd/config.toml | 4 +- .../xcpq_editor/cmd/server/httpServer.go | 6 ++ .../handlers/property_types_handler.go | 59 +++++++++++++++++++ .../internal/models/product_group.go | 9 +++ .../xcpq_editor/internal/models/property.go | 15 +++-- .../repositories/property_repository.go | 45 ++++++++++++++ .../repositories/property_type_repository.go | 8 ++- .../property_types_group_repository.go | 46 +++++++++++++-- .../internal/services/propertyTypeService.go | 29 +++++++++ .../templates/layouts/properties.html | 13 +++- .../xcpq_editor/templates/partials/.ignore | 0 .../templates/partials/ptg_treeNode.html | 6 +- 12 files changed, 220 insertions(+), 20 deletions(-) create mode 100644 xcpq/buildtime/xcpq_editor/internal/handlers/property_types_handler.go create mode 100644 xcpq/buildtime/xcpq_editor/internal/models/product_group.go create mode 100644 xcpq/buildtime/xcpq_editor/internal/services/propertyTypeService.go delete mode 100644 xcpq/buildtime/xcpq_editor/templates/partials/.ignore diff --git a/xcpq/buildtime/xcpq_editor/cmd/config.toml b/xcpq/buildtime/xcpq_editor/cmd/config.toml index 697182581..870a09505 100644 --- a/xcpq/buildtime/xcpq_editor/cmd/config.toml +++ b/xcpq/buildtime/xcpq_editor/cmd/config.toml @@ -4,8 +4,8 @@ # password= dbname= [database] -##connection_string = "host=192.168.178.50 port=5432 user=tsindt password=XD14ko8d dbname=xcpq sslmode=disable" -connection_string = "host=localhost port=5432 user=tsindt password=XD14ko8d dbname=xcpq sslmode=disable" +connection_string = "host=192.168.178.50 port=5432 user=tsindt password=XD14ko8d dbname=xcpq sslmode=disable" +##connection_string = "host=localhost port=5432 user=tsindt password=XD14ko8d dbname=xcpq sslmode=disable" driver_name = "postgres" ################################################################ diff --git a/xcpq/buildtime/xcpq_editor/cmd/server/httpServer.go b/xcpq/buildtime/xcpq_editor/cmd/server/httpServer.go index 45a22b38f..7c4f7b370 100644 --- a/xcpq/buildtime/xcpq_editor/cmd/server/httpServer.go +++ b/xcpq/buildtime/xcpq_editor/cmd/server/httpServer.go @@ -38,9 +38,11 @@ func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer { // service initialisation: importService := services.NewImportService(importRepository) + propertyTypesService := services.NewPropertyTypeService(propertyTypeRepository, propertyTypeGroupRepository) // handler initialisation: importHandler := handlers.NewImportHandler(importService) + propertyTypesHandler := handlers.NewPropertyTypesHandler(propertyTypesService) // Service API router := gin.Default() @@ -50,6 +52,10 @@ func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer { router.Static("/static", "../static") router.GET("/", handlers.Home) router.GET("/properties", handlers.PropertiesHandler) + + // properties + router.GET("/properties/rootnodes", propertyTypesHandler.GetRootPropertyTypesGroups) + /* router.GET("/products", handlers.ProductsHandler) router.GET("/conditions", handlers.ConditionsHandler) diff --git a/xcpq/buildtime/xcpq_editor/internal/handlers/property_types_handler.go b/xcpq/buildtime/xcpq_editor/internal/handlers/property_types_handler.go new file mode 100644 index 000000000..777ae3aa3 --- /dev/null +++ b/xcpq/buildtime/xcpq_editor/internal/handlers/property_types_handler.go @@ -0,0 +1,59 @@ +package handlers + +import ( + "html/template" + "log" + "net/http" + "xcpq_editor/internal/services" + + "github.com/gin-gonic/gin" +) + +type PropertyTypesHandler struct { + propertyTypeService *services.PropertyTypeService +} + +func NewPropertyTypesHandler(propertyTypeService *services.PropertyTypeService) *PropertyTypesHandler { + return &PropertyTypesHandler{ + propertyTypeService: propertyTypeService, + } +} + +func (h *PropertyTypesHandler) GetRootPropertyTypesGroups(ctx *gin.Context) { + rootGroups, err := h.propertyTypeService.GetRootPropertyTypesGroups() + log.Printf("Handler retrieved %d root property types groups", len(rootGroups)) + + if rootGroups == nil { + ctx.JSON(http.StatusNotFound, gin.H{"error": "No root property types groups found"}) + return + } + + // If there was an error but rootGroups is not nil, we still want to return the data + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + log.Printf("Loading template for %d root property types groups", len(rootGroups)) + // template for handling the response can be added here if needed + tmpl, err := template.ParseFiles( + "../templates/partials/ptg_treeNode.html", + ) + + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + log.Printf("Template parsed successfully") + + // execute template + log.Printf("Executing template with root property types groups") + err = tmpl.Execute(ctx.Writer, rootGroups) + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + log.Printf("Template executed successfully") + ctx.JSON(http.StatusOK, rootGroups) +} diff --git a/xcpq/buildtime/xcpq_editor/internal/models/product_group.go b/xcpq/buildtime/xcpq_editor/internal/models/product_group.go new file mode 100644 index 000000000..fc84a57f6 --- /dev/null +++ b/xcpq/buildtime/xcpq_editor/internal/models/product_group.go @@ -0,0 +1,9 @@ +package models + +type Product_Group struct { + Id int `json:"id,omitempty"` + Name string `json:"name"` + Description string `json:"description,omitempty"` + Image_Url string `json:"image_url,omitempty"` + Properties []Property `json:"properties,omitempty"` +} diff --git a/xcpq/buildtime/xcpq_editor/internal/models/property.go b/xcpq/buildtime/xcpq_editor/internal/models/property.go index 0688c4b2c..d2a814ddd 100644 --- a/xcpq/buildtime/xcpq_editor/internal/models/property.go +++ b/xcpq/buildtime/xcpq_editor/internal/models/property.go @@ -1,10 +1,13 @@ package models type Property struct { - Id int `json:"id,omitempty"` - Name string `json:"name"` - Description string `json:"description,omitempty"` - Image_Url string `json:"image_url,omitempty"` - Parent_Property *Property `json:"parent_property,omitempty"` - Values []Value `json:"values,omitempty"` + Id int `json:"id,omitempty"` + Name string `json:"name"` + Description string `json:"description,omitempty"` + Image_Url string `json:"image_url,omitempty"` + Property_Type *Property_Type `json:"property_type,omitempty"` + Parent_Property *Property `json:"parent_property,omitempty"` + Products_Group *Product_Group `json:"products_group,omitempty"` + Product *Product `json:"products,omitempty"` + Values []Value `json:"values,omitempty"` } diff --git a/xcpq/buildtime/xcpq_editor/internal/repositories/property_repository.go b/xcpq/buildtime/xcpq_editor/internal/repositories/property_repository.go index 813f053d9..ffb8e4de6 100644 --- a/xcpq/buildtime/xcpq_editor/internal/repositories/property_repository.go +++ b/xcpq/buildtime/xcpq_editor/internal/repositories/property_repository.go @@ -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 +} diff --git a/xcpq/buildtime/xcpq_editor/internal/repositories/property_type_repository.go b/xcpq/buildtime/xcpq_editor/internal/repositories/property_type_repository.go index fa440dbe7..a376089ca 100644 --- a/xcpq/buildtime/xcpq_editor/internal/repositories/property_type_repository.go +++ b/xcpq/buildtime/xcpq_editor/internal/repositories/property_type_repository.go @@ -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 } diff --git a/xcpq/buildtime/xcpq_editor/internal/repositories/property_types_group_repository.go b/xcpq/buildtime/xcpq_editor/internal/repositories/property_types_group_repository.go index 4b9a0fdfd..af2481268 100644 --- a/xcpq/buildtime/xcpq_editor/internal/repositories/property_types_group_repository.go +++ b/xcpq/buildtime/xcpq_editor/internal/repositories/property_types_group_repository.go @@ -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 +} diff --git a/xcpq/buildtime/xcpq_editor/internal/services/propertyTypeService.go b/xcpq/buildtime/xcpq_editor/internal/services/propertyTypeService.go new file mode 100644 index 000000000..f32bc5864 --- /dev/null +++ b/xcpq/buildtime/xcpq_editor/internal/services/propertyTypeService.go @@ -0,0 +1,29 @@ +package services + +import ( + "log" + "xcpq_editor/internal/models" + "xcpq_editor/internal/repositories" +) + +type PropertyTypeService struct { + // Add any dependencies or fields needed for the service here + property_Types_Repository *repositories.Property_Type_Repository + property_Types_Groups_Repository *repositories.Property_Types_Group_Repository +} + +func NewPropertyTypeService(propertyTypesRepo *repositories.Property_Type_Repository, propertyTypesGroupsRepo *repositories.Property_Types_Group_Repository) *PropertyTypeService { + return &PropertyTypeService{ + property_Types_Repository: propertyTypesRepo, + property_Types_Groups_Repository: propertyTypesGroupsRepo, + } +} + +func (s *PropertyTypeService) GetRootPropertyTypesGroups() ([]*models.Property_Types_Group, error) { + var rootGroups []*models.Property_Types_Group + var err error + + rootGroups, err = s.property_Types_Groups_Repository.GetRootPropertyTypesGroups() + log.Printf("Service Loaded %d root property types groups", len(rootGroups)) + return rootGroups, err +} diff --git a/xcpq/buildtime/xcpq_editor/templates/layouts/properties.html b/xcpq/buildtime/xcpq_editor/templates/layouts/properties.html index 4677bb075..7b76d8b17 100644 --- a/xcpq/buildtime/xcpq_editor/templates/layouts/properties.html +++ b/xcpq/buildtime/xcpq_editor/templates/layouts/properties.html @@ -6,9 +6,16 @@

Property Type Groups

-
- Keine Daten vorhanden! -
+
    +
    + + X-CPQ-Eigenschaften + +
    + +
    +
    +
+{{end}}