Implemented first pass of character and inventory services
This commit is contained in:
@ -3,43 +3,82 @@ package characterapi
|
||||
import (
|
||||
"context"
|
||||
character "crossnokaye-interview-assignment/services/character/gen/character"
|
||||
"errors"
|
||||
"log"
|
||||
)
|
||||
|
||||
// character service example implementation.
|
||||
// The example methods log the requests and return zero values.
|
||||
type charactersrvc struct {
|
||||
logger *log.Logger
|
||||
logger *log.Logger
|
||||
characters map[int]*character.Character
|
||||
}
|
||||
|
||||
// NewCharacter returns the character service implementation.
|
||||
func NewCharacter(logger *log.Logger) character.Service {
|
||||
return &charactersrvc{logger}
|
||||
characterMap := make(map[int]*character.Character)
|
||||
return &charactersrvc{logger, characterMap}
|
||||
}
|
||||
|
||||
// GetCharacter implements getCharacter.
|
||||
func (s *charactersrvc) GetCharacter(ctx context.Context, p int) (res *character.Character, err error) {
|
||||
res = &character.Character{}
|
||||
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]
|
||||
if itemToGet == nil {
|
||||
s.logger.Printf("character with id %d not found", &p.ID)
|
||||
return nil, errors.New("character not found")
|
||||
}
|
||||
res = s.characters[*p.ID]
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// CreateCharacter implements createCharacter.
|
||||
func (s *charactersrvc) CreateCharacter(ctx context.Context, p *character.Character) (res *character.Character, err error) {
|
||||
res = &character.Character{}
|
||||
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]
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateCharacter implements updateCharacter.
|
||||
func (s *charactersrvc) UpdateCharacter(ctx context.Context, p *character.Character) (res *character.Character, err error) {
|
||||
res = &character.Character{}
|
||||
s.logger.Print("character.updateCharacter")
|
||||
|
||||
itemToUpdate := s.characters[*p.ID]
|
||||
if itemToUpdate == nil {
|
||||
s.logger.Printf("characters with id %d not found", &p.ID)
|
||||
return nil, errors.New("characters not found")
|
||||
}
|
||||
s.characters[*p.ID] = p
|
||||
res = s.characters[*p.ID]
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteCharacter implements deleteCharacter.
|
||||
func (s *charactersrvc) DeleteCharacter(ctx context.Context, p int) (err error) {
|
||||
func (s *charactersrvc) DeleteCharacter(ctx context.Context, p *character.DeleteCharacterPayload) (err error) {
|
||||
s.logger.Print("character.deleteCharacter")
|
||||
|
||||
itemToDelete := s.characters[*p.ID]
|
||||
if itemToDelete == nil {
|
||||
s.logger.Printf("characters with id %d not found", &p.ID)
|
||||
return errors.New("characters not found")
|
||||
}
|
||||
|
||||
delete(s.characters, *p.ID)
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -9,17 +9,19 @@ import (
|
||||
var _ = API("character", func() {
|
||||
Title("Character Srvice")
|
||||
Server("character", func() {
|
||||
Host("localhost", func() {
|
||||
URI("grpc://localhost:8083")
|
||||
})
|
||||
})
|
||||
Host("localhost", func() {
|
||||
URI("grpc://localhost:8083")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
var _ = Service("character", func() {
|
||||
Description("A GRPC back service that handles CRUD operations for the characters and their attributes")
|
||||
|
||||
Method("getCharacter", func() {
|
||||
Payload(Int)
|
||||
Payload(func() {
|
||||
Field(1, "id", Int)
|
||||
})
|
||||
Result(Character)
|
||||
Error("NotFound")
|
||||
Error("BadRequest")
|
||||
@ -61,7 +63,9 @@ var _ = Service("character", func() {
|
||||
})
|
||||
|
||||
Method("deleteCharacter", func() {
|
||||
Payload(Int)
|
||||
Payload(func() {
|
||||
Field(1, "id", Int)
|
||||
})
|
||||
Result(Empty)
|
||||
Error("NotFound")
|
||||
Error("BadRequest")
|
||||
|
Reference in New Issue
Block a user