diff --git a/commands.go b/commands.go index 463c9bd..afe5dbb 100644 --- a/commands.go +++ b/commands.go @@ -15,6 +15,7 @@ type context struct { client *pokeapi.Client prevOffset int nextOffset int + args []string } func (r *context) register(c cliCommand) { @@ -30,24 +31,30 @@ func NewContext() *context { callback: commandExit, } - var helpCommand = cliCommand{ + helpCommand := cliCommand{ name: "help", description: "Displays a help message", callback: commandHelp, } - var mapCommand = cliCommand{ + mapCommand := cliCommand{ name: "map", description: "Displays the names of the next 20 location areas", callback: commandMap, } - var mapBackCommand = cliCommand{ + mapBackCommand := cliCommand{ name: "mapb", description: "Displays the names of the previous 20 location areas", callback: commandMapBack, } + exploreCommand := cliCommand{ + name: "explore", + description: "Displays a list of pokemon in the area", + callback: commandExplore, + } + c := &context{ commands: map[string]cliCommand{}, client: pokeapi.NewClient(pokecache.NewCache(5 * time.Second)), @@ -59,6 +66,7 @@ func NewContext() *context { c.register(helpCommand) c.register(mapCommand) c.register(mapBackCommand) + c.register(exploreCommand) return c } @@ -99,7 +107,7 @@ func commandHelp(c *context) error { } func getMaps(c *context, offset int) error { - page, err := c.client.Locations.Get(offset) + page, err := c.client.Locations.List(offset) if err != nil { fmt.Println("Unable to get locations") @@ -138,3 +146,22 @@ func commandMapBack(c *context) error { return getMaps(c, c.prevOffset) } + +func commandExplore(c *context) error { + if len(c.args) < 1 { + fmt.Println("Please provide area name. i.e. explore ") + return nil + } + + area, err := c.client.Locations.Get(c.args[0]) + + if err != nil { + return err + } + + for _, encounter := range area.PokemonEncounters { + fmt.Println(encounter.Pokemon.Name) + } + + return nil +} diff --git a/internal/pokeapi/client.go b/internal/pokeapi/client.go index 1717bca..eb9a9eb 100644 --- a/internal/pokeapi/client.go +++ b/internal/pokeapi/client.go @@ -160,12 +160,43 @@ type Location struct { Url string `json:"url"` } +type Pokemon struct { + Name string `json:"name"` +} + +type PokemonEncounter struct { + Pokemon Pokemon `json:"pokemon"` +} + +type LocationArea struct { + PokemonEncounters []PokemonEncounter `json:"pokemon_encounters"` +} + type locationsEndpoint struct { path string client *Client } -func (le *locationsEndpoint) Get(offset int) (Page[Location], error) { +func (le *locationsEndpoint) Get(name string) (LocationArea, error) { + var area LocationArea + + requestUrl := fmt.Sprintf("%s/%s", le.path, name) + req, err := le.client.NewRequest(http.MethodGet, requestUrl, map[string]string{}, nil) + + if err != nil { + return area, err + } + + err = le.client.doWithJsonResponse(req, &area) + + if err != nil { + return area, err + } + + return area, nil +} + +func (le *locationsEndpoint) List(offset int) (Page[Location], error) { const limit int = 20 var page Page[Location] diff --git a/repl.go b/repl.go index 3628560..50dfea2 100644 --- a/repl.go +++ b/repl.go @@ -58,6 +58,7 @@ func runREPL(s inputScanner, w io.Writer, sanitizer func(string) []string) { continue } + context.args = cleanedInput[1:] err = command.callback(context) if err != nil {