221 lines
6.9 KiB
Go
221 lines
6.9 KiB
Go
package customization
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
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:"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
|
|
}
|
|
|
|
type CustomizationParams struct {
|
|
// ID(s) of the object being modified
|
|
IDArray []uint `json:"id"`
|
|
// New fields
|
|
Name string `json:"name"`
|
|
Functions []uint `json:"functions"`
|
|
FlavorText string `json:"flavor_text"`
|
|
RulesDescription string `json:"rules_description"`
|
|
PhysrepRequirements string `json:"physrep_requirements"`
|
|
Tags []uint `json:"tags"`
|
|
Visibility []uint `json:"visibility"`
|
|
}
|
|
|
|
func (params *CustomizationParams) parse(IDUintArray *[]uint, body *[]byte) error {
|
|
var inputParams CustomizationParams
|
|
params.IDArray = *IDUintArray
|
|
if len(*body) == 0 {
|
|
return nil
|
|
}
|
|
err := json.Unmarshal(*body, &inputParams)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if inputParams.Name == "" {
|
|
return errors.New("input name cannot be empty")
|
|
}
|
|
params.Name = inputParams.Name
|
|
params.Functions = inputParams.Functions
|
|
params.FlavorText = inputParams.FlavorText
|
|
params.RulesDescription = inputParams.RulesDescription
|
|
params.PhysrepRequirements = inputParams.PhysrepRequirements
|
|
params.Tags = inputParams.Tags
|
|
params.Visibility = inputParams.Visibility
|
|
return nil
|
|
}
|
|
|
|
func (customization Customization) create(db *gorm.DB) error {
|
|
result := db.Create(&customization)
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
return nil
|
|
}
|
|
|
|
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 uint) {
|
|
db.Where("id = ?", inputCustomization).Take(&customization)
|
|
customization.getAssociations(db)
|
|
}
|
|
|
|
func (customization Customization) update(db *gorm.DB) error {
|
|
// Get the original Function object
|
|
var originalCustomization Customization
|
|
result := db.Model(&Customization{}).Where("id = ?", customization.Model.ID).Take(&originalCustomization)
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
// Set the static values
|
|
originalCustomization.Name = customization.Name
|
|
// Set the associated values by grabbing them from the database
|
|
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
|
|
}
|
|
result = db.Model(&originalCustomization).Update("name", originalCustomization.Name)
|
|
if result.Error != nil {
|
|
err := errors.New("new name already exists")
|
|
return err
|
|
}
|
|
db.Save(&originalCustomization)
|
|
return nil
|
|
}
|
|
|
|
func (customization Customization) delete(db *gorm.DB) error {
|
|
result := db.Unscoped().Delete(&customization)
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Create(db *gorm.DB, params CustomizationParams) error {
|
|
var newFunctions []function.Function
|
|
if len(params.Functions) > 0 {
|
|
newFunctions = *function.Get(db, params.Functions)
|
|
}
|
|
var newTags []itemtag.ItemTag
|
|
if len(params.Tags) > 0 {
|
|
newTags = *itemtag.Get(db, params.Tags)
|
|
}
|
|
var newVisibility []group.Group
|
|
if len(params.Visibility) > 0 {
|
|
newVisibility = *group.Get(db, params.Visibility)
|
|
}
|
|
return Customization{
|
|
Name: params.Name,
|
|
Functions: newFunctions,
|
|
FlavorText: params.FlavorText,
|
|
RulesDescription: params.RulesDescription,
|
|
PhysrepRequirements: params.PhysrepRequirements,
|
|
Tags: newTags,
|
|
Visibility: newVisibility,
|
|
}.create(db)
|
|
}
|
|
|
|
func Get(db *gorm.DB, inputCustomizations []uint) *[]Customization {
|
|
var outputCustomizations []Customization
|
|
if len(inputCustomizations) < 1 {
|
|
db.Model(&Customization{}).Select("id").Find(&inputCustomizations)
|
|
}
|
|
for _, inputCustomization := range inputCustomizations {
|
|
var outputCustomization Customization
|
|
outputCustomization.get(db, inputCustomization)
|
|
outputCustomizations = append(outputCustomizations, outputCustomization)
|
|
}
|
|
return &outputCustomizations
|
|
}
|
|
|
|
func Update(db *gorm.DB, params CustomizationParams) error {
|
|
var newFunctions []function.Function
|
|
if len(params.Functions) > 0 {
|
|
newFunctions = *function.Get(db, params.Functions)
|
|
}
|
|
var newTags []itemtag.ItemTag
|
|
if len(params.Tags) > 0 {
|
|
newTags = *itemtag.Get(db, params.Tags)
|
|
}
|
|
var newVisibility []group.Group
|
|
if len(params.Visibility) > 0 {
|
|
newVisibility = *group.Get(db, params.Visibility)
|
|
}
|
|
return Customization{
|
|
Name: params.Name,
|
|
Functions: newFunctions,
|
|
FlavorText: params.FlavorText,
|
|
RulesDescription: params.RulesDescription,
|
|
PhysrepRequirements: params.PhysrepRequirements,
|
|
Tags: newTags,
|
|
Visibility: newVisibility,
|
|
}.update(db)
|
|
}
|
|
|
|
func Delete(db *gorm.DB, inputCustomizations []uint) error {
|
|
var customizations []Customization
|
|
// if len(inputCustomizations) < 1 {
|
|
// result := db.Model(&Customization{}).Select("id").Find(&inputCustomizations)
|
|
// if result.Error != nil {
|
|
// return result.Error
|
|
// }
|
|
// }
|
|
for _, inputCustomization := range inputCustomizations {
|
|
var customization Customization
|
|
customization.get(db, inputCustomization)
|
|
customizations = append(customizations, customization)
|
|
}
|
|
for _, customization := range customizations {
|
|
err := customization.delete(db)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func HandleRequest(method string, db *gorm.DB, IDUintArray *[]uint, body *[]byte) (*[]Customization, error) {
|
|
var err error
|
|
var params CustomizationParams
|
|
err = params.parse(IDUintArray, body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var result *[]Customization
|
|
switch method {
|
|
case "GET":
|
|
result = Get(db, params.IDArray)
|
|
case "POST":
|
|
err = Create(db, params)
|
|
case "PUT":
|
|
err = Update(db, params)
|
|
case "DELETE":
|
|
err = Delete(db, params.IDArray)
|
|
}
|
|
return result, err
|
|
}
|