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 -2
View File
@@ -4,8 +4,8 @@
# password=<databaseUserPassword> dbname=<databaseName> # password=<databaseUserPassword> dbname=<databaseName>
[database] [database]
##connection_string = "host=192.168.178.50 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" ##connection_string = "host=localhost port=5432 user=tsindt password=XD14ko8d dbname=xcpq sslmode=disable"
driver_name = "postgres" driver_name = "postgres"
################################################################ ################################################################
@@ -38,9 +38,11 @@ func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
// service initialisation: // service initialisation:
importService := services.NewImportService(importRepository) importService := services.NewImportService(importRepository)
propertyTypesService := services.NewPropertyTypeService(propertyTypeRepository, propertyTypeGroupRepository)
// handler initialisation: // handler initialisation:
importHandler := handlers.NewImportHandler(importService) importHandler := handlers.NewImportHandler(importService)
propertyTypesHandler := handlers.NewPropertyTypesHandler(propertyTypesService)
// Service API // Service API
router := gin.Default() router := gin.Default()
@@ -50,6 +52,10 @@ func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
router.Static("/static", "../static") router.Static("/static", "../static")
router.GET("/", handlers.Home) router.GET("/", handlers.Home)
router.GET("/properties", handlers.PropertiesHandler) router.GET("/properties", handlers.PropertiesHandler)
// properties
router.GET("/properties/rootnodes", propertyTypesHandler.GetRootPropertyTypesGroups)
/* /*
router.GET("/products", handlers.ProductsHandler) router.GET("/products", handlers.ProductsHandler)
router.GET("/conditions", handlers.ConditionsHandler) router.GET("/conditions", handlers.ConditionsHandler)
@@ -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)
}
@@ -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"`
}
@@ -5,6 +5,9 @@ type Property struct {
Name string `json:"name"` Name string `json:"name"`
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
Image_Url string `json:"image_url,omitempty"` Image_Url string `json:"image_url,omitempty"`
Property_Type *Property_Type `json:"property_type,omitempty"`
Parent_Property *Property `json:"parent_property,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"` Values []Value `json:"values,omitempty"`
} }
@@ -2,6 +2,7 @@ package repositories
import ( import (
"database/sql" "database/sql"
"log"
"xcpq_editor/internal/models" "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 // 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` query := `SELECT id, name, description, parent_property_id, image_url FROM properties.properties WHERE id = $1`
property := &models.Property{} property := &models.Property{}
log.Printf("Repository: Retrieving property with ID %d", id)
var parent_property_id int var parent_property_id int
err := pr.db.QueryRow(query, id).Scan(&property.Id, &property.Name, &property.Description, &parent_property_id, &property.Image_Url) err := pr.db.QueryRow(query, id).Scan(&property.Id, &property.Name, &property.Description, &parent_property_id, &property.Image_Url)
if err != nil { 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) parent_property, err := pr.GetPropertyById(parent_property_id)
if err == nil { if err == nil {
property.Parent_Property = parent_property 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 { } else {
property.Parent_Property = nil property.Parent_Property = nil
@@ -128,3 +131,45 @@ func (pr *Property_Repository) DeleteProperty(id int) error {
_, err = pr.db.Exec(query, id) _, err = pr.db.Exec(query, id)
return err 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 ( import (
"database/sql" "database/sql"
"log"
"xcpq_editor/internal/models" "xcpq_editor/internal/models"
) )
@@ -56,7 +57,12 @@ func (ptr *Property_Type_Repository) GetPropertyTypeById(id int) (*models.Proper
if err != nil { if err != nil {
return nil, err 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 property_type.Property = *property
return property_type, nil return property_type, nil
} }
@@ -2,6 +2,7 @@ package repositories
import ( import (
"database/sql" "database/sql"
"log"
"xcpq_editor/internal/models" "xcpq_editor/internal/models"
) )
@@ -44,9 +45,9 @@ func (ptrg *Property_Types_Group_Repository) GetPropertyTypesGroupById(id int) (
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.Printf("Repository: Retrieved property types group with ID %d", id)
// get all childs // 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) rows, err := ptrg.db.Query(queryChilds, id)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -59,15 +60,18 @@ func (ptrg *Property_Types_Group_Repository) GetPropertyTypesGroupById(id int) (
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.Printf("Repository: Retrieved child property types group with ID %d", id)
child, err := ptrg.GetPropertyTypesGroupById(id) child, err := ptrg.GetPropertyTypesGroupById(id)
if err != nil { if err != nil {
return nil, err 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) 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 // 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) rowsProperties, err := ptrg.db.Query(queryProperties, id)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -80,13 +84,15 @@ func (ptrg *Property_Types_Group_Repository) GetPropertyTypesGroupById(id int) (
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.Printf("Repository: Retrieved property type with ID %d", id)
property, err := ptrg.property_Type_Repository.GetPropertyTypeById(id) property, err := ptrg.property_Type_Repository.GetPropertyTypeById(id)
if err != nil { if err != nil {
return nil, err 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_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 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) _, err := ptrg.db.Exec(query, id)
// delete all child property types groups // 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) rows, err := ptrg.db.Query(queryChilds, id)
if err != nil { if err != nil {
return err return err
@@ -123,7 +129,7 @@ func (ptrg *Property_Types_Group_Repository) DeletePropertyTypesGroup(id int) er
} }
// delete all property types in this group // 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) rowsProperties, err := ptrg.db.Query(queryProperties, id)
if err != nil { if err != nil {
return err return err
@@ -144,3 +150,31 @@ func (ptrg *Property_Types_Group_Repository) DeletePropertyTypesGroup(id int) er
return err 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
}
@@ -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
}
@@ -6,9 +6,16 @@
<div class="editor-container"> <div class="editor-container">
<section class="editor-view"> <section class="editor-view">
<h3>Property Type Groups</h3> <h3>Property Type Groups</h3>
<div class="mt-16"> <ul class="node mt-16" hx-get="/properties/rootnodes" hx-trigger="click" hx-target="#PropertyTypeGroupList" hx-swap="outerHTML">
Keine Daten vorhanden! <details close>
<summary>
X-CPQ-Eigenschaften
</summary>
<div id="PropertyTypeGroupList">
<!-- Property type groups will be dynamically loaded here -->
</div> </div>
</details>
</ul>
<!-- <!--
<ul class="node mt-16"> <ul class="node mt-16">
<details open> <details open>
@@ -1,8 +1,10 @@
{{range .}}
<ul class="node"> <ul class="node">
<details open> <details closed>
<summary> <summary>
Systemeigenschaften {{.Name}}
</summary> </summary>
<!-- Sub nodes --> <!-- Sub nodes -->
</details> </details>
</ul> </ul>
{{end}}