Backend Start

This commit is contained in:
2026-09-29 06:23:26 +02:00
parent 6628d8a06c
commit 7bfea738af
7 changed files with 129 additions and 30 deletions
+9 -9
View File
@@ -1,13 +1,13 @@
package models package models
type Property struct { type Property struct {
Id int `json:"id,omitempty"` Id int `json:"id,omitempty"`
Name string `json:"name"` Name string `json:"name"`
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
ImageURL string `json:"ImageURL,omitempty"` ImageURL string `json:"ImageURL,omitempty"`
//Property_Type *Property_Type `json:"property_type,omitempty"` PropertyTypeId int `json:"property_type_id,omitempty"`
Parent_Property *Property `json:"parent_property,omitempty"` ParentPropertyId int `json:"parent_property_id,omitempty"`
Products_Group *ProductGroup `json:"products_group,omitempty"` ProductGroupId int `json:"product_group_id,omitempty"`
Product *Product `json:"products,omitempty"` ProductId int `json:"product_id,omitempty"`
Values []Value `json:"values,omitempty"` Values []Value `json:"values,omitempty"`
} }
+6 -6
View File
@@ -1,9 +1,9 @@
package models package models
type Property_Type struct { type PropertyType struct {
Id int `json:"id,omitempty"` Id int `json:"id,omitempty"`
Name string `json:"name"` Name string `json:"name"`
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
ImageURL string `json:"ImageURL,omitempty"` ImageURL string `json:"ImageURL,omitempty"`
Property Property `json:"property"` PropertyId int `json:"property_id,omitempty"`
} }
@@ -1,10 +1,9 @@
package models package models
type PropertyTypeGroup struct { type PropertyTypeGroup struct {
Id int `json:"id,omitempty"` Id int `json:"id,omitempty"`
Name string `json:"name"` Name string `json:"name"`
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
ImageURL string `json:"ImageURL,omitempty"` ImageURL string `json:"ImageURL,omitempty"`
PropertyTypes []Property_Type `json:"PropertyTypes,omitempty"` ParentPropertyTypeGroupId int `json:"parent_property_type_group_id,omitempty"`
PropertyTypeGroups []PropertyTypeGroup `json:"PropertyTypeGroups,omitempty"`
} }
+4 -3
View File
@@ -1,7 +1,8 @@
package models package models
type Value struct { type Value struct {
Id int `json:"id,omitempty"` Id int `json:"id,omitempty"`
Value string `json:"value"` Value string `json:"value"`
ImageURL string `json:"ImageURL,omitempty"` ImageURL string `json:"ImageURL,omitempty"`
PropertiesId int `json:"properties_id,omitempty"`
} }
@@ -1,17 +1,48 @@
package repositories package repositories
import ( import (
"SimplyCPQ2/internal/models"
"database/sql" "database/sql"
) )
type PropertyTypeGroupRepository struct { type PropertyTypeGroupRepository struct {
db *sql.DB db *sql.DB
PropertyTypeRepository *PropertyTypeRepository PropertyTypeRepository *PropertyTypeRepository
cache map[int]models.PropertyTypeGroup
} }
func NewPropertyTypesGroupRepository(db *sql.DB, PropertyTypeRepository *PropertyTypeRepository) *PropertyTypeGroupRepository { func NewPropertyTypesGroupRepository(db *sql.DB, PropertyTypeRepository *PropertyTypeRepository) *PropertyTypeGroupRepository {
return &PropertyTypeGroupRepository{ return &PropertyTypeGroupRepository{
db: db, db: db,
PropertyTypeRepository: PropertyTypeRepository, PropertyTypeRepository: PropertyTypeRepository,
cache: make(map[int]models.PropertyTypeGroup),
} }
} }
func (r *PropertyTypeGroupRepository) GetPropertyTypeGroupByID(id int) (models.PropertyTypeGroup, error) {
if value, ok := r.cache[id]; ok {
return value, nil
}
var value models.PropertyTypeGroup
err := r.db.QueryRow("SELECT id, name, description, parenz_property_typw_goup_id, image_url FROM property_type_groups WHERE id = ?", id).Scan(&value.Id, &value.Name, &value.Description, &value.ParentPropertyTypeGroupId, &value.ImageURL)
if err != nil {
return models.PropertyTypeGroup{}, err
}
r.cache[id] = value
return value, nil
}
func (r *PropertyTypeGroupRepository) CreatePropertyTypeGroup(value models.PropertyTypeGroup) (models.PropertyTypeGroup, error) {
result, err := r.db.Exec("INSERT INTO property_type_groups (name) VALUES (?)", value.Name)
if err != nil {
return models.PropertyTypeGroup{}, err
}
id, err := result.LastInsertId()
if err != nil {
return models.PropertyTypeGroup{}, err
}
value.Id = int(id)
r.cache[value.Id] = value
return value, nil
}
@@ -1,17 +1,27 @@
package repositories package repositories
import ( import (
"SimplyCPQ2/internal/models"
"database/sql" "database/sql"
) )
type PropertyTypeRepository struct { type PropertyTypeRepository struct {
db *sql.DB db *sql.DB
pr *PropertyRepository pr *PropertyRepository
cache map[int]models.PropertyType
} }
func NewPropertyTypeRepository(db *sql.DB, propertyRepository *PropertyRepository) *PropertyTypeRepository { func NewPropertyTypeRepository(db *sql.DB, propertyRepository *PropertyRepository) *PropertyTypeRepository {
return &PropertyTypeRepository{ return &PropertyTypeRepository{
db: db, db: db,
pr: propertyRepository, pr: propertyRepository,
cache: make(map[int]models.PropertyType),
} }
} }
func (r *PropertyTypeRepository) GetPropertyTypeByID(id int) (models.PropertyType, error) {
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)
@@ -1,15 +1,73 @@
package repositories package repositories
import ( import (
"SimplyCPQ2/internal/models"
"database/sql" "database/sql"
) )
type ValueRepository struct { type ValueRepository struct {
db *sql.DB db *sql.DB
cache map[int]models.Value
} }
func NewValueRepository(db *sql.DB) *ValueRepository { func NewValueRepository(db *sql.DB) *ValueRepository {
return &ValueRepository{ return &ValueRepository{
db: db, db: db,
cache: make(map[int]models.Value),
} }
} }
func (r *ValueRepository) GetValueByID(id int) (models.Value, error) {
if value, ok := r.cache[id]; ok {
return value, nil
}
var value models.Value
err := r.db.QueryRow("SELECT id, value, image_url FROM values WHERE id = ?", id).Scan(&value.Id, &value.Value, &value.ImageURL)
if err != nil {
return models.Value{}, err
}
r.cache[id] = value
return value, nil
}
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)
if err != nil {
return nil, err
}
defer rows.Close()
var values []models.Value
for rows.Next() {
var value models.Value
err := rows.Scan(&value.Id, &value.Value, &value.ImageURL)
if err != nil {
return nil, err
}
if cachedValue, ok := r.cache[value.Id]; ok {
values = append(values, cachedValue)
} else {
r.cache[value.Id] = value
values = append(values, value)
}
}
if err := rows.Err(); err != nil {
return nil, err
}
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)
if err != nil {
return models.Value{}, err
}
id, err := result.LastInsertId()
if err != nil {
return models.Value{}, err
}
value.Id = int(id)
r.cache[value.Id] = value
return value, nil
}