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", "name": "Launch API Server",
"type": "go", "type": "go",
"request": "launch", "request": "launch",
"host": "0.0.0.0",
"mode": "debug", "mode": "debug",
"program": "${workspaceFolder}/src/gin-cpularp.go", "program": "${workspaceFolder}/src/gin-cpularp.go",
"cwd": "${workspaceFolder}/src" "cwd": "${workspaceFolder}/src"

View file

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

View file

@ -70,7 +70,7 @@ func (customization Customization) Delete(db *gorm.DB) error {
return nil 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{ return Customization{
Name: name, Name: name,
Functions: *function.Get(db, functions), Functions: *function.Get(db, functions),
@ -101,7 +101,7 @@ func GetAll(db *gorm.DB) *[]Customization {
return Get(db, outputCustomizationNames) 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{ return Customization{
Name: name, Name: name,
Functions: *function.Get(db, functions), 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 { 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) body, err := io.ReadAll(context.Request.Body)
if err != nil { if err != nil {
log.Println(err) log.Println(err)

View file

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

View file

@ -73,7 +73,7 @@ func (item Item) Delete(db *gorm.DB) error {
return nil 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{ return Item{
Name: name, Name: name,
Functions: *function.Get(db, functions), Functions: *function.Get(db, functions),
@ -105,7 +105,7 @@ func GetAll(db *gorm.DB) *[]Item {
return Get(db, outputItemNames) 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{ return Item{
Name: name, Name: name,
Functions: *function.Get(db, functions), Functions: *function.Get(db, functions),

View file

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

View file

@ -51,7 +51,7 @@ func (role Role) Delete(db *gorm.DB) error {
return nil 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{ return Role{
Name: name, Name: name,
Tiers: *tier.Get(db, tiers), Tiers: *tier.Get(db, tiers),
@ -78,7 +78,7 @@ func GetAll(db *gorm.DB) *[]Role {
return Get(db, outputRoleNames) 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{ return Role{
Name: name, Name: name,
Tiers: *tier.Get(db, tiers), Tiers: *tier.Get(db, tiers),

View file

@ -73,7 +73,7 @@ func (schematic Schematic) Delete(db *gorm.DB) error {
return nil 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{ return Schematic{
Material: *inventoryslot.Get(db, material), Material: *inventoryslot.Get(db, material),
Tools: *inventoryslot.Get(db, tools), Tools: *inventoryslot.Get(db, tools),
@ -103,7 +103,7 @@ func GetAll(db *gorm.DB) *[]Schematic {
return Get(db, outputSchematics) 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{ return Schematic{
Material: *inventoryslot.Get(db, material), Material: *inventoryslot.Get(db, material),
Tools: *inventoryslot.Get(db, tools), 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 { 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] newPerson := (*person.Get(db, []string{displayName}))[0]
newUser := User{ newUser := User{
Id: id, Id: id,