Backend Start
This commit is contained in:
@@ -5,9 +5,9 @@ type Property struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description,omitempty"`
|
||||
ImageURL string `json:"ImageURL,omitempty"`
|
||||
//Property_Type *Property_Type `json:"property_type,omitempty"`
|
||||
Parent_Property *Property `json:"parent_property,omitempty"`
|
||||
Products_Group *ProductGroup `json:"products_group,omitempty"`
|
||||
Product *Product `json:"products,omitempty"`
|
||||
PropertyTypeId int `json:"property_type_id,omitempty"`
|
||||
ParentPropertyId int `json:"parent_property_id,omitempty"`
|
||||
ProductGroupId int `json:"product_group_id,omitempty"`
|
||||
ProductId int `json:"product_id,omitempty"`
|
||||
Values []Value `json:"values,omitempty"`
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package models
|
||||
|
||||
type Property_Type struct {
|
||||
type PropertyType struct {
|
||||
Id int `json:"id,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description,omitempty"`
|
||||
ImageURL string `json:"ImageURL,omitempty"`
|
||||
Property Property `json:"property"`
|
||||
PropertyId int `json:"property_id,omitempty"`
|
||||
}
|
||||
|
||||
@@ -5,6 +5,5 @@ type PropertyTypeGroup struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description,omitempty"`
|
||||
ImageURL string `json:"ImageURL,omitempty"`
|
||||
PropertyTypes []Property_Type `json:"PropertyTypes,omitempty"`
|
||||
PropertyTypeGroups []PropertyTypeGroup `json:"PropertyTypeGroups,omitempty"`
|
||||
ParentPropertyTypeGroupId int `json:"parent_property_type_group_id,omitempty"`
|
||||
}
|
||||
|
||||
@@ -4,4 +4,5 @@ type Value struct {
|
||||
Id int `json:"id,omitempty"`
|
||||
Value string `json:"value"`
|
||||
ImageURL string `json:"ImageURL,omitempty"`
|
||||
PropertiesId int `json:"properties_id,omitempty"`
|
||||
}
|
||||
|
||||
@@ -1,17 +1,48 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"SimplyCPQ2/internal/models"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type PropertyTypeGroupRepository struct {
|
||||
db *sql.DB
|
||||
PropertyTypeRepository *PropertyTypeRepository
|
||||
cache map[int]models.PropertyTypeGroup
|
||||
}
|
||||
|
||||
func NewPropertyTypesGroupRepository(db *sql.DB, PropertyTypeRepository *PropertyTypeRepository) *PropertyTypeGroupRepository {
|
||||
return &PropertyTypeGroupRepository{
|
||||
db: db,
|
||||
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
|
||||
|
||||
import (
|
||||
"SimplyCPQ2/internal/models"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type PropertyTypeRepository struct {
|
||||
db *sql.DB
|
||||
pr *PropertyRepository
|
||||
cache map[int]models.PropertyType
|
||||
}
|
||||
|
||||
func NewPropertyTypeRepository(db *sql.DB, propertyRepository *PropertyRepository) *PropertyTypeRepository {
|
||||
return &PropertyTypeRepository{
|
||||
db: db,
|
||||
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
|
||||
|
||||
import (
|
||||
"SimplyCPQ2/internal/models"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type ValueRepository struct {
|
||||
db *sql.DB
|
||||
cache map[int]models.Value
|
||||
}
|
||||
|
||||
func NewValueRepository(db *sql.DB) *ValueRepository {
|
||||
return &ValueRepository{
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user