2022-08-12 08:59:03 +00:00
|
|
|
package gorm_models
|
|
|
|
|
|
|
|
|
|
import (
|
2022-11-01 06:35:43 +00:00
|
|
|
"errors"
|
2022-08-12 08:59:03 +00:00
|
|
|
"log"
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type AttributeOption struct {
|
2022-11-01 06:35:43 +00:00
|
|
|
ID uint `gorm:"primaryKey"`
|
|
|
|
|
AttributeID uint
|
|
|
|
|
AdminName string
|
|
|
|
|
SortOrder int `sql:"DEFAULT:NULL" gorm:"default:null"`
|
|
|
|
|
Translations []AttributeOptionTranslation
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AttributeOptionTranslation struct {
|
|
|
|
|
ID uint `gorm:"primaryKey"`
|
|
|
|
|
AttributeOptionID uint
|
|
|
|
|
Locale string
|
|
|
|
|
Label string
|
2022-08-12 08:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Attribute struct {
|
|
|
|
|
ID uint `gorm:"primaryKey"`
|
|
|
|
|
Code string
|
|
|
|
|
AttributeOptions []AttributeOption
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AttributeFamily struct {
|
|
|
|
|
ID uint `gorm:"primaryKey"`
|
|
|
|
|
Code string
|
|
|
|
|
Groups []AttributeGroup
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AttributeGroup struct {
|
|
|
|
|
ID uint `gorm:"primaryKey"`
|
|
|
|
|
Name string
|
|
|
|
|
AttributeFamilyID uint
|
|
|
|
|
Attributes []Attribute `gorm:"many2many:attribute_group_mappings;"`
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-03 13:27:46 +00:00
|
|
|
func GetFamilies(db *gorm.DB) ([]AttributeFamily, error) {
|
2022-08-12 08:59:03 +00:00
|
|
|
var families []AttributeFamily
|
|
|
|
|
|
2022-09-03 13:27:46 +00:00
|
|
|
err := db.Model(&AttributeFamily{}).Preload("Groups").Order("id ASC").Find(&families).Error
|
2022-08-12 08:59:03 +00:00
|
|
|
|
2022-09-03 13:27:46 +00:00
|
|
|
return families, err
|
2022-08-12 08:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetGroups(db *gorm.DB) []AttributeGroup {
|
|
|
|
|
var groups []AttributeGroup
|
|
|
|
|
err := db.Model(&AttributeGroup{}).Preload("Family").Find(&groups).Error
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err.Error())
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return groups
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-03 13:27:46 +00:00
|
|
|
func GetAttributes(db *gorm.DB) ([]Attribute, error) {
|
2022-08-12 08:59:03 +00:00
|
|
|
var attributes []Attribute
|
|
|
|
|
|
2022-09-03 13:27:46 +00:00
|
|
|
err := db.Model(&Attribute{}).Find(&attributes).Error
|
2022-08-12 08:59:03 +00:00
|
|
|
|
2022-09-03 13:27:46 +00:00
|
|
|
return attributes, err
|
2022-08-12 08:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetAttributeOption(db *gorm.DB, attrbuteID uint, value string) AttributeOption {
|
|
|
|
|
var option AttributeOption
|
|
|
|
|
|
2022-11-01 08:28:13 +00:00
|
|
|
err := db.Where("attribute_id=? and admin_name=?", attrbuteID, value).First(&option).Error
|
2022-11-01 06:35:43 +00:00
|
|
|
|
|
|
|
|
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
option = AttributeOption{
|
|
|
|
|
AttributeID: attrbuteID,
|
|
|
|
|
AdminName: value,
|
|
|
|
|
SortOrder: 1000,
|
|
|
|
|
Translations: []AttributeOptionTranslation{
|
|
|
|
|
{Locale: "tm", Label: value},
|
|
|
|
|
{Locale: "ru", Label: value},
|
|
|
|
|
{Locale: "tr", Label: value},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := db.Create(&option).Error; err != nil {
|
|
|
|
|
log.Println(err.Error())
|
|
|
|
|
|
|
|
|
|
}
|
2022-08-12 08:59:03 +00:00
|
|
|
}
|
2022-11-01 06:35:43 +00:00
|
|
|
|
2022-08-12 08:59:03 +00:00
|
|
|
return option
|
|
|
|
|
}
|
2022-10-03 06:31:28 +00:00
|
|
|
|
|
|
|
|
func GetAttrOptions(db *gorm.DB, attrbuteID uint) ([]AttributeOption, error) {
|
|
|
|
|
|
|
|
|
|
var options []AttributeOption
|
|
|
|
|
|
|
|
|
|
err := db.Find(&options, AttributeOption{AttributeID: attrbuteID}).Error
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return options, err
|
|
|
|
|
}
|