Last active
September 7, 2021 17:35
-
-
Save percybolmer/7bb5ca822a5b46a02d54e2e86ac5ad6f to your computer and use it in GitHub Desktop.
graphql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// InMemoryRepository is a storage for gophers that uses a map to store them | |
type InMemoryRepository struct { | |
// gophers is our super storage for gophers. | |
gophers []Gopher | |
sync.Mutex | |
} | |
// NewMemoryRepository initializes a memory with mock data | |
func NewMemoryRepository() *InMemoryRepository { | |
gophers := []Gopher{ | |
{ | |
ID: "1", | |
Name: "Original Gopher", | |
Hired: true, | |
Profession: "Logo", | |
}, { | |
ID: "2", | |
Name: "Jan", | |
Hired: true, | |
Profession: "The Janitor", | |
}, | |
} | |
return &InMemoryRepository{ | |
gophers: gophers, | |
} | |
} | |
// GetGophers returns all gophers | |
func (imr *InMemoryRepository) GetGophers() ([]Gopher, error) { | |
return imr.gophers, nil | |
} | |
// GetGopher will return a goper by its ID | |
func (imr *InMemoryRepository) GetGopher(id string) (Gopher, error) { | |
for _, gopher := range imr.gophers { | |
if gopher.ID == id { | |
return gopher, nil | |
} | |
} | |
return Gopher{}, errors.New("no such gopher exists") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment