Changed theme, added search, added pagination
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user