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

@ -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")

View File

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