Updating resources according to instructions

This commit is contained in:
Brandon Watson
2023-08-14 21:03:23 -05:00
parent bdb7a39a91
commit a290cd1420
9 changed files with 89 additions and 108 deletions

View File

@ -11,12 +11,12 @@ import (
// The example methods log the requests and return zero values.
type inventorysrvc struct {
logger *log.Logger
inventories map[int]*[]int //key = characterId, value = array of itemIds
inventories map[string]*[]string //key = characterId, value = array of itemIds
}
// NewInventory returns the inventory service implementation.
func NewInventory(logger *log.Logger) inventory.Service {
inventoryMap := make(map[int]*[]int)
inventoryMap := make(map[string]*[]string)
return &inventorysrvc{logger, inventoryMap}
}
@ -24,12 +24,12 @@ func NewInventory(logger *log.Logger) inventory.Service {
func (s *inventorysrvc) AddItem(ctx context.Context, p *inventory.InventoryRecord) (err error) {
s.logger.Print("inventory.addItem")
itemList := s.inventories[*p.CharacterID]
itemList := s.inventories[p.CharacterName]
if itemList == nil {
itemList = s.initCharacterInventory(p.CharacterID)
itemList = s.initCharacterInventory(p.CharacterName)
}
newItemList := append(*itemList, *p.ItemID)
s.inventories[*p.CharacterID] = &newItemList
newItemList := append(*itemList, p.ItemName)
s.inventories[p.CharacterName] = &newItemList
return
}
@ -38,38 +38,38 @@ func (s *inventorysrvc) AddItem(ctx context.Context, p *inventory.InventoryRecor
func (s *inventorysrvc) RemoveItem(ctx context.Context, p *inventory.InventoryRecord) (err error) {
s.logger.Print("inventory.removeItem")
itemList := s.inventories[*p.CharacterID]
itemList := s.inventories[p.CharacterName]
if itemList == nil {
s.logger.Printf("inventory for character with id %d not found", p.CharacterID)
s.logger.Printf("inventory for character with id %d not found", p.CharacterName)
return errors.New("item not found")
}
idx := indexOf(*p.ItemID, *itemList)
idx := indexOf(p.ItemName, *itemList)
if idx != -1 {
newItemList := remove(*itemList, idx)
s.inventories[*p.CharacterID] = &newItemList
s.inventories[p.CharacterName] = &newItemList
} else {
s.logger.Printf("character with id %d does not have item %d in inventory", &p.CharacterID, &p.ItemID)
s.logger.Printf("character with id %d does not have item %d in inventory", p.CharacterName, p.ItemName)
return errors.New("item not found for character")
}
return
}
func (s *inventorysrvc) ListInventory(ctx context.Context, payload *inventory.ListInventoryPayload) (res []int, err error) {
res = *s.inventories[*payload.CharacterID]
func (s *inventorysrvc) ListInventory(ctx context.Context, payload *inventory.ListInventoryPayload) (res []string, err error) {
res = *s.inventories[*payload.CharacterName]
return
}
func (s *inventorysrvc) initCharacterInventory(characterId *int) (itemList *[]int) {
list := make([]int, 0)
func (s *inventorysrvc) initCharacterInventory(characterName string) (itemList *[]string) {
list := make([]string, 0)
itemList = &list
s.inventories[*characterId] = &list
s.inventories[characterName] = itemList
return
}
func indexOf(element int, data []int) int {
func indexOf(element string, data []string) int {
for k, v := range data {
if element == v {
return k
@ -78,7 +78,7 @@ func indexOf(element int, data []int) int {
return -1 //not found.
}
func remove(s []int, i int) []int {
func remove(s []string, i int) []string {
s[i] = s[len(s)-1]
return s[:len(s)-1]
}