Implementing list endpoints
This commit is contained in:
@ -34,6 +34,13 @@ func (s *charactersrvc) GetCharacter(ctx context.Context, p *character.GetCharac
|
||||
return
|
||||
}
|
||||
|
||||
func (s *charactersrvc) ListCharacters(ctx context.Context) (res []*character.Character, err error) {
|
||||
for _, value := range s.characters {
|
||||
res = append(res, value)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// CreateCharacter implements createCharacter.
|
||||
func (s *charactersrvc) CreateCharacter(ctx context.Context, p *character.Character) (res *character.Character, err error) {
|
||||
s.logger.Print("character.createCharacter")
|
||||
|
@ -33,8 +33,14 @@ var _ = Service("character", func() {
|
||||
})
|
||||
})
|
||||
|
||||
// Method("listCharacters", func() {
|
||||
// })
|
||||
Method("listCharacters", func() {
|
||||
Payload(Empty)
|
||||
Result(ArrayOf(Character))
|
||||
|
||||
GRPC(func() {
|
||||
Response(CodeOK)
|
||||
})
|
||||
})
|
||||
|
||||
Method("createCharacter", func() {
|
||||
Payload(Character)
|
||||
|
@ -32,8 +32,17 @@ var _ = Service("front", func() {
|
||||
})
|
||||
})
|
||||
|
||||
// Method("listItems", func() {
|
||||
// })
|
||||
Method("listItems", func() {
|
||||
Payload(Empty)
|
||||
Result(ArrayOf(Item))
|
||||
|
||||
HTTP(func() {
|
||||
GET("/item")
|
||||
Response(StatusOK)
|
||||
Response(StatusBadRequest)
|
||||
Response(StatusNotFound)
|
||||
})
|
||||
})
|
||||
|
||||
Method("createItem", func() {
|
||||
Payload(Item)
|
||||
@ -89,8 +98,17 @@ var _ = Service("front", func() {
|
||||
})
|
||||
})
|
||||
|
||||
// Method("listCharacters", func() {
|
||||
// })
|
||||
Method("listCharacters", func() {
|
||||
Payload(Empty)
|
||||
Result(ArrayOf(Character))
|
||||
|
||||
HTTP(func() {
|
||||
GET("/character")
|
||||
Response(StatusOK)
|
||||
Response(StatusBadRequest)
|
||||
Response(StatusNotFound)
|
||||
})
|
||||
})
|
||||
|
||||
Method("createCharacter", func() {
|
||||
Payload(Character)
|
||||
@ -160,5 +178,19 @@ var _ = Service("front", func() {
|
||||
})
|
||||
})
|
||||
|
||||
Method("listInventoryItems", func() {
|
||||
Payload(func() {
|
||||
Field(1, "characterId", Int)
|
||||
})
|
||||
Result(ArrayOf(Item))
|
||||
|
||||
HTTP(func() {
|
||||
GET("/character/{characterId}/item")
|
||||
Response(StatusOK)
|
||||
Response(StatusBadRequest)
|
||||
Response(StatusNotFound)
|
||||
})
|
||||
})
|
||||
|
||||
Files("/openapi.json", "./gen/http/openapi.json")
|
||||
})
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
genInventory "crossnokaye-interview-assignment/services/inventory/gen/inventory"
|
||||
genItemClient "crossnokaye-interview-assignment/services/item/gen/grpc/item/client"
|
||||
genItem "crossnokaye-interview-assignment/services/item/gen/item"
|
||||
"github.com/golang/protobuf/ptypes/empty"
|
||||
goa "goa.design/goa/v3/pkg"
|
||||
"google.golang.org/grpc"
|
||||
"log"
|
||||
@ -16,6 +17,7 @@ import (
|
||||
|
||||
type itemClient struct {
|
||||
getItem goa.Endpoint
|
||||
listItems goa.Endpoint
|
||||
createItem goa.Endpoint
|
||||
updateItem goa.Endpoint
|
||||
deleteItem goa.Endpoint
|
||||
@ -23,6 +25,7 @@ type itemClient struct {
|
||||
|
||||
type characterClient struct {
|
||||
getCharacter goa.Endpoint
|
||||
listCharacters goa.Endpoint
|
||||
createCharacter goa.Endpoint
|
||||
updateCharacter goa.Endpoint
|
||||
deleteCharacter goa.Endpoint
|
||||
@ -31,6 +34,7 @@ type characterClient struct {
|
||||
type inventoryClient struct {
|
||||
addItem goa.Endpoint
|
||||
removeItem goa.Endpoint
|
||||
listInventoryItems goa.Endpoint
|
||||
}
|
||||
|
||||
// front service example implementation.
|
||||
@ -53,17 +57,20 @@ func NewFront(logger *log.Logger, itemClientConnection *grpc.ClientConn, charact
|
||||
return &frontsrvc{logger: logger,
|
||||
itemClient: &itemClient{
|
||||
ic.GetItem(),
|
||||
ic.ListItems(),
|
||||
ic.CreateItem(),
|
||||
ic.UpdateItem(),
|
||||
ic.DeleteItem()},
|
||||
characterClient: &characterClient{
|
||||
cc.GetCharacter(),
|
||||
cc.ListCharacters(),
|
||||
cc.CreateCharacter(),
|
||||
cc.UpdateCharacter(),
|
||||
cc.DeleteCharacter()},
|
||||
inventoryClient: &inventoryClient{
|
||||
icc.AddItem(),
|
||||
icc.RemoveItem(),
|
||||
icc.ListInventory(),
|
||||
}}
|
||||
}
|
||||
|
||||
@ -79,6 +86,21 @@ func (s *frontsrvc) GetItem(ctx context.Context, id int) (res *front.Item, err e
|
||||
return
|
||||
}
|
||||
|
||||
func (s *frontsrvc) ListItems(ctx context.Context) (res []*front.Item, err error) {
|
||||
s.logger.Print("front.listItems")
|
||||
listItemsResponse, err := s.itemClient.listItems(ctx, empty.Empty{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
items := listItemsResponse.([]*genItem.Item)
|
||||
for _, itemToReturn := range items {
|
||||
res = append(res, (*front.Item)(itemToReturn))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// CreateItem implements createItem.
|
||||
func (s *frontsrvc) CreateItem(ctx context.Context, p *front.Item) (res *front.Item, err error) {
|
||||
s.logger.Print("front.createItem")
|
||||
@ -127,6 +149,21 @@ func (s *frontsrvc) GetCharacter(ctx context.Context, id int) (res *front.Charac
|
||||
return
|
||||
}
|
||||
|
||||
func (s *frontsrvc) ListCharacters(ctx context.Context) (res []*front.Character, err error) {
|
||||
s.logger.Print("front.listCharacters")
|
||||
listCharactersResponse, err := s.characterClient.listCharacters(ctx, empty.Empty{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
characters := listCharactersResponse.([]*genCharacter.Character)
|
||||
for _, itemToReturn := range characters {
|
||||
res = append(res, (*front.Character)(itemToReturn))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// CreateCharacter implements createCharacter.
|
||||
func (s *frontsrvc) CreateCharacter(ctx context.Context, p *front.Character) (res *front.Character, err error) {
|
||||
s.logger.Print("front.createCharacter")
|
||||
@ -186,3 +223,17 @@ func (s *frontsrvc) RemoveItemFromInventory(ctx context.Context, p *front.Invent
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s *frontsrvc) ListInventoryItems(ctx context.Context, payload *front.ListInventoryItemsPayload) (res []*front.Item, err error) {
|
||||
//listInventoryResponse, err := s.inventoryClient.listInventoryItems(ctx,
|
||||
// &genInventory.ListInventoryPayload{CharacterID: payload.CharacterID})
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
//
|
||||
//itemIds := listInventoryResponse.([]*int)
|
||||
//for _, itemToReturn := range itemIds {
|
||||
// res = append(frontCharacters, (*front.Character)(itemToReturn))
|
||||
//}
|
||||
return
|
||||
}
|
||||
|
@ -16,8 +16,16 @@ var _ = API("inventory", func() {
|
||||
|
||||
var _ = Service("inventory", func() {
|
||||
Description("A GRPC back service that handles CRUD operations for the characters’ inventories")
|
||||
// Method("listItems", func() {
|
||||
// })
|
||||
Method("listInventory", func() {
|
||||
Payload(func() {
|
||||
Field(1, "characterId", Int)
|
||||
})
|
||||
Result(ArrayOf(Int))
|
||||
|
||||
GRPC(func() {
|
||||
Response(CodeOK)
|
||||
})
|
||||
})
|
||||
|
||||
Method("addItem", func() {
|
||||
Payload(design.InventoryRecord)
|
||||
|
@ -56,6 +56,12 @@ func (s *inventorysrvc) RemoveItem(ctx context.Context, p *inventory.InventoryRe
|
||||
return
|
||||
}
|
||||
|
||||
func (s *inventorysrvc) ListInventory(ctx context.Context, payload *inventory.ListInventoryPayload) (res []int, err error) {
|
||||
res = *s.inventories[*payload.CharacterID]
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s *inventorysrvc) initCharacterInventory(characterId *int) (itemList *[]int) {
|
||||
list := make([]int, 0)
|
||||
itemList = &list
|
||||
|
@ -30,8 +30,14 @@ var _ = Service("item", func() {
|
||||
})
|
||||
})
|
||||
|
||||
// Method("listItems", func() {
|
||||
// })
|
||||
Method("listItems", func() {
|
||||
Payload(Empty)
|
||||
Result(ArrayOf(Item))
|
||||
|
||||
GRPC(func() {
|
||||
Response(CodeOK)
|
||||
})
|
||||
})
|
||||
|
||||
Method("createItem", func() {
|
||||
Payload(Item)
|
||||
|
@ -34,6 +34,13 @@ func (s *itemsrvc) GetItem(ctx context.Context, p *item.GetItemPayload) (res *it
|
||||
return
|
||||
}
|
||||
|
||||
func (s *itemsrvc) ListItems(ctx context.Context) (res []*item.Item, err error) {
|
||||
for _, value := range s.items {
|
||||
res = append(res, value)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// CreateItem implements createItem.
|
||||
func (s *itemsrvc) CreateItem(ctx context.Context, p *item.Item) (res *item.Item, err error) {
|
||||
s.logger.Print("item.createItem")
|
||||
|
Reference in New Issue
Block a user