Files
SimplyCPQ2/buildtime/internal/repositories/propertyRepository.go
T
2026-10-01 21:10:08 +02:00

160 lines
5.4 KiB
Go

package repositories
import (
"SimplyCPQ2/internal/models"
"database/sql"
)
type PropertyRepository struct {
db *sql.DB
vr *ValueRepository
cache map[int]models.Property
}
func NewPropertyRepository(db *sql.DB, valueRepository *ValueRepository) *PropertyRepository {
return &PropertyRepository{
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) GetPropertyByPropertyTypeID(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 models.Property{}, 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 models.Property{}, 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 models.Property{}, err
}
if len(properties) == 0 {
return models.Property{}, nil
}
return properties[0], nil
}