2025-04-28 12:38:15 -05:00
|
|
|
package customization
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
function "example.com/database/function"
|
|
|
|
group "example.com/database/group"
|
|
|
|
itemtag "example.com/database/itemtag"
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Customization struct {
|
|
|
|
gorm.Model
|
|
|
|
Name string `gorm:"primaryKey uniqueIndex" json:"name"`
|
|
|
|
Functions []function.Function `gorm:"many2many:customization_function_associations" json:"functions"`
|
|
|
|
FlavorText string `json:"flavor_text"`
|
|
|
|
RulesDescription string `json:"rules_description"`
|
|
|
|
PhysrepRequirements string `json:"physrep_requirements"`
|
|
|
|
Tags []itemtag.ItemTag `gorm:"many2many:customization_tag_associations" json:"tags"` // Unique
|
|
|
|
Visibility []group.Group `gorm:"many2many:customization_visibility_associations" json:"visibility"` // Unique
|
|
|
|
}
|
|
|
|
|
2025-04-29 17:54:32 -05:00
|
|
|
func (customization Customization) Create(db *gorm.DB) error {
|
2025-04-28 12:38:15 -05:00
|
|
|
result := db.Create(&customization)
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-04-29 17:54:32 -05:00
|
|
|
func (customization *Customization) getAssociations(db *gorm.DB) {
|
|
|
|
db.Model(&customization).Association("Functions").Find(&customization.Functions)
|
|
|
|
db.Model(&customization).Association("Tags").Find(&customization.Tags)
|
|
|
|
db.Model(&customization).Association("Visibility").Find(&customization.Visibility)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (customization *Customization) Get(db *gorm.DB, inputCustomization string) {
|
|
|
|
db.Where("name = ?", inputCustomization).Take(&customization)
|
|
|
|
customization.getAssociations(db)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (customization Customization) Update(db *gorm.DB) error {
|
|
|
|
var originalCustomization Customization
|
|
|
|
db.Updates(&Customization{
|
|
|
|
FlavorText: customization.FlavorText,
|
|
|
|
RulesDescription: customization.RulesDescription,
|
|
|
|
PhysrepRequirements: customization.PhysrepRequirements,
|
|
|
|
})
|
|
|
|
functionsError := db.Model(&originalCustomization).Association("Functions").Replace(&customization.Functions)
|
|
|
|
if functionsError != nil {
|
|
|
|
return functionsError
|
|
|
|
}
|
|
|
|
tagsError := db.Model(&originalCustomization).Association("Tags").Replace(&customization.Tags)
|
|
|
|
if tagsError != nil {
|
|
|
|
return tagsError
|
|
|
|
}
|
|
|
|
visibilityError := db.Model(&originalCustomization).Association("Visibility").Replace(&customization.Visibility)
|
|
|
|
if visibilityError != nil {
|
|
|
|
return visibilityError
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (customization Customization) Delete(db *gorm.DB) error {
|
|
|
|
result := db.Delete(&customization)
|
2025-04-28 12:38:15 -05:00
|
|
|
if result.Error != nil {
|
2025-04-29 17:54:32 -05:00
|
|
|
return result.Error
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
2025-04-29 17:54:32 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Create(db *gorm.DB, name string, functions []string, flavorText string, rulesDescription string, physrepRequesrements string, itemTags []string, visibility []string) error {
|
|
|
|
return Customization{
|
|
|
|
Name: name,
|
|
|
|
Functions: *function.Get(db, functions),
|
|
|
|
FlavorText: flavorText,
|
|
|
|
RulesDescription: rulesDescription,
|
|
|
|
PhysrepRequirements: physrepRequesrements,
|
|
|
|
Tags: *itemtag.Get(db, itemTags),
|
|
|
|
Visibility: *group.Get(db, visibility),
|
|
|
|
}.Create(db)
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
|
|
|
|
2025-04-29 17:54:32 -05:00
|
|
|
func Get(db *gorm.DB, inputCustomizations []string) *[]Customization {
|
2025-04-28 12:38:15 -05:00
|
|
|
var outputCustomizations []Customization
|
|
|
|
for _, inputCustomization := range inputCustomizations {
|
2025-04-29 17:54:32 -05:00
|
|
|
var outputCustomization Customization
|
|
|
|
outputCustomization.Get(db, inputCustomization)
|
|
|
|
outputCustomizations = append(outputCustomizations, outputCustomization)
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
2025-04-29 17:54:32 -05:00
|
|
|
return &outputCustomizations
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
|
|
|
|
2025-04-29 17:54:32 -05:00
|
|
|
func GetAll(db *gorm.DB) *[]Customization {
|
|
|
|
var outputCustomizationNames []string
|
|
|
|
result := db.Model(&Customization{}).Select("name").Find(&outputCustomizationNames)
|
2025-04-28 12:38:15 -05:00
|
|
|
if result.Error != nil {
|
|
|
|
log.Println(result.Error)
|
|
|
|
}
|
2025-04-29 17:54:32 -05:00
|
|
|
return Get(db, outputCustomizationNames)
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
|
|
|
|
2025-04-29 17:54:32 -05:00
|
|
|
func Update(db *gorm.DB, name string, functions []string, flavorText string, rulesDescription string, physrepRequesrements string, itemTags []string, visibility []string) error {
|
|
|
|
return Customization{
|
|
|
|
Name: name,
|
|
|
|
Functions: *function.Get(db, functions),
|
|
|
|
FlavorText: flavorText,
|
|
|
|
RulesDescription: rulesDescription,
|
|
|
|
PhysrepRequirements: physrepRequesrements,
|
|
|
|
Tags: *itemtag.Get(db, itemTags),
|
|
|
|
Visibility: *group.Get(db, visibility),
|
|
|
|
}.Update(db)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Delete(db *gorm.DB, inputCustomizations []string) {
|
|
|
|
customizations := Get(db, inputCustomizations)
|
|
|
|
for _, customization := range *customizations {
|
|
|
|
customization.Delete(db)
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
|
|
|
}
|