71 lines
2.5 KiB
Go
71 lines
2.5 KiB
Go
![]() |
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
|
||
|
}
|
||
|
|
||
|
func CreateDatabaseCustomization(db *gorm.DB, customization Customization) error {
|
||
|
result := db.Create(&customization)
|
||
|
if result.Error != nil {
|
||
|
return result.Error
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func GetDatabaseCustomization(db *gorm.DB, inputCustomization string) Customization {
|
||
|
var outputCustomization Customization
|
||
|
result := db.Model(&Customization{}).Where("name = ?", inputCustomization).Take(&outputCustomization)
|
||
|
if result.Error != nil {
|
||
|
return Customization{}
|
||
|
}
|
||
|
db.Model(&outputCustomization).Association("Functions").Find(&outputCustomization.Functions)
|
||
|
db.Model(&outputCustomization).Association("Tags").Find(&outputCustomization.Tags)
|
||
|
db.Model(&outputCustomization).Association("Visibility").Find(&outputCustomization.Visibility)
|
||
|
return outputCustomization
|
||
|
}
|
||
|
|
||
|
func GetDatabaseCustomizations(db *gorm.DB, inputCustomizations []string) []Customization {
|
||
|
var outputCustomizations []Customization
|
||
|
for _, inputCustomization := range inputCustomizations {
|
||
|
outputCustomizations = append(outputCustomizations, GetDatabaseCustomization(db, inputCustomization))
|
||
|
}
|
||
|
return outputCustomizations
|
||
|
}
|
||
|
|
||
|
func GetAllDatabaseCustomizations(db *gorm.DB) []Customization {
|
||
|
var outputCustomizations []Customization
|
||
|
result := db.Find(&outputCustomizations)
|
||
|
if result.Error != nil {
|
||
|
log.Println(result.Error)
|
||
|
}
|
||
|
for index, outputCustomization := range outputCustomizations {
|
||
|
outputCustomizations[index] = GetDatabaseCustomization(db, outputCustomization.Name)
|
||
|
}
|
||
|
return outputCustomizations
|
||
|
}
|
||
|
|
||
|
func UpdateDatabaseCustomization(db *gorm.DB, customization Customization) error {
|
||
|
result := db.Save(&customization)
|
||
|
if result.Error != nil {
|
||
|
return result.Error
|
||
|
}
|
||
|
return nil
|
||
|
}
|