feat: implemented explore command
This commit is contained in:
+31
-4
@@ -15,6 +15,7 @@ type context struct {
|
|||||||
client *pokeapi.Client
|
client *pokeapi.Client
|
||||||
prevOffset int
|
prevOffset int
|
||||||
nextOffset int
|
nextOffset int
|
||||||
|
args []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *context) register(c cliCommand) {
|
func (r *context) register(c cliCommand) {
|
||||||
@@ -30,24 +31,30 @@ func NewContext() *context {
|
|||||||
callback: commandExit,
|
callback: commandExit,
|
||||||
}
|
}
|
||||||
|
|
||||||
var helpCommand = cliCommand{
|
helpCommand := cliCommand{
|
||||||
name: "help",
|
name: "help",
|
||||||
description: "Displays a help message",
|
description: "Displays a help message",
|
||||||
callback: commandHelp,
|
callback: commandHelp,
|
||||||
}
|
}
|
||||||
|
|
||||||
var mapCommand = cliCommand{
|
mapCommand := cliCommand{
|
||||||
name: "map",
|
name: "map",
|
||||||
description: "Displays the names of the next 20 location areas",
|
description: "Displays the names of the next 20 location areas",
|
||||||
callback: commandMap,
|
callback: commandMap,
|
||||||
}
|
}
|
||||||
|
|
||||||
var mapBackCommand = cliCommand{
|
mapBackCommand := cliCommand{
|
||||||
name: "mapb",
|
name: "mapb",
|
||||||
description: "Displays the names of the previous 20 location areas",
|
description: "Displays the names of the previous 20 location areas",
|
||||||
callback: commandMapBack,
|
callback: commandMapBack,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exploreCommand := cliCommand{
|
||||||
|
name: "explore",
|
||||||
|
description: "Displays a list of pokemon in the area",
|
||||||
|
callback: commandExplore,
|
||||||
|
}
|
||||||
|
|
||||||
c := &context{
|
c := &context{
|
||||||
commands: map[string]cliCommand{},
|
commands: map[string]cliCommand{},
|
||||||
client: pokeapi.NewClient(pokecache.NewCache(5 * time.Second)),
|
client: pokeapi.NewClient(pokecache.NewCache(5 * time.Second)),
|
||||||
@@ -59,6 +66,7 @@ func NewContext() *context {
|
|||||||
c.register(helpCommand)
|
c.register(helpCommand)
|
||||||
c.register(mapCommand)
|
c.register(mapCommand)
|
||||||
c.register(mapBackCommand)
|
c.register(mapBackCommand)
|
||||||
|
c.register(exploreCommand)
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
@@ -99,7 +107,7 @@ func commandHelp(c *context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getMaps(c *context, offset int) 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 {
|
if err != nil {
|
||||||
fmt.Println("Unable to get locations")
|
fmt.Println("Unable to get locations")
|
||||||
@@ -138,3 +146,22 @@ func commandMapBack(c *context) error {
|
|||||||
|
|
||||||
return getMaps(c, c.prevOffset)
|
return getMaps(c, c.prevOffset)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func commandExplore(c *context) error {
|
||||||
|
if len(c.args) < 1 {
|
||||||
|
fmt.Println("Please provide area name. i.e. explore <area_name>")
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -160,12 +160,43 @@ type Location struct {
|
|||||||
Url string `json:"url"`
|
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 {
|
type locationsEndpoint struct {
|
||||||
path string
|
path string
|
||||||
client *Client
|
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
|
const limit int = 20
|
||||||
|
|
||||||
var page Page[Location]
|
var page Page[Location]
|
||||||
|
|||||||
Reference in New Issue
Block a user