TreeView aufgebaut

This commit is contained in:
2026-10-04 16:41:46 +02:00
parent 7ae541aaa9
commit 5b9c2a4dae
8 changed files with 50 additions and 45 deletions
@@ -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)
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"})
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
}
+1 -1
View File
@@ -5,5 +5,5 @@ type PropertyType struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
ImageURL string `json:"image_url,omitempty"`
PropertyTypesGroupsId int `json:"property_types_groups_id,omitempty"`
PropertyTypeGroupId int `json:"property_types_groups_id,omitempty"`
}
@@ -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
}
@@ -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
}
@@ -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
}
@@ -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
}
+1 -1
View File
@@ -20,7 +20,7 @@
<nav class="col-2 col-s-12">
<ul>
<li>
<ul class="node" hx-get="/properties/0/childs" hx-trigger="click" hx-target="#node-0" hx-swap="outerHTML">
<ul class="node" hx-get="/properties/0/childs" hx-trigger="click" hx-swap="outerHTML">
<details closed>
<summary>
Eigenschaften
@@ -5,7 +5,7 @@
<summary>
{{.Name}}
</summary>
<!-- Die PropertyTypeGroup Subgruppen-->
{{- range .PropertyTypeGroups}}
<ul class="node" hx-get="/properties/{{.Id}}/childs" hx-trigger="click" hx-swap="outerHTML">
<details closed>
@@ -16,7 +16,6 @@
</ul>
{{- end}}
<!-- die PropertyTypes der PropertyTypeGroup-->
{{- range .PropertyTypes}}
<ul class="node">
<li>{{.Name}}</li>
@@ -24,3 +23,4 @@
{{- end}}
</details>
</ul>
{{end}}