From 62af3f17f23e72c04cfb5cb8221b7fce3e90d823 Mon Sep 17 00:00:00 2001 From: tsindt Date: Wed, 16 Sep 2026 16:32:31 +0200 Subject: [PATCH] Tree steht --- .../xcpq_editor/cmd/server/httpServer.go | 5 ++ .../handlers/property_types_handler.go | 42 +++++----- .../repositories/property_repository.go | 5 +- .../property_types_group_repository.go | 77 ++++++++++++++++--- .../internal/services/propertyTypeService.go | 38 ++++++++- .../templates/partials/pt_treeNode.html | 19 +++-- .../templates/partials/ptg_treeNode.html | 9 ++- 7 files changed, 152 insertions(+), 43 deletions(-) diff --git a/xcpq/buildtime/xcpq_editor/cmd/server/httpServer.go b/xcpq/buildtime/xcpq_editor/cmd/server/httpServer.go index 7c4f7b370..7cdb44da2 100644 --- a/xcpq/buildtime/xcpq_editor/cmd/server/httpServer.go +++ b/xcpq/buildtime/xcpq_editor/cmd/server/httpServer.go @@ -46,6 +46,10 @@ func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer { // Service API router := gin.Default() + + // initialize templates + router.LoadHTMLGlob("../templates/**/*.html") + router.Use(cors.Default()) // static content @@ -55,6 +59,7 @@ func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer { // properties router.GET("/properties/rootnodes", propertyTypesHandler.GetRootPropertyTypesGroups) + router.GET("/properties/:id/childs", propertyTypesHandler.GetPropertyTypesGroupDirectChildsById) /* router.GET("/products", handlers.ProductsHandler) diff --git a/xcpq/buildtime/xcpq_editor/internal/handlers/property_types_handler.go b/xcpq/buildtime/xcpq_editor/internal/handlers/property_types_handler.go index 12f3fe014..fc46ee317 100644 --- a/xcpq/buildtime/xcpq_editor/internal/handlers/property_types_handler.go +++ b/xcpq/buildtime/xcpq_editor/internal/handlers/property_types_handler.go @@ -1,9 +1,9 @@ package handlers import ( - "html/template" "log" "net/http" + "strconv" "xcpq_editor/internal/services" "github.com/gin-gonic/gin" @@ -21,7 +21,6 @@ func NewPropertyTypesHandler(propertyTypeService *services.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"}) @@ -34,28 +33,35 @@ func (h *PropertyTypesHandler) GetRootPropertyTypesGroups(ctx *gin.Context) { 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", - ) + // execute template + log.Printf("Rendering root property types groups template with data") + ctx.HTML(http.StatusOK, "ptg_treeNode", rootGroups) + log.Printf("Rendered root property types groups template successfully") +} + +func (h *PropertyTypesHandler) GetPropertyTypesGroupDirectChildsById(ctx *gin.Context) { + idParam := ctx.Param("id") + id, err := strconv.Atoi(idParam) + if err != nil { + ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"}) + return + } + + propertyTypesGroup, err := h.propertyTypeService.GetPropertyTypesGroupWithDirectChildsById(id) if err != nil { - log.Printf("Error parsing template: %v", err) ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } - log.Printf("Template parsed successfully") + if propertyTypesGroup == nil { + ctx.JSON(http.StatusNotFound, gin.H{"error": "Property types group not found"}) + return + } + log.Printf("Rendering direct child property types groups template for property types group with ID %d", id) + log.Printf("Direct child property types groups for property types group with ID %d: %v", id, propertyTypesGroup.Property_Types_Groups) + log.Printf("Direct child property types groups for property types with ID %d: %v", id, propertyTypesGroup.Property_Types) // execute template - log.Printf("Executing template with root property types groups") - err = tmpl.Execute(ctx.Writer, rootGroups) - if err != nil { - log.Printf("Error executing template: %v", err) - ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - return - } - log.Printf("Template executed successfully") - ctx.JSON(http.StatusOK, rootGroups) + ctx.HTML(http.StatusOK, "ptg_treeNode", propertyTypesGroup.Property_Types_Groups) } diff --git a/xcpq/buildtime/xcpq_editor/internal/repositories/property_repository.go b/xcpq/buildtime/xcpq_editor/internal/repositories/property_repository.go index ffb8e4de6..7fa0f0a54 100644 --- a/xcpq/buildtime/xcpq_editor/internal/repositories/property_repository.go +++ b/xcpq/buildtime/xcpq_editor/internal/repositories/property_repository.go @@ -138,8 +138,6 @@ func (pr *Property_Repository) GetPropertyByPropertyType(property_type *models.P 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 @@ -148,12 +146,11 @@ func (pr *Property_Repository) GetPropertyByPropertyType(property_type *models.P 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 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 af2481268..35df6f980 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 @@ -45,7 +45,40 @@ 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 property + 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 + } + defer rowsProperties.Close() + + for rowsProperties.Next() { + id := 0 + err := rowsProperties.Scan(&id) + + if err != nil { + return nil, err + } + log.Printf("Retrieved property type with ID %d for property types group with ID %d", id, property_types_group.Id) + property, err := ptrg.property_Type_Repository.GetPropertyTypeById(id) + if err != nil { + return nil, err + } + log.Printf("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) + } + + return property_types_group, nil +} + +func (ptrg *Property_Types_Group_Repository) GetPropertyTypesGroupWithDirectChildsById(id int) (*models.Property_Types_Group, error) { + // Implement the logic to retrieve a property types group by its ID from the database + property_types_group, err := ptrg.GetPropertyTypesGroupById(id) + if err != nil { + return nil, err + } + // get all childs queryChilds := "SELECT id FROM properties.property_types_groups WHERE parent_property_type_group_id = $1" rows, err := ptrg.db.Query(queryChilds, id) @@ -60,16 +93,44 @@ 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) + return property_types_group, nil +} + +func (ptrg *Property_Types_Group_Repository) GetPropertyTypesGroupDirectChildGroupsById(id int) ([]*models.Property_Types_Group, error) { + // get all childs + 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 + } + defer rows.Close() + + property_Types_Groups := []*models.Property_Types_Group{} + + for rows.Next() { + id := 0 + err := rows.Scan(&id) + if err != nil { + return nil, err + } + child, err := ptrg.GetPropertyTypesGroupById(id) + if err != nil { + return nil, err + } + property_Types_Groups = append(property_Types_Groups, child) + } + return property_Types_Groups, nil +} + +func (ptrg *Property_Types_Group_Repository) GetPropertyTypesGroupPropertyTypesById(id int) ([]*models.Property_Type, error) { + // get all property queryProperties := "SELECT id FROM properties.property_types WHERE property_types_groups_id = $1" rowsProperties, err := ptrg.db.Query(queryProperties, id) @@ -78,22 +139,20 @@ func (ptrg *Property_Types_Group_Repository) GetPropertyTypesGroupById(id int) ( } defer rowsProperties.Close() + property_Types := []*models.Property_Type{} for rowsProperties.Next() { id := 0 err := rowsProperties.Scan(&id) 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) + property_Types = append(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 + return property_Types, nil } func (ptrg *Property_Types_Group_Repository) UpdatePropertyTypesGroup(property_types_group *models.Property_Types_Group) error { diff --git a/xcpq/buildtime/xcpq_editor/internal/services/propertyTypeService.go b/xcpq/buildtime/xcpq_editor/internal/services/propertyTypeService.go index f32bc5864..35afcad90 100644 --- a/xcpq/buildtime/xcpq_editor/internal/services/propertyTypeService.go +++ b/xcpq/buildtime/xcpq_editor/internal/services/propertyTypeService.go @@ -23,7 +23,41 @@ func (s *PropertyTypeService) GetRootPropertyTypesGroups() ([]*models.Property_T var rootGroups []*models.Property_Types_Group var err error - rootGroups, err = s.property_Types_Groups_Repository.GetRootPropertyTypesGroups() + rootGroupsTemp, err := s.property_Types_Groups_Repository.GetRootPropertyTypesGroups() + if err != nil { + return nil, err + } + for i := range rootGroupsTemp { + rootGroups = append(rootGroups, rootGroupsTemp[i]) + } log.Printf("Service Loaded %d root property types groups", len(rootGroups)) - return rootGroups, err + return rootGroups, nil +} + +func (s *PropertyTypeService) GetPropertyTypesGroupWithDirectChildsById(id int) (*models.Property_Types_Group, error) { + propertyTypesGroup, err := s.property_Types_Groups_Repository.GetPropertyTypesGroupWithDirectChildsById(id) + + if err != nil { + return nil, err + } + log.Printf("Service Loaded property types group with ID %d", propertyTypesGroup.Id) + return propertyTypesGroup, nil +} + +func (s *PropertyTypeService) GetPropertyTypesGroupDirectChildGroupsById(id int) ([]*models.Property_Types_Group, error) { + childGroups, err := s.property_Types_Groups_Repository.GetPropertyTypesGroupDirectChildGroupsById(id) + if err != nil { + return nil, err + } + log.Printf("Service Loaded %d direct child property types groups for property types group with ID %d", len(childGroups), id) + return childGroups, nil +} + +func (s *PropertyTypeService) GetPropertyTypesGroupPropertyTypesById(id int) ([]*models.Property_Type, error) { + propertyTypes, err := s.property_Types_Groups_Repository.GetPropertyTypesGroupPropertyTypesById(id) + if err != nil { + return nil, err + } + log.Printf("Service Loaded %d property types for property types group with ID %d", len(propertyTypes), id) + return propertyTypes, nil } diff --git a/xcpq/buildtime/xcpq_editor/templates/partials/pt_treeNode.html b/xcpq/buildtime/xcpq_editor/templates/partials/pt_treeNode.html index fab1efc9d..8e9963265 100644 --- a/xcpq/buildtime/xcpq_editor/templates/partials/pt_treeNode.html +++ b/xcpq/buildtime/xcpq_editor/templates/partials/pt_treeNode.html @@ -1,14 +1,21 @@ -{{template "pt_treeNode" .}} +{{define "ptg_treeNode"}} {{range .}} -