Moved groups to use uint IDs, fixed bug where string IDs were not getting converted to uints.

This commit is contained in:
Ada Werefox 2025-05-08 22:57:56 -07:00
parent 3ac4f3e79c
commit c1d265f1bf
10 changed files with 27 additions and 41 deletions

1
.vscode/launch.json vendored
View file

@ -8,6 +8,7 @@
"name": "Launch API Server",
"type": "go",
"request": "launch",
"host": "0.0.0.0",
"mode": "debug",
"program": "${workspaceFolder}/src/gin-cpularp.go",
"cwd": "${workspaceFolder}/src"

View file

@ -123,7 +123,7 @@ func objectIDStringsToInts(context *gin.Context, objectIDs []string) *[]uint {
var objectIDInts []uint
for _, objectID := range objectIDs {
objectIDInt, err := strconv.Atoi(objectID)
if err != nil {
if err == nil {
objectIDInts = append(objectIDInts, uint(objectIDInt))
} else {
context.AbortWithStatus(http.StatusBadRequest)
@ -363,8 +363,9 @@ func GetObjects(context *gin.Context) {
"persons": person.Get(GlobalDatabase, objectIDs),
})
case "groups":
objectIDInts := objectIDStringsToInts(context, objectIDs)
context.JSON(http.StatusOK, gin.H{
"groups": group.Get(GlobalDatabase, objectIDs),
"groups": group.Get(GlobalDatabase, *objectIDInts),
})
case "characters":
context.JSON(http.StatusOK, gin.H{
@ -495,8 +496,9 @@ func DeleteObject(context *gin.Context) {
//
case "person":
//
case "group":
// result = group.Create(GlobalDatabase, context)
case "groups":
log.Println(*uintObjectIDs, objectIDs)
result = group.Delete(GlobalDatabase, *uintObjectIDs)
case "character":
//
case "role":

View file

@ -70,7 +70,7 @@ func (customization Customization) Delete(db *gorm.DB) error {
return nil
}
func Create(db *gorm.DB, name string, functions []uint, flavorText string, rulesDescription string, physrepRequesrements string, itemTags []string, visibility []string) error {
func Create(db *gorm.DB, name string, functions []uint, flavorText string, rulesDescription string, physrepRequesrements string, itemTags []string, visibility []uint) error {
return Customization{
Name: name,
Functions: *function.Get(db, functions),
@ -101,7 +101,7 @@ func GetAll(db *gorm.DB) *[]Customization {
return Get(db, outputCustomizationNames)
}
func Update(db *gorm.DB, name string, functions []uint, flavorText string, rulesDescription string, physrepRequesrements string, itemTags []string, visibility []string) error {
func Update(db *gorm.DB, name string, functions []uint, flavorText string, rulesDescription string, physrepRequesrements string, itemTags []string, visibility []uint) error {
return Customization{
Name: name,
Functions: *function.Get(db, functions),

View file

@ -40,23 +40,6 @@ func (function *Function) getAssociations(db *gorm.DB) {
}
func (params *functionParams) validate(context *gin.Context) error {
// Id, idOk := context.GetQuery("id")
// if !idOk {
// return errors.New("ID was not included in the request.")
// }
// Tags, tagsOK := context.GetQueryArray("tags")
// Requirements, requirementsOK := context.GetQueryArray("requirements")
// if idOk && tagsOK && requirementsOK {
// params.Id = Id
// if len(Tags) > 0 {
// params.Tags = Tags
// }
// if len(Requirements) > 0 {
// params.Requirements = Requirements
// }
// } else {
// return errors.New("One or more parameters not included in request")
// }
body, err := io.ReadAll(context.Request.Body)
if err != nil {
log.Println(err)

View file

@ -26,7 +26,7 @@ type groupParams struct {
func (params *groupParams) validate(context *gin.Context) error {
ID, IDOk := context.GetQuery("id")
if !IDOk {
return errors.New("ID was not included in the request.")
return errors.New("ID was not included in the request")
}
body, err := io.ReadAll(context.Request.Body)
if err != nil {
@ -34,14 +34,14 @@ func (params *groupParams) validate(context *gin.Context) error {
return err
}
var name groupParams
err = json.Unmarshal(body, &name)
_ = json.Unmarshal(body, &name)
params.ID = ID
params.Name = name.Name
return nil
}
func (group *Group) Get(db *gorm.DB, inputGroup string) {
db.Model(&Group{}).Where("name = ?", inputGroup).Take(&group)
func (group *Group) Get(db *gorm.DB, inputGroup uint) {
db.Model(&Group{}).Where("ID = ?", inputGroup).Take(&group)
}
func (group Group) update(db *gorm.DB) error {
@ -87,7 +87,7 @@ func Create(db *gorm.DB, context *gin.Context) error {
}.Create(db)
}
func Get(db *gorm.DB, inputGroups []string) *[]Group {
func Get(db *gorm.DB, inputGroups []uint) *[]Group {
var outputGroups []Group
for _, inputGroup := range inputGroups {
var outputGroup Group
@ -99,12 +99,12 @@ func Get(db *gorm.DB, inputGroups []string) *[]Group {
func GetAll(db *gorm.DB) *[]Group {
var outputGroups []Group
var outputGroupNames []string
result := db.Model(&Group{}).Select("name").Find(&outputGroupNames)
var outputGroupIDs []uint
result := db.Model(&Group{}).Select("id").Find(&outputGroupIDs)
if result.Error != nil {
log.Println(result.Error)
}
outputGroups = *Get(db, outputGroupNames)
outputGroups = *Get(db, outputGroupIDs)
return &outputGroups
}
@ -124,7 +124,7 @@ func Update(db *gorm.DB, context *gin.Context) error {
}.update(db)
}
func Delete(db *gorm.DB, inputGroups []string) error {
func Delete(db *gorm.DB, inputGroups []uint) error {
groups := Get(db, inputGroups)
for _, group := range *groups {
err := group.delete(db)

View file

@ -73,7 +73,7 @@ func (item Item) Delete(db *gorm.DB) error {
return nil
}
func Create(db *gorm.DB, name string, functions []uint, flavorText string, rulesDescription string, physrepRequirements string, tags []string, customizations []string, visibility []string) error {
func Create(db *gorm.DB, name string, functions []uint, flavorText string, rulesDescription string, physrepRequirements string, tags []string, customizations []string, visibility []uint) error {
return Item{
Name: name,
Functions: *function.Get(db, functions),
@ -105,7 +105,7 @@ func GetAll(db *gorm.DB) *[]Item {
return Get(db, outputItemNames)
}
func Update(db *gorm.DB, name string, functions []uint, flavorText string, rulesDescription string, physrepRequirements string, tags []string, customizations []string, visibility []string) error {
func Update(db *gorm.DB, name string, functions []uint, flavorText string, rulesDescription string, physrepRequirements string, tags []string, customizations []string, visibility []uint) error {
return Item{
Name: name,
Functions: *function.Get(db, functions),

View file

@ -49,7 +49,7 @@ func (person Person) Delete(db *gorm.DB) error {
return nil
}
func Create(db *gorm.DB, name string, groups []string) error {
func Create(db *gorm.DB, name string, groups []uint) error {
return Person{
Name: name,
Groups: *group.Get(db, groups),
@ -75,7 +75,7 @@ func GetAll(db *gorm.DB) *[]Person {
return Get(db, outputPersonNames)
}
func Update(db *gorm.DB, name string, groups []string) error {
func Update(db *gorm.DB, name string, groups []uint) error {
return Person{
Name: name,
Groups: *group.Get(db, groups),

View file

@ -51,7 +51,7 @@ func (role Role) Delete(db *gorm.DB) error {
return nil
}
func Create(db *gorm.DB, name string, tiers []uint, visibility []string) error {
func Create(db *gorm.DB, name string, tiers []uint, visibility []uint) error {
return Role{
Name: name,
Tiers: *tier.Get(db, tiers),
@ -78,7 +78,7 @@ func GetAll(db *gorm.DB) *[]Role {
return Get(db, outputRoleNames)
}
func Update(db *gorm.DB, name string, tiers []uint, visibility []string) error {
func Update(db *gorm.DB, name string, tiers []uint, visibility []uint) error {
return Role{
Name: name,
Tiers: *tier.Get(db, tiers),

View file

@ -73,7 +73,7 @@ func (schematic Schematic) Delete(db *gorm.DB) error {
return nil
}
func Create(db *gorm.DB, material []uint, tools []uint, requirements []uint, timeUnits int64, result uint, visibility []string) error {
func Create(db *gorm.DB, material []uint, tools []uint, requirements []uint, timeUnits int64, result uint, visibility []uint) error {
return Schematic{
Material: *inventoryslot.Get(db, material),
Tools: *inventoryslot.Get(db, tools),
@ -103,7 +103,7 @@ func GetAll(db *gorm.DB) *[]Schematic {
return Get(db, outputSchematics)
}
func Update(db *gorm.DB, material []uint, tools []uint, requirements []uint, timeUnits int64, result uint, visibility []string) error {
func Update(db *gorm.DB, material []uint, tools []uint, requirements []uint, timeUnits int64, result uint, visibility []uint) error {
return Schematic{
Material: *inventoryslot.Get(db, material),
Tools: *inventoryslot.Get(db, tools),

View file

@ -146,7 +146,7 @@ func Exists(db *gorm.DB, id string) bool {
}
func Create(db *gorm.DB, id string, displayName string, username string, avatar string, avatarDecoration string, loginToken string, loggedIn bool) error {
person.Create(db, displayName, []string{})
person.Create(db, displayName, []uint{})
newPerson := (*person.Get(db, []string{displayName}))[0]
newUser := User{
Id: id,