Product Datenbank Schnittstelle angefangen

This commit is contained in:
2026-08-10 21:41:24 +02:00
parent 132f0de90b
commit 0b84803f5e
@@ -21,8 +21,8 @@ func NewProductgroupRepository(dbHandler *sql.DB) *ProductgroupRepository {
} }
func (rr ProductgroupRepository) CreateProductgroup(Productgroup *models.Productgroup) (*models.Productgroup, *models.ResponseError) { func (rr ProductgroupRepository) CreateProductgroup(Productgroup *models.Productgroup) (*models.Productgroup, *models.ResponseError) {
query := `INSERT INTO product_groups(label, description) VALUES($1,$2) RETURNING id` 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.Description) rows, err := rr.dbHandler.Query(query, Productgroup.Label, Productgroup.ParentProductGroupID, Productgroup.IsVisible, Productgroup.Path)
if err != nil { if err != nil {
return nil, &models.ResponseError{ return nil, &models.ResponseError{
Message: err.Error(), Message: err.Error(),
@@ -47,10 +47,10 @@ func (rr ProductgroupRepository) CreateProductgroup(Productgroup *models.Product
Status: http.StatusInternalServerError, Status: http.StatusInternalServerError,
} }
} }
PropertygroupId, err := strconv.Atoi(idstr) ProductgroupId, err := strconv.Atoi(idstr)
if err != nil { if err != nil {
return nil, &models.ResponseError{ return nil, &models.ResponseError{
Message: "Propertygroup id not int", Message: "Productgroup id not int",
Status: http.StatusInternalServerError, Status: http.StatusInternalServerError,
} }
} }
@@ -58,7 +58,9 @@ func (rr ProductgroupRepository) CreateProductgroup(Productgroup *models.Product
pg := &models.Productgroup{ pg := &models.Productgroup{
ID: ProductgroupId, ID: ProductgroupId,
Label: Productgroup.Label, Label: Productgroup.Label,
Description: Productgroup.Description, ParentProductGroupID: Productgroup.ParentProductGroupID,
IsVisible: Productgroup.IsVisible,
Path: Productgroup.Path,
} }
rr.m[pg.ID] = pg 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 { func (rr ProductgroupRepository) UpdateProductgroup(Productgroup *models.Productgroup) *models.ResponseError {
query := `UPDATE product_groups SET label=$1, description=$2 WHERE id=$3` 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.Description, Productgroup.ID) res, err := rr.dbHandler.Exec(query, Productgroup.Label, Productgroup.ParentProductGroupID, Productgroup.IsVisible, Productgroup.Path, Productgroup.ID)
if err != nil { if err != nil {
return &models.ResponseError{ return &models.ResponseError{
Message: err.Error(), Message: err.Error(),
@@ -138,7 +140,7 @@ func (rr ProductgroupRepository) GetProductgroup(ProductgroupId int) (*models.Pr
return pg, nil 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) rows, err := rr.dbHandler.Query(query, ProductgroupId)
if err != nil { if err != nil {
return nil, &models.ResponseError{ return nil, &models.ResponseError{
@@ -148,9 +150,10 @@ func (rr ProductgroupRepository) GetProductgroup(ProductgroupId int) (*models.Pr
} }
defer rows.Close() defer rows.Close()
var idstr, label, description string var idstr, label, parentProductGroupIDstr, path string
var isVisible bool
for rows.Next() { for rows.Next() {
err := rows.Scan(&idstr, &label, &description) err := rows.Scan(&idstr, &label, &parentProductGroupIDstr, &isVisible, &path)
if err != nil { if err != nil {
return nil, &models.ResponseError{ return nil, &models.ResponseError{
Message: err.Error(), Message: err.Error(),
@@ -171,17 +174,99 @@ func (rr ProductgroupRepository) GetProductgroup(ProductgroupId int) (*models.Pr
Status: http.StatusInternalServerError, 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{ pg := &models.Productgroup{
ID: id, ID: id,
Label: label, Label: label,
Description: description, 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 rr.m[pg.ID] = pg
return pg, nil 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) { func (rr ProductgroupRepository) GetAllProductgroups() ([]*models.Productgroup, *models.ResponseError) {
var Productgroups []*models.Productgroup var Productgroups []*models.Productgroup
query := `SELECT id FROM product_groups` query := `SELECT id FROM product_groups`