diff --git a/buildtime/internal/models/propertyType.go b/buildtime/internal/models/propertyType.go index 4368f22..6b0e3ce 100644 --- a/buildtime/internal/models/propertyType.go +++ b/buildtime/internal/models/propertyType.go @@ -1,9 +1,9 @@ package models type PropertyType struct { - Id int `json:"id,omitempty"` - Name string `json:"name"` - Description string `json:"description,omitempty"` - ImageURL string `json:"ImageURL,omitempty"` - PropertyId int `json:"property_id,omitempty"` + Id int `json:"id,omitempty"` + Name string `json:"name"` + Description string `json:"description,omitempty"` + ImageURL string `json:"image_url,omitempty"` + PropertyTypesGroupsId int `json:"property_types_groups_id,omitempty"` } diff --git a/buildtime/internal/repositories/propertyRepository.go b/buildtime/internal/repositories/propertyRepository.go index c50b5ba..01ba48c 100644 --- a/buildtime/internal/repositories/propertyRepository.go +++ b/buildtime/internal/repositories/propertyRepository.go @@ -1,17 +1,156 @@ package repositories import ( + "SimplyCPQ2/internal/models" "database/sql" ) type PropertyRepository struct { - db *sql.DB - vr *ValueRepository + db *sql.DB + vr *ValueRepository + cache map[int]models.Property } func NewPropertyRepository(db *sql.DB, valueRepository *ValueRepository) *PropertyRepository { return &PropertyRepository{ - db: db, - vr: valueRepository, + db: db, + vr: valueRepository, + cache: make(map[int]models.Property), } } + +func (r *PropertyRepository) createProperty(property models.Property) (models.Property, error) { + result, err := r.db.Exec("INSERT INTO properties (name, description, image_url, parent_property_id, property_type_id, product_groups_id, products_id) VALUES (?, ?, ?, ?, ?, ?, ?)", property.Name, property.Description, property.ImageURL, property.ParentPropertyId, property.PropertyTypeId, property.ProductGroupId, property.ProductId) + if err != nil { + return models.Property{}, err + } + id, err := result.LastInsertId() + if err != nil { + return models.Property{}, err + } + property.Id = int(id) + r.cache[property.Id] = property + return property, nil +} + +func (r *PropertyRepository) GetPropertyByID(id int) (models.Property, error) { + if property, ok := r.cache[id]; ok { + return property, nil + } + + var property models.Property + err := r.db.QueryRow("SELECT id, name, description, image_url, parent_property_id, property_type_id, product_groups_id, products_id FROM properties WHERE id = ?", id).Scan(&property.Id, &property.Name, &property.Description, &property.ImageURL, &property.ParentPropertyId, &property.PropertyTypeId, &property.ProductGroupId, &property.ProductId) + if err != nil { + return models.Property{}, err + } + r.cache[property.Id] = property + return property, nil +} + +func (r *PropertyRepository) GetPropertiesByProductGroupID(pid int) ([]models.Property, error) { + rows, err := r.db.Query("SELECT id, name, description, image_url, parent_property_id, property_type_id, product_groups_id, products_id FROM properties WHERE product_groups_id = ?", pid) + if err != nil { + return nil, err + } + defer rows.Close() + + var properties []models.Property + for rows.Next() { + var property models.Property + err := rows.Scan(&property.Id, &property.Name, &property.Description, &property.ImageURL, &property.ParentPropertyId, &property.PropertyTypeId, &property.ProductGroupId, &property.ProductId) + if err != nil { + return nil, err + } + if cachedProperty, ok := r.cache[property.Id]; ok { + properties = append(properties, cachedProperty) + } else { + r.cache[property.Id] = property + properties = append(properties, property) + } + } + if err := rows.Err(); err != nil { + return nil, err + } + return properties, nil +} + +func (r *PropertyRepository) GetPropertiesByProductID(pid int) ([]models.Property, error) { + rows, err := r.db.Query("SELECT id, name, description, image_url, parent_property_id, property_type_id, product_groups_id, products_id FROM properties WHERE products_id = ?", pid) + if err != nil { + return nil, err + } + defer rows.Close() + + var properties []models.Property + for rows.Next() { + var property models.Property + err := rows.Scan(&property.Id, &property.Name, &property.Description, &property.ImageURL, &property.ParentPropertyId, &property.PropertyTypeId, &property.ProductGroupId, &property.ProductId) + if err != nil { + return nil, err + } + if cachedProperty, ok := r.cache[property.Id]; ok { + properties = append(properties, cachedProperty) + } else { + r.cache[property.Id] = property + properties = append(properties, property) + } + } + if err := rows.Err(); err != nil { + return nil, err + } + return properties, nil +} + +func (r *PropertyRepository) GetPropertiesByParentPropertyID(pid int) ([]models.Property, error) { + rows, err := r.db.Query("SELECT id, name, description, image_url, parent_property_id, property_type_id, product_groups_id, products_id FROM properties WHERE parent_property_id = ?", pid) + if err != nil { + return nil, err + } + defer rows.Close() + + var properties []models.Property + for rows.Next() { + var property models.Property + err := rows.Scan(&property.Id, &property.Name, &property.Description, &property.ImageURL, &property.ParentPropertyId, &property.PropertyTypeId, &property.ProductGroupId, &property.ProductId) + if err != nil { + return nil, err + } + if cachedProperty, ok := r.cache[property.Id]; ok { + properties = append(properties, cachedProperty) + } else { + r.cache[property.Id] = property + properties = append(properties, property) + } + } + if err := rows.Err(); err != nil { + return nil, err + } + return properties, nil +} + +func (r *PropertyRepository) GetPropertiesByPropertyTypeID(pid int) ([]models.Property, error) { + rows, err := r.db.Query("SELECT id, name, description, image_url, parent_property_id, property_type_id, product_groups_id, products_id FROM properties WHERE property_type_id = ?", pid) + if err != nil { + return nil, err + } + defer rows.Close() + + var properties []models.Property + for rows.Next() { + var property models.Property + err := rows.Scan(&property.Id, &property.Name, &property.Description, &property.ImageURL, &property.ParentPropertyId, &property.PropertyTypeId, &property.ProductGroupId, &property.ProductId) + if err != nil { + return nil, err + } + if cachedProperty, ok := r.cache[property.Id]; ok { + properties = append(properties, cachedProperty) + } else { + r.cache[property.Id] = property + properties = append(properties, property) + } + } + if err := rows.Err(); err != nil { + return nil, err + } + return properties, nil +} diff --git a/buildtime/internal/repositories/propertyTypeRepository.go b/buildtime/internal/repositories/propertyTypeRepository.go index ea0364f..7c170ad 100644 --- a/buildtime/internal/repositories/propertyTypeRepository.go +++ b/buildtime/internal/repositories/propertyTypeRepository.go @@ -23,5 +23,52 @@ func (r *PropertyTypeRepository) GetPropertyTypeByID(id int) (models.PropertyTyp if propertyType, ok := r.cache[id]; ok { return propertyType, nil } - var propertyType models.PropertyType - err := r.db.QueryRow("SELECT id, name, description, image_url, property_id FROM property_types WHERE id = ?", id).Scan(&propertyType.Id, &propertyType.Name, &propertyType.Description, &propertyType.ImageURL, &propertyType.PropertyId) + var propertyType models.PropertyType + err := r.db.QueryRow("SELECT id, name, description, image_url, property_types_groups_id FROM property_types WHERE id = ?", id).Scan(&propertyType.Id, &propertyType.Name, &propertyType.Description, &propertyType.ImageURL, &propertyType.PropertyTypesGroupsId) + if err != nil { + return models.PropertyType{}, err + } + r.cache[id] = propertyType + return propertyType, nil +} + +func (r *PropertyTypeRepository) GetPropertyTypesByGroupID(pid int) ([]models.PropertyType, error) { + rows, err := r.db.Query("SELECT id, name, description, image_url, property_types_groups_id FROM property_types WHERE property_types_groups_id = ?", pid) + if err != nil { + return nil, err + } + defer rows.Close() + + var propertyTypes []models.PropertyType + for rows.Next() { + var propertyType models.PropertyType + err := rows.Scan(&propertyType.Id, &propertyType.Name, &propertyType.Description, &propertyType.ImageURL, &propertyType.PropertyTypesGroupsId) + if err != nil { + return nil, err + } + if cachedPropertyType, ok := r.cache[propertyType.Id]; ok { + propertyTypes = append(propertyTypes, cachedPropertyType) + } else { + r.cache[propertyType.Id] = propertyType + propertyTypes = append(propertyTypes, propertyType) + } + } + if err := rows.Err(); err != nil { + return nil, err + } + return propertyTypes, nil +} + +func (r *PropertyTypeRepository) createPropertyType(propertyType models.PropertyType) (models.PropertyType, error) { + result, err := r.db.Exec("INSERT INTO property_types (name, description, image_url, property_types_groups_id) VALUES (?, ?, ?, ?)", propertyType.Name, propertyType.Description, propertyType.ImageURL, propertyType.PropertyTypesGroupsId) + if err != nil { + return models.PropertyType{}, err + } + id, err := result.LastInsertId() + if err != nil { + return models.PropertyType{}, err + } + propertyType.Id = int(id) + r.cache[propertyType.Id] = propertyType + return propertyType, nil +} diff --git a/buildtime/internal/repositories/value_repository.go b/buildtime/internal/repositories/valueRepository.go similarity index 83% rename from buildtime/internal/repositories/value_repository.go rename to buildtime/internal/repositories/valueRepository.go index f12b05f..1091ea8 100644 --- a/buildtime/internal/repositories/value_repository.go +++ b/buildtime/internal/repositories/valueRepository.go @@ -32,7 +32,7 @@ func (r *ValueRepository) GetValueByID(id int) (models.Value, error) { } func (r *ValueRepository) GetValuesByPropertyId(pid int) ([]models.Value, error) { - rows, err := r.db.Query("SELECT id, value, image_url FROM values WHERE properties_id = ?", pid) + rows, err := r.db.Query("SELECT id, value, image_url, properties_id FROM values WHERE properties_id = ?", pid) if err != nil { return nil, err } @@ -41,7 +41,7 @@ func (r *ValueRepository) GetValuesByPropertyId(pid int) ([]models.Value, error) var values []models.Value for rows.Next() { var value models.Value - err := rows.Scan(&value.Id, &value.Value, &value.ImageURL) + err := rows.Scan(&value.Id, &value.Value, &value.ImageURL, &value.PropertiesId) if err != nil { return nil, err } @@ -58,8 +58,8 @@ func (r *ValueRepository) GetValuesByPropertyId(pid int) ([]models.Value, error) return values, nil } -func (r *ValueRepository) CreateValue(value models.Value, pid int) (models.Value, error) { - result, err := r.db.Exec("INSERT INTO values (value, image_url, properties_id) VALUES (?, ?, ?)", value.Value, value.ImageURL, pid) +func (r *ValueRepository) CreateValue(value models.Value) (models.Value, error) { + result, err := r.db.Exec("INSERT INTO values (value, image_url, properties_id) VALUES (?, ?, ?)", value.Value, value.ImageURL, value.PropertiesId) if err != nil { return models.Value{}, err }