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 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
}