Fixing bug where items were allowed to be deleted when referenced by a character

This commit is contained in:
2023-08-15 19:22:07 -05:00
parent abce265ba5
commit 5b9e100028
4 changed files with 85 additions and 5 deletions

View File

@ -21,9 +21,24 @@ var _ = Service("inventory", func() {
Field(1, "characterName", String)
})
Result(ArrayOf(String))
Error("NotFound")
GRPC(func() {
Response(CodeOK)
Response("NotFound", CodeNotFound)
})
})
Method("listCharactersWithItem", func() {
Payload(func() {
Field(1, "itemName", String)
})
Result(ArrayOf(String))
Error("NotFound")
GRPC(func() {
Response(CodeOK)
Response("NotFound", CodeNotFound)
})
})
@ -46,4 +61,17 @@ var _ = Service("inventory", func() {
Response("NotFound", CodeNotFound)
})
})
Method("removeAll", func() {
Payload(func() {
Field("1", "characterName", String)
})
Result(Empty)
Error("NotFound")
GRPC(func() {
Response(CodeOK)
Response("NotFound", CodeNotFound)
})
})
})

View File

@ -57,15 +57,43 @@ func (s *inventorysrvc) RemoveItem(ctx context.Context, p *inventory.InventoryRe
return
}
func (s *inventorysrvc) RemoveAll(ctx context.Context, payload *inventory.RemoveAllPayload) (err error) {
s.logger.Print("inventory.removeAll")
itemList := (*s.inventories)[*payload.CharacterName]
if itemList == nil {
s.logger.Printf("inventory for character with name '%s' not found", payload.CharacterName)
return inventory.MakeNotFound(fmt.Errorf("inventory for character with name '%s' not found",
*payload.CharacterName))
}
delete(*s.inventories, *payload.CharacterName)
return
}
func (s *inventorysrvc) ListInventory(ctx context.Context, payload *inventory.ListInventoryPayload) (res []string, err error) {
s.logger.Print("inventory.listInventory")
if (*s.inventories)[*payload.CharacterName] == nil {
return nil, nil
s.logger.Printf("inventory for character with name '%s' not found", payload.CharacterName)
return nil, inventory.MakeNotFound(fmt.Errorf("inventory for character with name '%s' not found",
*payload.CharacterName))
}
res = *((*s.inventories)[*payload.CharacterName])
return
}
func (s *inventorysrvc) ListCharactersWithItem(ctx context.Context, payload *inventory.ListCharactersWithItemPayload) (res []string, err error) {
s.logger.Print("inventory.listCharactersWithItem")
for character := range *(s.inventories) {
idxOfItem := indexOf(*payload.ItemName, *(*s.inventories)[character])
if idxOfItem != -1 {
res = append(res, character)
}
}
return
}
func (s *inventorysrvc) initCharacterInventory(characterName string) (itemList *[]string) {
list := make([]string, 0)
itemList = &list