From 5b9c2a4daee7bef415c216ea9d83849e3265028d Mon Sep 17 00:00:00 2001 From: tsindt Date: Sun, 4 Oct 2026 16:41:46 +0200 Subject: [PATCH] TreeView aufgebaut --- .../internal/handler/propertyTypeHandler.go | 24 +++++++++------ buildtime/internal/models/propertyType.go | 10 +++---- .../repositories/propertyRepository.go | 10 +++---- .../propertyTypeGroupRepository.go | 29 +++++++++---------- .../repositories/propertyTypeRepository.go | 8 ++--- .../internal/repositories/valueRepository.go | 6 ++-- buildtime/templates/pages/index.html | 2 +- .../templates/partials/ptg_treenode.html | 6 ++-- 8 files changed, 50 insertions(+), 45 deletions(-) diff --git a/buildtime/internal/handler/propertyTypeHandler.go b/buildtime/internal/handler/propertyTypeHandler.go index 2ba1d9c..d27743a 100644 --- a/buildtime/internal/handler/propertyTypeHandler.go +++ b/buildtime/internal/handler/propertyTypeHandler.go @@ -3,6 +3,7 @@ package handlers import ( "SimplyCPQ2/internal/models" "SimplyCPQ2/internal/services" + "net/http" "strconv" "github.com/gin-gonic/gin" @@ -40,23 +41,30 @@ func (h *PropertyTypeHandler) GetPropertyTypeGroupContentById(ctx *gin.Context) return } - ptg, err := h.propertyTypeService.GetPropertyTypeGroupByID(id) - if err != nil { - ctx.JSON(500, gin.H{"error": "Failed to get property type group"}) - return + var ptg models.PropertyTypeGroup + if id != 0 { + ptg, err = h.propertyTypeService.GetPropertyTypeGroupByID(id) + if err != nil { + ctx.JSON(500, gin.H{"error": "Failed to get property type group", "details": err.Error()}) + return + } + } else { + ptg.Name = "Root" + ptg.Id = 0 + ptg.ParentPropertyTypeGroupId = 0 } // get property type groups by parent ID ptgs, err := h.propertyTypeService.GetPropertyTypeGroupsByParentId(id) if err != nil { - ctx.JSON(500, gin.H{"error": "Failed to get property type group content"}) + ctx.JSON(500, gin.H{"error": "Failed to get property type group content", "details": err.Error()}) return } // get property types by parent ID pts, err := h.propertyTypeService.GetPropertyTypesByGroupId(id) if err != nil { - ctx.JSON(500, gin.H{"error": "Failed to get property types"}) + ctx.JSON(500, gin.H{"error": "Failed to get property types", "details": err.Error()}) return } @@ -68,9 +76,7 @@ func (h *PropertyTypeHandler) GetPropertyTypeGroupContentById(ctx *gin.Context) PropertyTypes: pts, } - ctx.HTML(200, "ptg_treenode.html", gin.H{ - "propertyTypeGroupContent": content, - }) + ctx.HTML(http.StatusOK, "ptg_treeNode", content) // Implement the logic to get property type group content by ID here using idParam } diff --git a/buildtime/internal/models/propertyType.go b/buildtime/internal/models/propertyType.go index 6b0e3ce..ffe2734 100644 --- a/buildtime/internal/models/propertyType.go +++ b/buildtime/internal/models/propertyType.go @@ -1,9 +1,9 @@ package models type PropertyType struct { - Id int `json:"id,omitempty"` - Name string `json:"name"` - Description string `json:"description,omitempty"` - ImageURL string `json:"image_url,omitempty"` - PropertyTypesGroupsId int `json:"property_types_groups_id,omitempty"` + Id int `json:"id,omitempty"` + Name string `json:"name"` + Description string `json:"description,omitempty"` + ImageURL string `json:"image_url,omitempty"` + PropertyTypeGroupId int `json:"property_types_groups_id,omitempty"` } diff --git a/buildtime/internal/repositories/propertyRepository.go b/buildtime/internal/repositories/propertyRepository.go index bda2728..187d0c9 100644 --- a/buildtime/internal/repositories/propertyRepository.go +++ b/buildtime/internal/repositories/propertyRepository.go @@ -39,7 +39,7 @@ func (r *PropertyRepository) GetPropertyByID(id int) (models.Property, error) { } var property models.Property - err := r.db.QueryRow("SELECT id, name, description, image_url, parent_property_id, property_type_id, product_groups_id, products_id FROM properties WHERE id = ?", id).Scan(&property.Id, &property.Name, &property.Description, &property.ImageURL, &property.ParentPropertyId, &property.PropertyTypeId, &property.ProductGroupId, &property.ProductId) + err := r.db.QueryRow("SELECT id, name, description, image_url, parent_property_id, property_type_id, product_groups_id, products_id FROM properties.properties WHERE id = $1", id).Scan(&property.Id, &property.Name, &property.Description, &property.ImageURL, &property.ParentPropertyId, &property.PropertyTypeId, &property.ProductGroupId, &property.ProductId) if err != nil { return models.Property{}, err } @@ -48,7 +48,7 @@ func (r *PropertyRepository) GetPropertyByID(id int) (models.Property, error) { } func (r *PropertyRepository) GetPropertiesByProductGroupID(pid int) ([]models.Property, error) { - rows, err := r.db.Query("SELECT id, name, description, image_url, parent_property_id, property_type_id, product_groups_id, products_id FROM properties WHERE product_groups_id = ?", pid) + rows, err := r.db.Query("SELECT id, name, description, image_url, parent_property_id, property_type_id, product_groups_id, products_id FROM properties.properties WHERE product_groups_id = $1", pid) if err != nil { return nil, err } @@ -75,7 +75,7 @@ func (r *PropertyRepository) GetPropertiesByProductGroupID(pid int) ([]models.Pr } func (r *PropertyRepository) GetPropertiesByProductID(pid int) ([]models.Property, error) { - rows, err := r.db.Query("SELECT id, name, description, image_url, parent_property_id, property_type_id, product_groups_id, products_id FROM properties WHERE products_id = ?", pid) + rows, err := r.db.Query("SELECT id, name, description, image_url, parent_property_id, property_type_id, product_groups_id, products_id FROM properties.properties WHERE products_id = $1", pid) if err != nil { return nil, err } @@ -102,7 +102,7 @@ func (r *PropertyRepository) GetPropertiesByProductID(pid int) ([]models.Propert } func (r *PropertyRepository) GetPropertiesByParentPropertyID(pid int) ([]models.Property, error) { - rows, err := r.db.Query("SELECT id, name, description, image_url, parent_property_id, property_type_id, product_groups_id, products_id FROM properties WHERE parent_property_id = ?", pid) + rows, err := r.db.Query("SELECT id, name, description, image_url, parent_property_id, property_type_id, product_groups_id, products_id FROM properties.properties WHERE parent_property_id = $1", pid) if err != nil { return nil, err } @@ -129,7 +129,7 @@ func (r *PropertyRepository) GetPropertiesByParentPropertyID(pid int) ([]models. } func (r *PropertyRepository) GetPropertyByPropertyTypeID(pid int) (models.Property, error) { - rows, err := r.db.Query("SELECT id, name, description, image_url, parent_property_id, property_type_id, product_groups_id, products_id FROM properties WHERE property_type_id = ?", pid) + rows, err := r.db.Query("SELECT id, name, description, image_url, parent_property_id, property_type_id, product_groups_id, products_id FROM properties.properties WHERE property_type_id = $1", pid) if err != nil { return models.Property{}, err } diff --git a/buildtime/internal/repositories/propertyTypeGroupRepository.go b/buildtime/internal/repositories/propertyTypeGroupRepository.go index 1e36427..af32572 100644 --- a/buildtime/internal/repositories/propertyTypeGroupRepository.go +++ b/buildtime/internal/repositories/propertyTypeGroupRepository.go @@ -3,6 +3,7 @@ package repositories import ( "SimplyCPQ2/internal/models" "database/sql" + "log" ) type PropertyTypeGroupRepository struct { @@ -23,18 +24,20 @@ func (r *PropertyTypeGroupRepository) GetPropertyTypeGroupByID(id int) (models.P if value, ok := r.cache[id]; ok { return value, nil } - + log.Println("Fetching property type group by ID:", id) var value models.PropertyTypeGroup - err := r.db.QueryRow("SELECT id, name, description, parenz_property_typw_goup_id, image_url FROM property_type_groups WHERE id = ?", id).Scan(&value.Id, &value.Name, &value.Description, &value.ParentPropertyTypeGroupId, &value.ImageURL) + err := r.db.QueryRow("SELECT id, name, description, parent_property_type_group_id, image_url FROM properties.property_types_groups WHERE id = $1", id).Scan(&value.Id, &value.Name, &value.Description, &value.ParentPropertyTypeGroupId, &value.ImageURL) if err != nil { + log.Println("Error fetching property type group by ID:", err) return models.PropertyTypeGroup{}, err } r.cache[id] = value + log.Println("Fetched property type group by ID:", value) return value, nil } func (r *PropertyTypeGroupRepository) GetPropertyTypeGroupsByParentId(parentId int) ([]models.PropertyTypeGroup, error) { - rows, err := r.db.Query("SELECT id, name, description, parenz_property_typw_goup_id, image_url FROM property_type_groups WHERE parenz_property_typw_goup_id = ?", parentId) + rows, err := r.db.Query("SELECT id, name, description, parent_property_type_group_id, image_url FROM properties.property_types_groups WHERE parent_property_type_group_id = $1", parentId) if err != nil { return nil, err } @@ -59,34 +62,30 @@ func (r *PropertyTypeGroupRepository) GetPropertyTypeGroupsByParentId(parentId i return groups, nil } -func (r *PropertyTypeGroupRepository) GetPropertyTypesByGroupId(gid int) ([]models.PropertyTypeGroup, error) { - rows, err := r.db.Query("SELECT id, name, description, parenz_property_typw_goup_id, image_url FROM property_type_groups WHERE parenz_property_typw_goup_id = ?", gid) +func (r *PropertyTypeGroupRepository) GetPropertyTypesByGroupId(gid int) ([]models.PropertyType, error) { + rows, err := r.db.Query("SELECT id, name, description, property_type_group_id, image_url FROM properties.property_types WHERE property_type_group_id = $1", gid) if err != nil { return nil, err } defer rows.Close() - var groups []models.PropertyTypeGroup + var types []models.PropertyType for rows.Next() { - var value models.PropertyTypeGroup - err := rows.Scan(&value.Id, &value.Name, &value.Description, &value.ParentPropertyTypeGroupId, &value.ImageURL) + var value models.PropertyType + err := rows.Scan(&value.Id, &value.Name, &value.Description, &value.PropertyTypeGroupId, &value.ImageURL) if err != nil { return nil, err } - if _, ok := r.cache[value.Id]; !ok { - r.cache[value.Id] = value - } - - groups = append(groups, value) + types = append(types, value) } if err = rows.Err(); err != nil { return nil, err } - return groups, nil + return types, nil } func (r *PropertyTypeGroupRepository) CreatePropertyTypeGroup(value models.PropertyTypeGroup) (models.PropertyTypeGroup, error) { - result, err := r.db.Exec("INSERT INTO property_type_groups (name) VALUES (?)", value.Name) + result, err := r.db.Exec("INSERT INTO properties.property_types_groups (name, description, parent_property_type_group_id, image_url) VALUES ($1, $2, $3, $4)", value.Name, value.Description, value.ParentPropertyTypeGroupId, value.ImageURL) if err != nil { return models.PropertyTypeGroup{}, err } diff --git a/buildtime/internal/repositories/propertyTypeRepository.go b/buildtime/internal/repositories/propertyTypeRepository.go index 7c170ad..a6efa5a 100644 --- a/buildtime/internal/repositories/propertyTypeRepository.go +++ b/buildtime/internal/repositories/propertyTypeRepository.go @@ -24,7 +24,7 @@ func (r *PropertyTypeRepository) GetPropertyTypeByID(id int) (models.PropertyTyp return propertyType, nil } var propertyType models.PropertyType - err := r.db.QueryRow("SELECT id, name, description, image_url, property_types_groups_id FROM property_types WHERE id = ?", id).Scan(&propertyType.Id, &propertyType.Name, &propertyType.Description, &propertyType.ImageURL, &propertyType.PropertyTypesGroupsId) + err := r.db.QueryRow("SELECT id, name, description, image_url, property_type_group_id FROM properties.property_types WHERE id = $1", id).Scan(&propertyType.Id, &propertyType.Name, &propertyType.Description, &propertyType.ImageURL, &propertyType.PropertyTypeGroupId) if err != nil { return models.PropertyType{}, err } @@ -33,7 +33,7 @@ func (r *PropertyTypeRepository) GetPropertyTypeByID(id int) (models.PropertyTyp } func (r *PropertyTypeRepository) GetPropertyTypesByGroupID(pid int) ([]models.PropertyType, error) { - rows, err := r.db.Query("SELECT id, name, description, image_url, property_types_groups_id FROM property_types WHERE property_types_groups_id = ?", pid) + rows, err := r.db.Query("SELECT id, name, description, image_url, property_type_group_id FROM properties.property_types WHERE property_type_group_id = $1", pid) if err != nil { return nil, err } @@ -42,7 +42,7 @@ func (r *PropertyTypeRepository) GetPropertyTypesByGroupID(pid int) ([]models.Pr var propertyTypes []models.PropertyType for rows.Next() { var propertyType models.PropertyType - err := rows.Scan(&propertyType.Id, &propertyType.Name, &propertyType.Description, &propertyType.ImageURL, &propertyType.PropertyTypesGroupsId) + err := rows.Scan(&propertyType.Id, &propertyType.Name, &propertyType.Description, &propertyType.ImageURL, &propertyType.PropertyTypeGroupId) if err != nil { return nil, err } @@ -60,7 +60,7 @@ func (r *PropertyTypeRepository) GetPropertyTypesByGroupID(pid int) ([]models.Pr } func (r *PropertyTypeRepository) createPropertyType(propertyType models.PropertyType) (models.PropertyType, error) { - result, err := r.db.Exec("INSERT INTO property_types (name, description, image_url, property_types_groups_id) VALUES (?, ?, ?, ?)", propertyType.Name, propertyType.Description, propertyType.ImageURL, propertyType.PropertyTypesGroupsId) + result, err := r.db.Exec("INSERT INTO properties.property_types (name, description, image_url, property_type_group_id) VALUES ($1, $2, $3, $4)", propertyType.Name, propertyType.Description, propertyType.ImageURL, propertyType.PropertyTypeGroupId) if err != nil { return models.PropertyType{}, err } diff --git a/buildtime/internal/repositories/valueRepository.go b/buildtime/internal/repositories/valueRepository.go index 1091ea8..bea66c2 100644 --- a/buildtime/internal/repositories/valueRepository.go +++ b/buildtime/internal/repositories/valueRepository.go @@ -23,7 +23,7 @@ func (r *ValueRepository) GetValueByID(id int) (models.Value, error) { } var value models.Value - err := r.db.QueryRow("SELECT id, value, image_url FROM values WHERE id = ?", id).Scan(&value.Id, &value.Value, &value.ImageURL) + err := r.db.QueryRow("SELECT id, value, image_url FROM properties.values WHERE id = ?", id).Scan(&value.Id, &value.Value, &value.ImageURL) if err != nil { return models.Value{}, err } @@ -32,7 +32,7 @@ func (r *ValueRepository) GetValueByID(id int) (models.Value, error) { } func (r *ValueRepository) GetValuesByPropertyId(pid int) ([]models.Value, error) { - rows, err := r.db.Query("SELECT id, value, image_url, properties_id FROM values WHERE properties_id = ?", pid) + rows, err := r.db.Query("SELECT id, value, image_url, properties_id FROM properties.values WHERE properties_id = ?", pid) if err != nil { return nil, err } @@ -59,7 +59,7 @@ func (r *ValueRepository) GetValuesByPropertyId(pid int) ([]models.Value, error) } func (r *ValueRepository) CreateValue(value models.Value) (models.Value, error) { - result, err := r.db.Exec("INSERT INTO values (value, image_url, properties_id) VALUES (?, ?, ?)", value.Value, value.ImageURL, value.PropertiesId) + result, err := r.db.Exec("INSERT INTO properties.values (value, image_url, properties_id) VALUES (?, ?, ?)", value.Value, value.ImageURL, value.PropertiesId) if err != nil { return models.Value{}, err } diff --git a/buildtime/templates/pages/index.html b/buildtime/templates/pages/index.html index 2a1eafa..389fcec 100644 --- a/buildtime/templates/pages/index.html +++ b/buildtime/templates/pages/index.html @@ -20,7 +20,7 @@