Tree steht

This commit is contained in:
2026-09-16 16:32:31 +02:00
parent c755a80b47
commit 62af3f17f2
7 changed files with 152 additions and 43 deletions
@@ -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)
@@ -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)
}
@@ -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
@@ -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 {
@@ -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
}
@@ -1,14 +1,21 @@
{{template "pt_treeNode" .}}
{{define "ptg_treeNode"}}
{{range .}}
<ul class="node"">
<ul class="node" hx-get="/properties/{{.Id}}/childs" hx-trigger="click" hx-target="#node-{{.Id}}" hx-swap="outerHTML">
<details closed>
<summary>
{{.Name}}
</summary>
{{template "ptg_treeNode" .Property_Types_Groups}}
<div id="node-{{.Id}}">Keine Informationen vorhanden
</div>
<!--
{{template "ptg_treeNode" .Property_Types_Groups}}
<ul class="node">
{{range .Property_Types}}
<li>{{.Name}}</li>
<li>{{.Name}}</li>
{{end}}
</ul>
-->
</details>
</ul>
{{end}}
{{end}}
@@ -1,11 +1,12 @@
{{define "ptg_treeNode"}}
{{range .}}
<ul class="node">
{{- range .}}
<ul class="node" hx-get="/properties/{{.Id}}/childs" hx-trigger="click" hx-target="#node-{{.Id}}" hx-swap="outerHTML">
<details closed>
<summary>
{{.Name}}
</summary>
{{template "ptg_treeNode" .Property_Types_Groups}}
<div id="node-{{.Id}}">Keine Informationen vorhanden
</div>
<ul class="node">
{{range .Property_Types}}
<li>{{.Name}}</li>
@@ -13,5 +14,5 @@
</ul>
</details>
</ul>
{{end}}
{{- end}}
{{end}}