From 0b84803f5ee0f0e5149bee95bddefac287e1c9c4 Mon Sep 17 00:00:00 2001 From: tsindt Date: Mon, 10 Aug 2026 21:41:24 +0200 Subject: [PATCH] Product Datenbank Schnittstelle angefangen --- .../repositories/productgroupRepository.go | 115 +++++++++++++++--- 1 file changed, 100 insertions(+), 15 deletions(-) diff --git a/buildtime/server/src/scpq-editor-server/repositories/productgroupRepository.go b/buildtime/server/src/scpq-editor-server/repositories/productgroupRepository.go index bf2bcefc0..a637e9612 100644 --- a/buildtime/server/src/scpq-editor-server/repositories/productgroupRepository.go +++ b/buildtime/server/src/scpq-editor-server/repositories/productgroupRepository.go @@ -21,8 +21,8 @@ func NewProductgroupRepository(dbHandler *sql.DB) *ProductgroupRepository { } func (rr ProductgroupRepository) CreateProductgroup(Productgroup *models.Productgroup) (*models.Productgroup, *models.ResponseError) { - query := `INSERT INTO product_groups(label, description) VALUES($1,$2) RETURNING id` - rows, err := rr.dbHandler.Query(query, Productgroup.Label, Productgroup.Description) + query := `INSERT INTO product_groups(label, parent_product_group_id, is_visible, path) VALUES($1,$2, $3, $4) RETURNING id` + rows, err := rr.dbHandler.Query(query, Productgroup.Label, Productgroup.ParentProductGroupID, Productgroup.IsVisible, Productgroup.Path) if err != nil { return nil, &models.ResponseError{ Message: err.Error(), @@ -47,18 +47,20 @@ func (rr ProductgroupRepository) CreateProductgroup(Productgroup *models.Product Status: http.StatusInternalServerError, } } - PropertygroupId, err := strconv.Atoi(idstr) + ProductgroupId, err := strconv.Atoi(idstr) if err != nil { return nil, &models.ResponseError{ - Message: "Propertygroup id not int", + Message: "Productgroup id not int", Status: http.StatusInternalServerError, } } pg := &models.Productgroup{ - ID: ProductgroupId, - Label: Productgroup.Label, - Description: Productgroup.Description, + ID: ProductgroupId, + Label: Productgroup.Label, + ParentProductGroupID: Productgroup.ParentProductGroupID, + IsVisible: Productgroup.IsVisible, + Path: Productgroup.Path, } rr.m[pg.ID] = pg @@ -66,8 +68,8 @@ func (rr ProductgroupRepository) CreateProductgroup(Productgroup *models.Product } func (rr ProductgroupRepository) UpdateProductgroup(Productgroup *models.Productgroup) *models.ResponseError { - query := `UPDATE product_groups SET label=$1, description=$2 WHERE id=$3` - res, err := rr.dbHandler.Exec(query, Productgroup.Label, Productgroup.Description, Productgroup.ID) + query := `UPDATE product_groups SET label=$1, parent_product_group_id=$2, is_visible=$3, path=$4 WHERE id=$5` + res, err := rr.dbHandler.Exec(query, Productgroup.Label, Productgroup.ParentProductGroupID, Productgroup.IsVisible, Productgroup.Path, Productgroup.ID) if err != nil { return &models.ResponseError{ Message: err.Error(), @@ -138,7 +140,7 @@ func (rr ProductgroupRepository) GetProductgroup(ProductgroupId int) (*models.Pr return pg, nil } - query := `SELECT id, label, description FROM product_groups WHERE id=$1` + query := `SELECT id, label, parent_product_group_id, is_visible, path FROM product_groups WHERE id=$1` rows, err := rr.dbHandler.Query(query, ProductgroupId) if err != nil { return nil, &models.ResponseError{ @@ -148,9 +150,10 @@ func (rr ProductgroupRepository) GetProductgroup(ProductgroupId int) (*models.Pr } defer rows.Close() - var idstr, label, description string + var idstr, label, parentProductGroupIDstr, path string + var isVisible bool for rows.Next() { - err := rows.Scan(&idstr, &label, &description) + err := rows.Scan(&idstr, &label, &parentProductGroupIDstr, &isVisible, &path) if err != nil { return nil, &models.ResponseError{ Message: err.Error(), @@ -171,17 +174,99 @@ func (rr ProductgroupRepository) GetProductgroup(ProductgroupId int) (*models.Pr Status: http.StatusInternalServerError, } } + parentProductGroupID, err := strconv.Atoi(parentProductGroupIDstr) + if err != nil { + return nil, &models.ResponseError{ + Message: "Parent Productgroup id not int", + Status: http.StatusInternalServerError, + } + } pg := &models.Productgroup{ - ID: id, - Label: label, - Description: description, + ID: id, + Label: label, + ParentProductGroupID: parentProductGroupID, + IsVisible: isVisible, + Path: path, } + // get sub product groups + subProductGroups, err2 := rr.GetSubProductgroups(pg.ID) + if err2 != nil { + return nil, &models.ResponseError{ + Message: err2.Error(), + Status: http.StatusInternalServerError, + } + } + for _, spg := range subProductGroups { + pg.ChildProductGroups = append(pg.ChildProductGroups, *spg) + } + + // get property types of this product group + ptr := NewPropertyTypesRepository{dbHandler: rr.dbHandler} + propertyTypes, err3 := ptr.GetPropertyTypesByProductGroupID(pg.ID) + if err3 != nil { + return nil, &models.ResponseError{ + Message: err3.Error(), + Status: http.StatusInternalServerError, + } + } + pg.PropertyTypes = propertyTypes + rr.m[pg.ID] = pg return pg, nil } +func (rr ProductgroupRepository) GetSubProductgroups(ProductgroupId int) ([]*models.Productgroup, *models.ResponseError) { + var Productgroups []*models.Productgroup + query := `SELECT id FROM product_groups WHERE parent_product_group_id=$1` + + rows, err := rr.dbHandler.Query(query, ProductgroupId) + if err != nil { + return nil, &models.ResponseError{ + Message: err.Error(), + Status: http.StatusInternalServerError, + } + } + + defer rows.Close() + var idstr string + for rows.Next() { + err := rows.Scan(&idstr) + if err != nil { + return nil, &models.ResponseError{ + Message: err.Error(), + Status: http.StatusInternalServerError, + } + } + id, err := strconv.Atoi(idstr) + if err != nil { + return nil, &models.ResponseError{ + Message: "Productgroup id not int " + idstr, + Status: http.StatusInternalServerError, + } + } + pg, err2 := rr.GetProductgroup(id) + if err2 != nil { + return nil, &models.ResponseError{ + Message: "Productgroup not found with the given id " + idstr, + Status: http.StatusInternalServerError, + } + } + if pg != nil { + rr.m[pg.ID] = pg + Productgroups = append(Productgroups, pg) + } + } + if rows.Err() != nil { + return nil, &models.ResponseError{ + Message: rows.Err().Error(), + Status: http.StatusInternalServerError, + } + } + return Productgroups, nil +} + func (rr ProductgroupRepository) GetAllProductgroups() ([]*models.Productgroup, *models.ResponseError) { var Productgroups []*models.Productgroup query := `SELECT id FROM product_groups`