First try
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package pokemongotemplates
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetPokemons() (*[]Pokemon, error) {
|
||||
//get pokemon data from api
|
||||
resp, err := http.Get("https://pokeapi.co/api/v2/pokemon/?offset=0&limit=500")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// read api response
|
||||
rowData, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
||||
var pokemonsData PokemonsAPIResponse
|
||||
if err := json.Unmarshal(rowData, &pokemonsData); err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
||||
PokemonSlice := make([]Pokemon, 0)
|
||||
|
||||
for i := range pokemonsData.Results {
|
||||
resp, err := http.Get(pokemonsData.Results[i].URL)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
rowData, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
||||
var pokemonDetails PokemonDetails
|
||||
if err := json.Unmarshal(rowData, &pokemonDetails); err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
||||
var pokeMoves string
|
||||
for x := range pokemonDetails.Moves {
|
||||
if x <= 3 {
|
||||
pokeMoves += pokemonDetails.Moves[x].Move.Name + " "
|
||||
}
|
||||
}
|
||||
var pokeTypes string
|
||||
for z := range pokemonDetails.Types {
|
||||
pokeTypes += pokemonDetails.Types[z].Type.Name + " "
|
||||
}
|
||||
|
||||
PokemonSlice = append(PokemonSlice, Pokemon{
|
||||
Name: pokemonDetails.Name,
|
||||
Weight: pokemonDetails.Weight,
|
||||
Height: pokemonDetails.Height,
|
||||
Moves: pokeMoves,
|
||||
Types: pokeTypes,
|
||||
Image: pokemonDetails.Sprites.Other.OfficialArtwork.FrontDefault,
|
||||
})
|
||||
|
||||
}
|
||||
return &PokemonSlice, nil
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package pokemongotemplates
|
||||
|
||||
type SiteLink struct {
|
||||
Title string
|
||||
Link string
|
||||
}
|
||||
|
||||
var NavigationLinks = []SiteLink{
|
||||
{
|
||||
Title: "500 Pokemons",
|
||||
Link: "/",
|
||||
},
|
||||
{
|
||||
Title: "Choose how many",
|
||||
Link: "/custom",
|
||||
},
|
||||
}
|
||||
|
||||
type Pokemon struct {
|
||||
Name string
|
||||
Weight int
|
||||
Height int
|
||||
Moves string
|
||||
Types string
|
||||
Image string
|
||||
}
|
||||
|
||||
type PageData struct {
|
||||
NavigationLinks []SiteLink
|
||||
Data []Pokemon
|
||||
}
|
||||
|
||||
type PokemonMove struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
type PokemonMoves struct {
|
||||
Move PokemonMove `json:"move"`
|
||||
}
|
||||
|
||||
type PokemonType struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
type PokemonTypes struct {
|
||||
Type PokemonType `json:"type"`
|
||||
}
|
||||
|
||||
type PokemonImage struct {
|
||||
FrontDefault string `json:"front_default"`
|
||||
}
|
||||
type PokemonArtwork struct {
|
||||
OfficialArtwork PokemonImage `json:"official-artwork"`
|
||||
}
|
||||
type PokemonSprites struct {
|
||||
Other PokemonArtwork `json:"other"`
|
||||
}
|
||||
|
||||
type PokemonDetails struct {
|
||||
Name string `json:"name"`
|
||||
Weight int `json:"weight"`
|
||||
Height int `json:"height"`
|
||||
Moves []PokemonMoves `json:"moves"`
|
||||
Types []PokemonTypes `json:"types"`
|
||||
Sprites PokemonSprites `json:"sprites"`
|
||||
}
|
||||
|
||||
type PokemonAPIResult struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
type PokemonsAPIResponse struct {
|
||||
Results []PokemonAPIResult `json:"results"`
|
||||
}
|
||||
Reference in New Issue
Block a user