Updating resources according to instructions
This commit is contained in:
@ -5,23 +5,24 @@ import (
|
||||
)
|
||||
|
||||
var Item = Type("item", func() {
|
||||
Field(1, "id", Int)
|
||||
Field(2, "name", String)
|
||||
Field(3, "description", String)
|
||||
Field(4, "multiplier", String)
|
||||
Field(5, "type", String)
|
||||
Required("name", "description", "multiplier", "type")
|
||||
Field(1, "name", String)
|
||||
Field(2, "description", String)
|
||||
Field(3, "damage", Int)
|
||||
Field(4, "healing", Int)
|
||||
Field(5, "protection", Int)
|
||||
Required("name", "description", "damage", "healing", "protection")
|
||||
})
|
||||
|
||||
var Character = Type("character", func() {
|
||||
Field(1, "id", Int)
|
||||
Field(2, "name", String)
|
||||
Field(3, "description", String)
|
||||
Field(4, "class", String)
|
||||
Required("name", "description", "class")
|
||||
Field(1, "name", String)
|
||||
Field(2, "description", String)
|
||||
Field(3, "health", Int)
|
||||
Field(4, "experience", Int)
|
||||
Required("name", "description", "health", "experience")
|
||||
})
|
||||
|
||||
var InventoryRecord = Type("inventoryRecord", func() {
|
||||
Field(1, "characterId", Int)
|
||||
Field(2, "itemId", Int)
|
||||
Field(1, "characterName", String)
|
||||
Field(2, "itemName", String)
|
||||
Required("characterName", "itemName")
|
||||
})
|
||||
|
@ -11,12 +11,12 @@ import (
|
||||
// The example methods log the requests and return zero values.
|
||||
type charactersrvc struct {
|
||||
logger *log.Logger
|
||||
characters map[int]*character.Character
|
||||
characters map[string]*character.Character
|
||||
}
|
||||
|
||||
// NewCharacter returns the character service implementation.
|
||||
func NewCharacter(logger *log.Logger) character.Service {
|
||||
characterMap := make(map[int]*character.Character)
|
||||
characterMap := make(map[string]*character.Character)
|
||||
return &charactersrvc{logger, characterMap}
|
||||
}
|
||||
|
||||
@ -24,12 +24,12 @@ func NewCharacter(logger *log.Logger) character.Service {
|
||||
func (s *charactersrvc) GetCharacter(ctx context.Context, p *character.GetCharacterPayload) (res *character.Character, err error) {
|
||||
s.logger.Print("character.getCharacter")
|
||||
|
||||
itemToGet := s.characters[*p.ID]
|
||||
itemToGet := s.characters[*p.Name]
|
||||
if itemToGet == nil {
|
||||
s.logger.Printf("character with id %d not found", &p.ID)
|
||||
s.logger.Printf("character with id %d not found", &p.Name)
|
||||
return nil, errors.New("character not found")
|
||||
}
|
||||
res = s.characters[*p.ID]
|
||||
res = s.characters[*p.Name]
|
||||
|
||||
return
|
||||
}
|
||||
@ -44,19 +44,8 @@ func (s *charactersrvc) ListCharacters(ctx context.Context) (res []*character.Ch
|
||||
// CreateCharacter implements createCharacter.
|
||||
func (s *charactersrvc) CreateCharacter(ctx context.Context, p *character.Character) (res *character.Character, err error) {
|
||||
s.logger.Print("character.createCharacter")
|
||||
id := -1
|
||||
|
||||
// Using this method of assigning IDs means they will
|
||||
// potentially be non-sequential if ids are deleted
|
||||
if p.ID != nil {
|
||||
id = *p.ID
|
||||
} else {
|
||||
id = len(s.characters)
|
||||
}
|
||||
|
||||
p.ID = &id
|
||||
s.characters[id] = p
|
||||
res = s.characters[id]
|
||||
s.characters[p.Name] = p
|
||||
res = s.characters[p.Name]
|
||||
return
|
||||
}
|
||||
|
||||
@ -64,13 +53,13 @@ func (s *charactersrvc) CreateCharacter(ctx context.Context, p *character.Charac
|
||||
func (s *charactersrvc) UpdateCharacter(ctx context.Context, p *character.Character) (res *character.Character, err error) {
|
||||
s.logger.Print("character.updateCharacter")
|
||||
|
||||
itemToUpdate := s.characters[*p.ID]
|
||||
itemToUpdate := s.characters[p.Name]
|
||||
if itemToUpdate == nil {
|
||||
s.logger.Printf("characters with id %d not found", &p.ID)
|
||||
s.logger.Printf("characters with id %d not found", &p.Name)
|
||||
return nil, errors.New("characters not found")
|
||||
}
|
||||
s.characters[*p.ID] = p
|
||||
res = s.characters[*p.ID]
|
||||
s.characters[p.Name] = p
|
||||
res = s.characters[p.Name]
|
||||
|
||||
return
|
||||
}
|
||||
@ -79,13 +68,13 @@ func (s *charactersrvc) UpdateCharacter(ctx context.Context, p *character.Charac
|
||||
func (s *charactersrvc) DeleteCharacter(ctx context.Context, p *character.DeleteCharacterPayload) (err error) {
|
||||
s.logger.Print("character.deleteCharacter")
|
||||
|
||||
itemToDelete := s.characters[*p.ID]
|
||||
itemToDelete := s.characters[*p.Name]
|
||||
if itemToDelete == nil {
|
||||
s.logger.Printf("characters with id %d not found", &p.ID)
|
||||
s.logger.Printf("characters with id %d not found", &p.Name)
|
||||
return errors.New("characters not found")
|
||||
}
|
||||
|
||||
delete(s.characters, *p.ID)
|
||||
delete(s.characters, *p.Name)
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ var _ = Service("character", func() {
|
||||
|
||||
Method("getCharacter", func() {
|
||||
Payload(func() {
|
||||
Field(1, "id", Int)
|
||||
Field(1, "name", String)
|
||||
})
|
||||
Result(Character)
|
||||
Error("NotFound")
|
||||
@ -70,7 +70,7 @@ var _ = Service("character", func() {
|
||||
|
||||
Method("deleteCharacter", func() {
|
||||
Payload(func() {
|
||||
Field(1, "id", Int)
|
||||
Field(1, "name", String)
|
||||
})
|
||||
Result(Empty)
|
||||
Error("NotFound")
|
||||
|
@ -20,12 +20,12 @@ var _ = Service("front", func() {
|
||||
Description("A GRPC back service that handles CRUD operations for the Items that exist and their attributes")
|
||||
|
||||
Method("getItem", func() {
|
||||
Payload(Int)
|
||||
Payload(String)
|
||||
Result(Item)
|
||||
Error("NotFound")
|
||||
|
||||
HTTP(func() {
|
||||
GET("/item/{id}")
|
||||
GET("/item/{name}")
|
||||
Response(StatusOK)
|
||||
Response(StatusBadRequest)
|
||||
Response(StatusNotFound)
|
||||
@ -63,7 +63,7 @@ var _ = Service("front", func() {
|
||||
Error("BadRequest")
|
||||
|
||||
HTTP(func() {
|
||||
PUT("/item/{id}")
|
||||
PUT("/item/{name}")
|
||||
Response(StatusOK)
|
||||
Response(StatusBadRequest)
|
||||
Response(StatusNotFound)
|
||||
@ -71,13 +71,13 @@ var _ = Service("front", func() {
|
||||
})
|
||||
|
||||
Method("deleteItem", func() {
|
||||
Payload(Int)
|
||||
Payload(String)
|
||||
Result(Empty)
|
||||
Error("NotFound")
|
||||
Error("BadRequest")
|
||||
|
||||
HTTP(func() {
|
||||
DELETE("/item/{id}")
|
||||
DELETE("/item/{name}")
|
||||
Response(StatusOK)
|
||||
Response(StatusBadRequest)
|
||||
Response(StatusNotFound)
|
||||
@ -85,13 +85,13 @@ var _ = Service("front", func() {
|
||||
})
|
||||
|
||||
Method("getCharacter", func() {
|
||||
Payload(Int)
|
||||
Payload(String)
|
||||
Result(Character)
|
||||
Error("NotFound")
|
||||
Error("BadRequest")
|
||||
|
||||
HTTP(func() {
|
||||
GET("/character/{id}")
|
||||
GET("/character/{name}")
|
||||
Response(StatusOK)
|
||||
Response(StatusBadRequest)
|
||||
Response(StatusNotFound)
|
||||
@ -131,7 +131,7 @@ var _ = Service("front", func() {
|
||||
Error("BadRequest")
|
||||
|
||||
HTTP(func() {
|
||||
PUT("/character/{id}")
|
||||
PUT("/character/{name}")
|
||||
Response(StatusOK)
|
||||
Response(StatusBadRequest)
|
||||
Response(StatusNotFound)
|
||||
@ -139,13 +139,13 @@ var _ = Service("front", func() {
|
||||
})
|
||||
|
||||
Method("deleteCharacter", func() {
|
||||
Payload(Int)
|
||||
Payload(String)
|
||||
Result(Empty)
|
||||
Error("NotFound")
|
||||
Error("BadRequest")
|
||||
|
||||
HTTP(func() {
|
||||
DELETE("/character/{id}")
|
||||
DELETE("/character/{name}")
|
||||
Response(StatusOK)
|
||||
Response(StatusBadRequest)
|
||||
Response(StatusNotFound)
|
||||
@ -159,7 +159,7 @@ var _ = Service("front", func() {
|
||||
Error("BadRequest")
|
||||
|
||||
HTTP(func() {
|
||||
POST("/character/{characterId}/item")
|
||||
POST("/character/{characterName}/item")
|
||||
Response(StatusOK)
|
||||
Response(StatusBadRequest)
|
||||
})
|
||||
@ -172,7 +172,7 @@ var _ = Service("front", func() {
|
||||
Error("BadRequest")
|
||||
|
||||
HTTP(func() {
|
||||
DELETE("/character/{characterId}/item/{itemId}")
|
||||
DELETE("/character/{characterName}/item/{itemName}")
|
||||
Response(StatusOK)
|
||||
Response(StatusBadRequest)
|
||||
})
|
||||
@ -180,12 +180,12 @@ var _ = Service("front", func() {
|
||||
|
||||
Method("listInventoryItems", func() {
|
||||
Payload(func() {
|
||||
Field(1, "characterId", Int)
|
||||
Field(1, "characterName", String)
|
||||
})
|
||||
Result(ArrayOf(Item))
|
||||
|
||||
HTTP(func() {
|
||||
GET("/character/{characterId}/item")
|
||||
GET("/character/{characterName}/item")
|
||||
Response(StatusOK)
|
||||
Response(StatusBadRequest)
|
||||
Response(StatusNotFound)
|
||||
|
@ -75,9 +75,9 @@ func NewFront(logger *log.Logger, itemClientConnection *grpc.ClientConn, charact
|
||||
}
|
||||
|
||||
// GetItem implements getItem.
|
||||
func (s *frontsrvc) GetItem(ctx context.Context, id int) (res *front.Item, err error) {
|
||||
func (s *frontsrvc) GetItem(ctx context.Context, name string) (res *front.Item, err error) {
|
||||
s.logger.Print("front.getItem")
|
||||
getItemResponse, err := s.itemClient.getItem(ctx, &genItem.GetItemPayload{ID: &id})
|
||||
getItemResponse, err := s.itemClient.getItem(ctx, &genItem.GetItemPayload{Name: &name})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -127,9 +127,9 @@ func (s *frontsrvc) UpdateItem(ctx context.Context, p *front.Item) (res *front.I
|
||||
}
|
||||
|
||||
// DeleteItem implements deleteItem.
|
||||
func (s *frontsrvc) DeleteItem(ctx context.Context, p int) (err error) {
|
||||
func (s *frontsrvc) DeleteItem(ctx context.Context, p string) (err error) {
|
||||
s.logger.Print("front.deleteItem")
|
||||
_, err = s.itemClient.deleteItem(ctx, &genItem.DeleteItemPayload{ID: &p})
|
||||
_, err = s.itemClient.deleteItem(ctx, &genItem.DeleteItemPayload{Name: &p})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -138,9 +138,9 @@ func (s *frontsrvc) DeleteItem(ctx context.Context, p int) (err error) {
|
||||
}
|
||||
|
||||
// GetCharacter implements getCharacter.
|
||||
func (s *frontsrvc) GetCharacter(ctx context.Context, id int) (res *front.Character, err error) {
|
||||
func (s *frontsrvc) GetCharacter(ctx context.Context, name string) (res *front.Character, err error) {
|
||||
s.logger.Print("front.getCharacter")
|
||||
getCharacterResponse, err := s.characterClient.getCharacter(ctx, &genCharacter.GetCharacterPayload{ID: &id})
|
||||
getCharacterResponse, err := s.characterClient.getCharacter(ctx, &genCharacter.GetCharacterPayload{Name: &name})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -191,10 +191,10 @@ func (s *frontsrvc) UpdateCharacter(ctx context.Context, p *front.Character) (re
|
||||
}
|
||||
|
||||
// DeleteCharacter implements deleteCharacter.
|
||||
func (s *frontsrvc) DeleteCharacter(ctx context.Context, p int) (err error) {
|
||||
func (s *frontsrvc) DeleteCharacter(ctx context.Context, name string) (err error) {
|
||||
s.logger.Print("front.deleteCharacter")
|
||||
|
||||
_, err = s.characterClient.deleteCharacter(ctx, &genCharacter.DeleteCharacterPayload{ID: &p})
|
||||
_, err = s.characterClient.deleteCharacter(ctx, &genCharacter.DeleteCharacterPayload{Name: &name})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -18,9 +18,9 @@ var _ = Service("inventory", func() {
|
||||
Description("A GRPC back service that handles CRUD operations for the characters’ inventories")
|
||||
Method("listInventory", func() {
|
||||
Payload(func() {
|
||||
Field(1, "characterId", Int)
|
||||
Field(1, "characterName", String)
|
||||
})
|
||||
Result(ArrayOf(Int))
|
||||
Result(ArrayOf(String))
|
||||
|
||||
GRPC(func() {
|
||||
Response(CodeOK)
|
||||
|
@ -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]
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ var _ = Service("item", func() {
|
||||
|
||||
Method("getItem", func() {
|
||||
Payload(func() {
|
||||
Field(1, "id", Int)
|
||||
Field(1, "name", String)
|
||||
})
|
||||
Result(Item)
|
||||
Error("NotFound")
|
||||
@ -62,7 +62,7 @@ var _ = Service("item", func() {
|
||||
|
||||
Method("deleteItem", func() {
|
||||
Payload(func() {
|
||||
Field(1, "id", Int)
|
||||
Field(1, "name", String)
|
||||
})
|
||||
Result(Empty)
|
||||
Error("NotFound")
|
||||
|
@ -11,12 +11,12 @@ import (
|
||||
// The example methods log the requests and return zero values.
|
||||
type itemsrvc struct {
|
||||
logger *log.Logger
|
||||
items map[int]*item.Item
|
||||
items map[string]*item.Item
|
||||
}
|
||||
|
||||
// NewItem returns the item service implementation.
|
||||
func NewItem(logger *log.Logger) item.Service {
|
||||
itemsMap := make(map[int]*item.Item)
|
||||
itemsMap := make(map[string]*item.Item)
|
||||
return &itemsrvc{logger, itemsMap}
|
||||
}
|
||||
|
||||
@ -24,12 +24,12 @@ func NewItem(logger *log.Logger) item.Service {
|
||||
func (s *itemsrvc) GetItem(ctx context.Context, p *item.GetItemPayload) (res *item.Item, err error) {
|
||||
s.logger.Print("itemToGet.getItem")
|
||||
|
||||
itemToGet := s.items[*p.ID]
|
||||
itemToGet := s.items[*p.Name]
|
||||
if itemToGet == nil {
|
||||
s.logger.Printf("item with id %d not found", &p.ID)
|
||||
s.logger.Printf("item with id %d not found", &p.Name)
|
||||
return nil, errors.New("item not found")
|
||||
}
|
||||
res = s.items[*p.ID]
|
||||
res = s.items[*p.Name]
|
||||
|
||||
return
|
||||
}
|
||||
@ -44,19 +44,10 @@ func (s *itemsrvc) ListItems(ctx context.Context) (res []*item.Item, err error)
|
||||
// CreateItem implements createItem.
|
||||
func (s *itemsrvc) CreateItem(ctx context.Context, p *item.Item) (res *item.Item, err error) {
|
||||
s.logger.Print("item.createItem")
|
||||
id := -1
|
||||
name := p.Name
|
||||
|
||||
// Using this method of assigning IDs means they will
|
||||
// potentially be non-sequential if ids are deleted
|
||||
if p.ID != nil {
|
||||
id = *p.ID
|
||||
} else {
|
||||
id = len(s.items)
|
||||
}
|
||||
|
||||
p.ID = &id
|
||||
s.items[id] = p
|
||||
res = s.items[id]
|
||||
s.items[name] = p
|
||||
res = s.items[name]
|
||||
|
||||
return
|
||||
}
|
||||
@ -64,13 +55,13 @@ func (s *itemsrvc) CreateItem(ctx context.Context, p *item.Item) (res *item.Item
|
||||
// UpdateItem implements updateItem.
|
||||
func (s *itemsrvc) UpdateItem(ctx context.Context, p *item.Item) (res *item.Item, err error) {
|
||||
s.logger.Print("itemToGet.updateItem")
|
||||
itemToUpdate := s.items[*p.ID]
|
||||
itemToUpdate := s.items[p.Name]
|
||||
if itemToUpdate == nil {
|
||||
s.logger.Printf("item with id %d not found", &p.ID)
|
||||
s.logger.Printf("item with id %d not found", &p.Name)
|
||||
return nil, errors.New("item not found")
|
||||
}
|
||||
s.items[*p.ID] = p
|
||||
res = s.items[*p.ID]
|
||||
s.items[p.Name] = p
|
||||
res = s.items[p.Name]
|
||||
|
||||
return
|
||||
}
|
||||
@ -78,13 +69,13 @@ func (s *itemsrvc) UpdateItem(ctx context.Context, p *item.Item) (res *item.Item
|
||||
// DeleteItem implements deleteItem.
|
||||
func (s *itemsrvc) DeleteItem(ctx context.Context, p *item.DeleteItemPayload) (err error) {
|
||||
s.logger.Print("item.deleteItem")
|
||||
itemToDelete := s.items[*p.ID]
|
||||
itemToDelete := s.items[*p.Name]
|
||||
if itemToDelete == nil {
|
||||
s.logger.Printf("item with id %d not found", &p.ID)
|
||||
s.logger.Printf("item with id %d not found", &p.Name)
|
||||
return errors.New("item not found")
|
||||
}
|
||||
|
||||
delete(s.items, *p.ID)
|
||||
delete(s.items, *p.Name)
|
||||
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user