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
@@ -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"`
}
@@ -1,10 +1,13 @@
package models
type Property struct {
Id int `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Image_Url string `json:"image_url,omitempty"`
Parent_Property *Property `json:"parent_property,omitempty"`
Values []Value `json:"values,omitempty"`
Id int `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Image_Url string `json:"image_url,omitempty"`
Property_Type *Property_Type `json:"property_type,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"`
}
@@ -2,6 +2,7 @@ package repositories
import (
"database/sql"
"log"
"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
query := `SELECT id, name, description, parent_property_id, image_url FROM properties.properties WHERE id = $1`
property := &models.Property{}
log.Printf("Repository: Retrieving property with ID %d", id)
var parent_property_id int
err := pr.db.QueryRow(query, id).Scan(&property.Id, &property.Name, &property.Description, &parent_property_id, &property.Image_Url)
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)
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
@@ -128,3 +131,45 @@ func (pr *Property_Repository) DeleteProperty(id int) error {
_, err = pr.db.Exec(query, id)
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 (
"database/sql"
"log"
"xcpq_editor/internal/models"
)
@@ -56,7 +57,12 @@ func (ptr *Property_Type_Repository) GetPropertyTypeById(id int) (*models.Proper
if err != nil {
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
return property_type, nil
}
@@ -2,6 +2,7 @@ package repositories
import (
"database/sql"
"log"
"xcpq_editor/internal/models"
)
@@ -44,9 +45,9 @@ 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 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)
if err != nil {
return nil, err
@@ -59,15 +60,18 @@ 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)
// 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)
if err != nil {
return nil, err
@@ -80,13 +84,15 @@ func (ptrg *Property_Types_Group_Repository) GetPropertyTypesGroupById(id int) (
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)
}
log.Printf("Repository: Retrieved all property types for property types group with ID %d", property_types_group.Id)
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)
// 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)
if err != nil {
return err
@@ -123,7 +129,7 @@ func (ptrg *Property_Types_Group_Repository) DeletePropertyTypesGroup(id int) er
}
// 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)
if err != nil {
return err
@@ -144,3 +150,31 @@ func (ptrg *Property_Types_Group_Repository) DeletePropertyTypesGroup(id int) er
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
}