Changed theme, added search, added pagination

This commit is contained in:
2022-11-27 09:27:49 +01:00
parent 7ab57f2ea0
commit bea96468d5
19 changed files with 370 additions and 137 deletions
+33 -18
View File
@@ -5,13 +5,24 @@ import (
"fmt"
"io/ioutil"
"net/http"
"sort"
"sync"
)
func GetPokemons() (map[string]*Pokemon, error) {
//get pokemon data from api
resp, err := http.Get("https://pokeapi.co/api/v2/pokemon/?offset=0&limit=500")
const (
AllPokemonsURL = "https://pokeapi.co/api/v2/pokemon/?offset=0&limit=1154"
PaginationURL = "https://pokeapi.co/api/v2/pokemon?offset=0&limit=20"
)
func GetPokemons(url string) (Pokemons, error) {
//get pokemon data from api
URL := url
if url == "" {
URL = PaginationURL
}
resp, err := http.Get(URL)
if err != nil {
fmt.Println(err.Error())
}
@@ -28,16 +39,7 @@ func GetPokemons() (map[string]*Pokemon, error) {
fmt.Println(err.Error())
}
// PokemonSlice := make([]Pokemon, 0)
// for i := range pokemonsData.Results {
// go func() {
// indPokemon := fetchIndividualPokemons(pokemonsData.Results[i].URL)
// PokemonSlice = append(PokemonSlice, indPokemon)
// }()
// }
PokemonMap := make(map[string]*Pokemon)
pokemonMap := make(map[string]*Pokemon)
pokemonStream := make(chan Pokemon)
@@ -47,14 +49,14 @@ func GetPokemons() (map[string]*Pokemon, error) {
for i := range pokemonsData.Results {
go func(iterator int) {
defer wg.Done()
indPokemon := fetchIndividualPokemons(pokemonsData.Results[iterator].URL)
indPokemon := fetchIndividualPokemon(pokemonsData.Results[iterator].URL)
pokemonStream <- indPokemon
}(i)
}
for range pokemonsData.Results {
p := <-pokemonStream
PokemonMap[p.Name] = &p
pokemonMap[p.Name] = &p
}
go func() {
@@ -62,12 +64,25 @@ func GetPokemons() (map[string]*Pokemon, error) {
close(pokemonStream)
}()
fmt.Print(len(PokemonMap))
sortedPokemonSlice := make([]string, len(pokemonsData.Results))
for pokemon := range pokemonMap {
name := pokemonMap[pokemon].Name
sortedPokemonSlice = append(sortedPokemonSlice, name)
}
sort.Strings(sortedPokemonSlice)
return PokemonMap, nil
pokemons := Pokemons{
Count: pokemonsData.Count,
Next: pokemonsData.Next,
Previous: pokemonsData.Previous,
SortedPokemons: sortedPokemonSlice,
PokemonMap: pokemonMap,
}
return pokemons, nil
}
func fetchIndividualPokemons(pokemonURL string) Pokemon {
func fetchIndividualPokemon(pokemonURL string) Pokemon {
resp, err := http.Get(pokemonURL)
if err != nil {
fmt.Println(err.Error())
+20 -5
View File
@@ -7,12 +7,12 @@ type SiteLink struct {
var NavigationLinks = []SiteLink{
{
Title: "500 Pokemons",
Title: "All Pokemons",
Link: "/",
},
{
Title: "Choose how many",
Link: "/custom",
Title: "With Pagination",
Link: "/pagination",
},
}
@@ -25,9 +25,21 @@ type Pokemon struct {
Image string
}
type Pokemons struct {
Count int
Next string
Previous string
SortedPokemons []string
PokemonMap map[string]*Pokemon
}
type PageData struct {
NavigationLinks []SiteLink
Data map[string]*Pokemon
Data Pokemons
}
type NextURL struct {
URL string `form:"url"`
}
type PokemonMove struct {
@@ -68,5 +80,8 @@ type PokemonAPIResult struct {
URL string `json:"url"`
}
type PokemonsAPIResponse struct {
Results []PokemonAPIResult `json:"results"`
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []PokemonAPIResult `json:"results"`
}
+22
View File
@@ -0,0 +1,22 @@
package infrastructure
import (
"fmt"
"time"
)
func GetCurrentYear() string {
return fmt.Sprintf("%v", time.Now().Year())
}
func GetPrevPagePokemons(current int) int {
if current < 10 {
return current
}
return current - 10
}
func GetNextPagePokemons(current int) int {
return current + 10
}
+16 -3
View File
@@ -6,9 +6,10 @@ import (
"net/http"
"path/filepath"
"strings"
"text/template"
"time"
"github.com/diaid83/pokemongotemplates/internal/infrastructure"
"github.com/dadobos/pokemongotemplates/internal/infrastructure"
"github.com/gin-gonic/gin"
)
@@ -22,6 +23,13 @@ type ApplicationServer struct {
State ApplicationState
}
var templateFunctionMap = template.FuncMap{
"GetCurrentYear": infrastructure.GetCurrentYear,
"GetPrevPagePokemons": infrastructure.GetPrevPagePokemons,
"GetNextPagePokemons": infrastructure.GetNextPagePokemons,
}
func (s *ApplicationServer) registerHandlers() {
var files []string
@@ -36,9 +44,14 @@ func (s *ApplicationServer) registerHandlers() {
if err != nil {
panic(err.Error())
}
s.State.Handler.SetFuncMap(templateFunctionMap)
s.State.Handler.LoadHTMLFiles(files...)
s.State.Handler.GET("/", s.pokemonPageHandler())
s.State.Handler.GET("/custom", s.customPokemonPageHandler())
s.State.Handler.GET("/", s.pageHandler())
s.State.Handler.GET("/pagination", s.paginationPageHandler())
s.State.Handler.GET("/form/send-pagination", s.slugSendPaginationRequestHandler())
}
func NewApplicationServer(userOptions *ApplicationState) *ApplicationServer {
+31 -5
View File
@@ -3,13 +3,14 @@ package app
import (
"fmt"
"net/http"
"net/url"
"github.com/diaid83/pokemongotemplates/internal/domain/pokemongotemplates"
"github.com/dadobos/pokemongotemplates/internal/domain/pokemongotemplates"
"github.com/gin-gonic/gin"
)
func (s *ApplicationServer) pokemonPageHandler() func(*gin.Context) {
data, err := pokemongotemplates.GetPokemons()
func (s *ApplicationServer) pageHandler() func(*gin.Context) {
data, err := pokemongotemplates.GetPokemons(pokemongotemplates.AllPokemonsURL)
if err != nil {
fmt.Println(err.Error())
}
@@ -20,10 +21,35 @@ func (s *ApplicationServer) pokemonPageHandler() func(*gin.Context) {
})
}
}
func (s *ApplicationServer) customPokemonPageHandler() func(*gin.Context) {
func (s *ApplicationServer) paginationPageHandler() func(*gin.Context) {
return func(c *gin.Context) {
c.HTML(http.StatusOK, "views/CustomPokemons.gohtml", pokemongotemplates.PageData{
requestURL := pokemongotemplates.PaginationURL
if newURL := c.Request.URL.Query(); newURL.Has("slug") {
requestURL = newURL["slug"][0]
}
data, err := pokemongotemplates.GetPokemons(requestURL)
if err != nil {
fmt.Println(err.Error())
}
c.HTML(http.StatusOK, "views/PaginationPokemons.gohtml", pokemongotemplates.PageData{
NavigationLinks: pokemongotemplates.NavigationLinks,
Data: data,
})
}
}
func (s *ApplicationServer) slugSendPaginationRequestHandler() func(*gin.Context) {
return func(c *gin.Context) {
var requestData pokemongotemplates.NextURL
if err := c.Bind(&requestData); err != nil {
c.Redirect(http.StatusFound, "/")
return
}
c.Redirect(http.StatusFound, fmt.Sprintf("/pagination/?slug=%s", url.QueryEscape(requestData.URL)))
}
}