Refactor property type repository methods and update references in property types group repository
This commit is contained in:
@@ -3,7 +3,7 @@ package models
|
||||
type Product struct {
|
||||
Id int
|
||||
Name string
|
||||
description string
|
||||
image_url string
|
||||
Description string
|
||||
Image_Url string
|
||||
Properties []Property
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ type Property struct {
|
||||
Id int
|
||||
Name string
|
||||
Description string
|
||||
image_url string
|
||||
Image_Url string
|
||||
Parent_Property *Property
|
||||
Values []Value
|
||||
}
|
||||
|
||||
@@ -4,6 +4,6 @@ type Property_Type struct {
|
||||
Id int
|
||||
Name string
|
||||
Description string
|
||||
image_url string
|
||||
Image_Url string
|
||||
Property Property
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ type Property_Types_Group struct {
|
||||
Id int
|
||||
Name string
|
||||
Description string
|
||||
Image_Url string
|
||||
Property_Types []Property_Type
|
||||
Property_Types_Groups []Property_Types_Group
|
||||
}
|
||||
|
||||
@@ -3,5 +3,5 @@ package models
|
||||
type Value struct {
|
||||
Id int
|
||||
Value string
|
||||
image_url string
|
||||
Image_Url string
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ func (ptr *Property_Type_Repository) UpdatePropertyType(property_type *models.Pr
|
||||
return err
|
||||
}
|
||||
|
||||
func (ptr *Property_Type_Repository) DeletePropertyType(id int) error {
|
||||
func (ptr *Property_Type_Repository) DeletePropertyTypeById(id int) error {
|
||||
// Implement the logic to delete a property type from the database
|
||||
query := `DELETE FROM property_types WHERE id=$1`
|
||||
_, err := ptr.db.Exec(query, id)
|
||||
|
||||
+103
-8
@@ -6,19 +6,30 @@ import (
|
||||
)
|
||||
|
||||
type Property_Types_Group_Repository struct {
|
||||
db *sql.DB
|
||||
db *sql.DB
|
||||
property_Type_Repository *Property_Type_Repository
|
||||
}
|
||||
|
||||
func NewPropertyTypesGroupRepository(db *sql.DB) *Property_Types_Group_Repository {
|
||||
func NewPropertyTypesGroupRepository(db *sql.DB, property_Type_Repository *Property_Type_Repository) *Property_Types_Group_Repository {
|
||||
return &Property_Types_Group_Repository{
|
||||
db: db,
|
||||
db: db,
|
||||
property_Type_Repository: property_Type_Repository,
|
||||
}
|
||||
}
|
||||
|
||||
func (ptrg *Property_Types_Group_Repository) CreatePropertyTypesGroup(property_types_group *models.Property_Types_Group) (*models.Property_Types_Group, error) {
|
||||
func (ptrg *Property_Types_Group_Repository) CreatePropertyTypesGroup(property_types_group *models.Property_Types_Group, parentPropertyTypeGroup *models.Property_Types_Group) (*models.Property_Types_Group, error) {
|
||||
// Implement the logic to create a new property types group in the database
|
||||
query := `INSERT INTO property_types_groups (name, description, image_url) VALUES ($1, $2, $3) RETURNING id`
|
||||
err := ptrg.db.QueryRow(query, property_types_group.Name, property_types_group.Description, property_types_group.image_url).Scan(&property_types_group.Id)
|
||||
var parentId int
|
||||
if parentPropertyTypeGroup != nil {
|
||||
parentId = parentPropertyTypeGroup.Id
|
||||
parentPropertyTypeGroup.Property_Types_Groups = append(parentPropertyTypeGroup.Property_Types_Groups, *property_types_group)
|
||||
} else {
|
||||
parentId = 0 // or any other value that indicates no parent
|
||||
property_types_group.Property_Types_Groups = []models.Property_Types_Group{}
|
||||
property_types_group.Property_Types = []models.Property_Type{}
|
||||
}
|
||||
query := `INSERT INTO property_types_groups (name, description, image_url, parent_property_type_group) VALUES ($1, $2, $3, $4) RETURNING id`
|
||||
err := ptrg.db.QueryRow(query, property_types_group.Name, property_types_group.Description, property_types_group.Image_Url, parentId).Scan(&property_types_group.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -29,17 +40,60 @@ func (ptrg *Property_Types_Group_Repository) GetPropertyTypesGroupById(id int) (
|
||||
// Implement the logic to retrieve a property types group by its ID from the database
|
||||
query := "SELECT id, name, description, image_url FROM property_types_groups WHERE id = $1"
|
||||
property_types_group := &models.Property_Types_Group{}
|
||||
err := ptrg.db.QueryRow(query, id).Scan(&property_types_group.Id, &property_types_group.Name, &property_types_group.Description, &property_types_group.image_url)
|
||||
err := ptrg.db.QueryRow(query, id).Scan(&property_types_group.Id, &property_types_group.Name, &property_types_group.Description, &property_types_group.Image_Url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// get all childs
|
||||
queryChilds := "SELECT id FROM property_types_groups WHERE parent_property_type_group = $1"
|
||||
rows, err := ptrg.db.Query(queryChilds, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
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_group.Property_Types_Groups = append(property_types_group.Property_Types_Groups, *child)
|
||||
}
|
||||
|
||||
// get all property
|
||||
queryProperties := "SELECT id FROM property_types WHERE property_types_group = $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
|
||||
}
|
||||
property, err := ptrg.property_Type_Repository.GetPropertyTypeById(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
property_types_group.Property_Types = append(property_types_group.Property_Types, *property)
|
||||
}
|
||||
|
||||
return property_types_group, nil
|
||||
}
|
||||
|
||||
func (ptrg *Property_Types_Group_Repository) UpdatePropertyTypesGroup(property_types_group *models.Property_Types_Group) error {
|
||||
// Implement the logic to update the label/description of an existing property types group
|
||||
query := `UPDATE property_types_groups SET name=$1, description=$2, image_url=$3 WHERE id=$4`
|
||||
_, err := ptrg.db.Exec(query, property_types_group.Name, property_types_group.Description, property_types_group.image_url, property_types_group.Id)
|
||||
_, err := ptrg.db.Exec(query, property_types_group.Name, property_types_group.Description, property_types_group.Image_Url, property_types_group.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -47,5 +101,46 @@ func (ptrg *Property_Types_Group_Repository) DeletePropertyTypesGroup(id int) er
|
||||
// Implement the logic to delete a property types group from the database
|
||||
query := `DELETE FROM property_types_groups WHERE id=$1`
|
||||
_, err := ptrg.db.Exec(query, id)
|
||||
|
||||
// delete all child property types groups
|
||||
queryChilds := "SELECT id FROM property_types_groups WHERE parent_property_type_group = $1"
|
||||
rows, err := ptrg.db.Query(queryChilds, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
childId := 0
|
||||
err := rows.Scan(&childId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ptrg.DeletePropertyTypesGroup(childId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// delete all property types in this group
|
||||
queryProperties := "SELECT id FROM property_types WHERE property_types_group = $1"
|
||||
rowsProperties, err := ptrg.db.Query(queryProperties, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rowsProperties.Close()
|
||||
|
||||
for rowsProperties.Next() {
|
||||
propertyId := 0
|
||||
err := rowsProperties.Scan(&propertyId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ptrg.property_Type_Repository.DeletePropertyTypeById(propertyId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ func NewValueRepository(db *sql.DB) *Value_Repository {
|
||||
func (vr *Value_Repository) CreateValueForPropertyId(propertyId int, value *models.Value) (*models.Value, error) {
|
||||
// Implement the logic to create a new value for the given propertyId in the database
|
||||
query := `INSERT INTO values (propertyid, value, image_url) VALUES ($1, $2, $3) RETURNING id`
|
||||
err := vr.db.QueryRow(query, propertyId, value.Value, value.image_url).Scan(&value.Id)
|
||||
err := vr.db.QueryRow(query, propertyId, value.Value, value.Image_Url).Scan(&value.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -35,7 +35,7 @@ func (vr *Value_Repository) DeleteValueFromPropertyId(valueId int, propertyId in
|
||||
func (vr *Value_Repository) UpdateValue(value *models.Value) error {
|
||||
// Implement the logic to update the value in the database for the given valueId
|
||||
query := `UPDATE values SET value=$1, image_url=$2 WHERE id=$3`
|
||||
_, err := vr.db.Exec(query, value.Value, value.image_url, value.Id)
|
||||
_, err := vr.db.Exec(query, value.Value, value.Image_Url, value.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ func (vr *Value_Repository) GetValue(id int) (*models.Value, error) {
|
||||
query := "SELECT id, value, image_url FROM values WHERE id = $1"
|
||||
row := vr.db.QueryRow(query, id)
|
||||
value := &models.Value{}
|
||||
err := row.Scan(&value.Id, &value.Value, &value.image_url)
|
||||
err := row.Scan(&value.Id, &value.Value, &value.Image_Url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -70,7 +70,7 @@ func (vr *Value_Repository) GetValuesFromPropertyId(propertyId int) ([]*models.V
|
||||
values := []*models.Value{}
|
||||
for rows.Next() {
|
||||
value := &models.Value{}
|
||||
err := rows.Scan(&value.Id, &value.Value, &value.image_url)
|
||||
err := rows.Scan(&value.Id, &value.Value, &value.Image_Url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user