Property Import
This commit is contained in:
@@ -1,13 +1,25 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"xcpq_editor/internal/models"
|
||||
"xcpq_editor/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ImportHandler_PropertyTypesGroup(ctx *gin.Context) {
|
||||
type ImportHandler struct {
|
||||
importService *services.Import_Service
|
||||
}
|
||||
|
||||
func NewImportHandler(i *services.Import_Service) *ImportHandler {
|
||||
return &ImportHandler{
|
||||
importService: i,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *ImportHandler) ImportPropertyTypeGroups(ctx *gin.Context) {
|
||||
// Handle the import request here
|
||||
|
||||
var importData []models.Property_Types_Group
|
||||
@@ -16,6 +28,16 @@ func ImportHandler_PropertyTypesGroup(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
for _, item := range importData {
|
||||
log.Println("Importing property types group with ID:", item.Id)
|
||||
log.Println("Anzahl Sub-Groups:", len(item.Property_Types_Groups))
|
||||
err := h.importService.ImportPropertyTypesData(&item, nil)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"message": "Import successful",
|
||||
})
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package models
|
||||
|
||||
type Property struct {
|
||||
Id int `json:"id"`
|
||||
Id int `json:"id,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Image_Url string `json:"image_url,omitempty"`
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package models
|
||||
|
||||
type Property_Type struct {
|
||||
Id int `json:"id"`
|
||||
Id int `json:"id,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Image_Url string `json:"image_url,omitempty"`
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package models
|
||||
|
||||
type Property_Types_Group struct {
|
||||
Id int `json:"id"`
|
||||
Id int `json:"id,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Image_Url string `json:"image_url,omitempty"`
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package models
|
||||
|
||||
type Value struct {
|
||||
Id int `json:"id"`
|
||||
Id int `json:"id,omitempty"`
|
||||
Value string `json:"value"`
|
||||
Image_Url string `json:"image_url,omitempty"`
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package repositories
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
"xcpq_editor/internal/models"
|
||||
)
|
||||
|
||||
@@ -13,7 +14,7 @@ type Import_Repository struct {
|
||||
pr *Property_Repository
|
||||
}
|
||||
|
||||
func NewImport_Repository(db *sql.DB, vr *Value_Repository, pt *Property_Type_Repository, ptg *Property_Types_Group_Repository, pr *Property_Repository) *Import_Repository {
|
||||
func NewImportRepository(db *sql.DB, vr *Value_Repository, pt *Property_Type_Repository, ptg *Property_Types_Group_Repository, pr *Property_Repository) *Import_Repository {
|
||||
return &Import_Repository{
|
||||
db: db,
|
||||
vr: vr,
|
||||
@@ -34,14 +35,17 @@ func (r *Import_Repository) ImportPropertyTypesData(newGroup *models.Property_Ty
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Println("Created new property types group with ID:", group.Id)
|
||||
newGroup.Id = group.Id
|
||||
for _, childPropertyType := range newGroup.Property_Types {
|
||||
log.Println("Creating new property type with ID:", childPropertyType.Id)
|
||||
_, err := r.pt.CreatePropertyType(&childPropertyType, newGroup, &childPropertyType.Property)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, childGroup := range newGroup.Property_Types_Groups {
|
||||
log.Println("Importing child property types group with ID:", childGroup.Id)
|
||||
err := r.ImportPropertyTypesData(&childGroup, newGroup)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -45,8 +45,8 @@ func (pr *Property_Repository) CreateProperty(
|
||||
parent_property_id = 0
|
||||
}
|
||||
|
||||
query := `INSERT INTO properties (name, description, parent_property_id, property_type_id, product_id, image_url) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id`
|
||||
err := pr.db.QueryRow(query, property.Name, property.Description, parent_property_id, property_type_id, product_id, property.image_url).Scan(&property.Id)
|
||||
query := `INSERT INTO properties.properties (name, description, parent_property_id, property_type_id, products_id, image_url) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id`
|
||||
err := pr.db.QueryRow(query, property.Name, property.Description, parent_property_id, property_type_id, product_id, property.Image_Url).Scan(&property.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -70,10 +70,10 @@ func (pr *Property_Repository) CreateProperty(
|
||||
|
||||
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 WHERE id = $1`
|
||||
query := `SELECT id, name, description, parent_property_id, image_url FROM properties.properties WHERE id = $1`
|
||||
property := &models.Property{}
|
||||
var parent_property_id int
|
||||
err := pr.db.QueryRow(query, id).Scan(&property.Id, &property.Name, &property.Description, &parent_property_id, &property.image_url)
|
||||
err := pr.db.QueryRow(query, id).Scan(&property.Id, &property.Name, &property.Description, &parent_property_id, &property.Image_Url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -103,8 +103,8 @@ func (pr *Property_Repository) GetPropertyById(id int) (*models.Property, error)
|
||||
|
||||
func (pr *Property_Repository) UpdateProperty(property *models.Property) error {
|
||||
// Implement the logic to update an existing property in the database
|
||||
query := `UPDATE properties SET name=$1, description=$2, image_url=$3 WHERE id=$4`
|
||||
_, err := pr.db.Exec(query, property.Name, property.Description, property.image_url, property.Id)
|
||||
query := `UPDATE properties.properties SET name=$1, description=$2, image_url=$3 WHERE id=$4`
|
||||
_, err := pr.db.Exec(query, property.Name, property.Description, property.Image_Url, property.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ func (pr *Property_Repository) DeleteProperty(id int) error {
|
||||
return err
|
||||
}
|
||||
|
||||
query := `DELETE FROM properties WHERE id=$1`
|
||||
query := `DELETE FROM properties.properties WHERE id=$1`
|
||||
_, err = pr.db.Exec(query, id)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -29,8 +29,8 @@ func (ptr *Property_Type_Repository) CreatePropertyType(
|
||||
} else {
|
||||
property_type_group_id = 0
|
||||
}
|
||||
query := `INSERT INTO property_types (name, description, property_type_group_id, image_url) VALUES ($1, $2, $3, $4) RETURNING id`
|
||||
err := ptr.db.QueryRow(query, property_type.Name, property_type.Description, property_type_group_id, property_type.image_url).Scan(&property_type.Id)
|
||||
query := `INSERT INTO properties.property_types (name, description, property_types_groups_id, image_url) VALUES ($1, $2, $3, $4) RETURNING id`
|
||||
err := ptr.db.QueryRow(query, property_type.Name, property_type.Description, property_type_group_id, property_type.Image_Url).Scan(&property_type.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -50,9 +50,9 @@ func (ptr *Property_Type_Repository) CreatePropertyType(
|
||||
|
||||
func (ptr *Property_Type_Repository) GetPropertyTypeById(id int) (*models.Property_Type, error) {
|
||||
// Implement the logic to retrieve a property type by its ID from the database
|
||||
query := "SELECT id, name, description, image_url FROM property_types WHERE id = $1"
|
||||
query := "SELECT id, name, description, image_url FROM properties.property_types WHERE id = $1"
|
||||
property_type := &models.Property_Type{}
|
||||
err := ptr.db.QueryRow(query, id).Scan(&property_type.Id, &property_type.Name, &property_type.Description, &property_type.image_url)
|
||||
err := ptr.db.QueryRow(query, id).Scan(&property_type.Id, &property_type.Name, &property_type.Description, &property_type.Image_Url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -63,14 +63,14 @@ func (ptr *Property_Type_Repository) GetPropertyTypeById(id int) (*models.Proper
|
||||
|
||||
func (ptr *Property_Type_Repository) UpdatePropertyType(property_type *models.Property_Type) error {
|
||||
// Implement the logic to update the label/description of an existing property type
|
||||
query := `UPDATE property_types SET name=$1, description=$2, image_url=$3 WHERE id=$4`
|
||||
_, err := ptr.db.Exec(query, property_type.Name, property_type.Description, property_type.image_url, property_type.Id)
|
||||
query := `UPDATE properties.property_types SET name=$1, description=$2, image_url=$3 WHERE id=$4`
|
||||
_, err := ptr.db.Exec(query, property_type.Name, property_type.Description, property_type.Image_Url, property_type.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
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`
|
||||
query := `DELETE FROM properties.property_types WHERE id=$1`
|
||||
_, err := ptr.db.Exec(query, id)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -25,10 +25,10 @@ func (ptrg *Property_Types_Group_Repository) CreatePropertyTypesGroup(property_t
|
||||
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{}
|
||||
//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`
|
||||
query := `INSERT INTO properties.property_types_groups (name, description, image_url, parent_property_type_group_id) 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
|
||||
@@ -38,7 +38,7 @@ func (ptrg *Property_Types_Group_Repository) CreatePropertyTypesGroup(property_t
|
||||
|
||||
func (ptrg *Property_Types_Group_Repository) GetPropertyTypesGroupById(id int) (*models.Property_Types_Group, error) {
|
||||
// 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"
|
||||
query := "SELECT id, name, description, image_url FROM properties.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)
|
||||
if err != nil {
|
||||
@@ -46,7 +46,7 @@ func (ptrg *Property_Types_Group_Repository) GetPropertyTypesGroupById(id int) (
|
||||
}
|
||||
|
||||
// get all childs
|
||||
queryChilds := "SELECT id FROM property_types_groups WHERE parent_property_type_group = $1"
|
||||
queryChilds := "SELECT id FROM properties.property_types_groups WHERE parent_property_types_groups_id = $1"
|
||||
rows, err := ptrg.db.Query(queryChilds, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -92,18 +92,18 @@ func (ptrg *Property_Types_Group_Repository) GetPropertyTypesGroupById(id int) (
|
||||
|
||||
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`
|
||||
query := `UPDATE properties.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)
|
||||
return err
|
||||
}
|
||||
|
||||
func (ptrg *Property_Types_Group_Repository) DeletePropertyTypesGroup(id int) error {
|
||||
// Implement the logic to delete a property types group from the database
|
||||
query := `DELETE FROM property_types_groups WHERE id=$1`
|
||||
query := `DELETE FROM properties.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"
|
||||
queryChilds := "SELECT id FROM properties.property_types_groups WHERE parent_property_types_groups_id = $1"
|
||||
rows, err := ptrg.db.Query(queryChilds, id)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -123,7 +123,7 @@ func (ptrg *Property_Types_Group_Repository) DeletePropertyTypesGroup(id int) er
|
||||
}
|
||||
|
||||
// delete all property types in this group
|
||||
queryProperties := "SELECT id FROM property_types WHERE property_types_group = $1"
|
||||
queryProperties := "SELECT id FROM properties.property_types WHERE property_types_group = $1"
|
||||
rowsProperties, err := ptrg.db.Query(queryProperties, id)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -17,7 +17,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`
|
||||
query := `INSERT INTO properties.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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -27,21 +27,21 @@ func (vr *Value_Repository) CreateValueForPropertyId(propertyId int, value *mode
|
||||
|
||||
func (vr *Value_Repository) DeleteValueFromPropertyId(valueId int, propertyId int) error {
|
||||
// Implement the logic to delete the value from the database for the given valueId and propertyId
|
||||
query := `DELETE FROM values WHERE id=$1 AND propertyid=$2`
|
||||
query := `DELETE FROM properties.values WHERE id=$1 AND propertyid=$2`
|
||||
_, err := vr.db.Exec(query, valueId, propertyId)
|
||||
return err
|
||||
}
|
||||
|
||||
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`
|
||||
query := `UPDATE properties.values SET value=$1, image_url=$2 WHERE id=$3`
|
||||
_, err := vr.db.Exec(query, value.Value, value.Image_Url, value.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (vr *Value_Repository) GetValue(id int) (*models.Value, error) {
|
||||
// Implement the logic to retrieve a value by its ID from the database
|
||||
query := "SELECT id, value, image_url FROM values WHERE id = $1"
|
||||
query := "SELECT id, value, image_url FROM properties.values WHERE id = $1"
|
||||
row := vr.db.QueryRow(query, id)
|
||||
value := &models.Value{}
|
||||
err := row.Scan(&value.Id, &value.Value, &value.Image_Url)
|
||||
@@ -53,14 +53,14 @@ func (vr *Value_Repository) GetValue(id int) (*models.Value, error) {
|
||||
|
||||
func (vr *Value_Repository) DeleteValuesFromPropertyId(propertyId int) error {
|
||||
// Implement the logic to delete all values from the database for the given propertyId
|
||||
query := `DELETE FROM values WHERE propertyid=$1`
|
||||
query := `DELETE FROM properties.values WHERE propertyid=$1`
|
||||
_, err := vr.db.Exec(query, propertyId)
|
||||
return err
|
||||
}
|
||||
|
||||
func (vr *Value_Repository) GetValuesFromPropertyId(propertyId int) ([]*models.Value, error) {
|
||||
// Implement the logic to retrieve all values from the database for the given propertyId
|
||||
query := `SELECT id, value, image_url FROM values WHERE propertyid=$1`
|
||||
query := `SELECT id, value, image_url FROM properties.values WHERE propertyid=$1`
|
||||
rows, err := vr.db.Query(query, propertyId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -9,7 +9,7 @@ type Import_Service struct {
|
||||
importRepository *repositories.Import_Repository
|
||||
}
|
||||
|
||||
func NewImport_Service(importRepository *repositories.Import_Repository) *Import_Service {
|
||||
func NewImportService(importRepository *repositories.Import_Repository) *Import_Service {
|
||||
return &Import_Service{
|
||||
importRepository: importRepository,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user