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
+6
View File
@@ -13,3 +13,9 @@
# Dependency directories (remove the comment below to include it)
# vendor/
# Ignore Mac system files
.DS_store
# Ignore files related to API keys
.env
+21 -1
View File
@@ -1,2 +1,22 @@
# pokemongotemplates
A go application that makes an api call to the pokemon api, and shows a detail list of pokemons using go templates.
A server side go application that makes multiple api calls to the pokemon api, and shows a detail list of pokemons using go templates.
![Go](https://img.shields.io/badge/go-%2300ADD8.svg?style=for-the-badge&logo=go&logoColor=white)
![HTML5](https://img.shields.io/badge/html5-%23E34F26.svg?style=for-the-badge&logo=html5&logoColor=white)
![Bootstrap](https://img.shields.io/badge/bootstrap-%23563D7C.svg?style=for-the-badge&logo=bootstrap&logoColor=white)
### Instalation
#### Clone
``` git clone https://github.com/dadobos/pokemongotemplates.git```
#### Go inside the folder
``` cd pokemongotemplates/cmd/pokemongotemplates```
#### Run the application
``` go run main.go ```
#### Open the browser at
``` http://localhost:8080/ ```
BIN
View File
Binary file not shown.
+2 -2
View File
@@ -10,8 +10,8 @@ import (
"syscall"
"time"
"github.com/diaid83/pokemongotemplates/internal/infrastructure"
"github.com/diaid83/pokemongotemplates/internal/infrastructure/app"
"github.com/dadobos/pokemongotemplates/internal/infrastructure"
"github.com/dadobos/pokemongotemplates/internal/infrastructure/app"
)
func ListenAndServeGoRoutine(srv *http.Server) {
+1 -1
View File
@@ -1,4 +1,4 @@
module github.com/diaid83/pokemongotemplates
module github.com/dadobos/pokemongotemplates
go 1.17
+10 -6
View File
@@ -64,6 +64,7 @@ github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWH
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@@ -85,6 +86,7 @@ github.com/gin-gonic/gin v1.7.4/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjX
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
@@ -110,7 +112,6 @@ github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+Licev
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk=
@@ -140,6 +141,7 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
@@ -190,7 +192,6 @@ github.com/hashicorp/memberlist v0.2.2/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOn
github.com/hashicorp/serf v0.9.5/go.mod h1:UWDWwZeL5cuWDJdl0C6wrvrUwEqtQ4ZKBKKENpqIUyk=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
@@ -200,8 +201,10 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
@@ -227,7 +230,6 @@ github.com/mitchellh/mapstructure v1.4.2 h1:6h7AQ0yhTcIsmFmnAwQls75jp2Gzs4iB8W7p
github.com/mitchellh/mapstructure v1.4.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
@@ -236,6 +238,7 @@ github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhEC
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
@@ -262,6 +265,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
@@ -295,7 +299,6 @@ golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3H3cr1v9wB50oz8l4C4h62xy7jSTY=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
@@ -417,7 +420,6 @@ golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -524,6 +526,7 @@ golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
@@ -653,17 +656,18 @@ google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+Rur
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.63.2 h1:tGK/CyBg7SMzb60vP1M03vNZ3VDu3wGQJwn7Sxi9r3c=
gopkg.in/ini.v1 v1.63.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
+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)))
}
}
+6 -1
View File
@@ -1,6 +1,11 @@
{{define "components/Footer.gohtml"}}
</body>
<div class="d-flex justify-content-center p-6 text-bg-dark text-info">Dan Dobos &copy; {{GetCurrentYear}}</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3"
crossorigin="anonymous"></script>
</body>
</html>
{{end}}
+20 -13
View File
@@ -1,17 +1,24 @@
{{define "components/Head.gohtml"}}
<!DOCTYPE html>
<html lang="en" style="scroll-behavior: smooth;">
<html lang="en" style="scroll-behavior: smooth">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link
rel="icon"
href="https://res.cloudinary.com/dadobos/image/upload/v1642766520/web/favicon_ugvc88.ico"
type="image/x-icon"
/>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css"
integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi"
crossorigin="anonymous"
/>
<title>Pokemon Go Templates</title>
</head>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link rel="icon" href="https://raw.githubusercontent.com/diaid83/details/master/favicon.ico" type="image/x-icon" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<title>Pokemon Go Templates</title>
</head>
<body>
{{ end }}
<body >
{{end}}
+53 -21
View File
@@ -1,25 +1,57 @@
{{define "components/Navbar.gohtml"}}
<nav class="navbar navbar-expand-lg fixed-top navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="#">Pokemon Go Templates</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
{{range .}}
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="{{.Link}}">{{.Title}}</a>
</li>
{{end}}
</ul>
<form class="d-flex" onsubmit="document.getElementById(document.getElementById('search').value).scrollIntoView();return false">
<input class="form-control me-2" type="search" id="search" name="search" placeholder="Pokemon Name" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
<nav class="navbar navbar-expand-lg fixed-top bg-dark">
<div class="container">
<a class="navbar-brand text-info" href="/">Pokemon Go Templates</a>
<button
class="navbar-toggler btn btn-sm text-bg-info"
type="button"
data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
{{range.NavigationLinks}}
<li class="nav-item">
<a
class="nav-link active text-info"
aria-current="page"
href="{{.Link}}"
>
{{.Title}}
</a>
</li>
{{end}}
</ul>
<form
autocomplete="off"
class="d-flex"
onsubmit="document.getElementById(document.getElementById('search').value).scrollIntoView();return false"
>
<div class="autocomplete" style="width: 300px">
<input
class="form-control me-2"
type="text"
id="search"
name="search"
placeholder="Pokemon Name"
aria-label="Search"
list="searchList"
/>
<datalist id="searchList">
{{ range.Data.SortedPokemons }}
<option value="{{.}}"></option>
{{ end }}
</datalist>
</div>
<button class="btn btn-outline-info" type="submit">Search</button>
</form>
</div>
</div>
</nav>
{{ end }}
@@ -0,0 +1,40 @@
{{define "components/Pagination.gohtml"}}
<nav class="p-3">
<ul class="pagination justify-content-center">
{{if .Data.Previous}}
<li class="page-item">
<form enctype="application/x-www-form-urlencoded"
action="/form/send-pagination"
method="get">
<label>
<input name="url" type="hidden" value="{{.Data.Previous}}">
</label>
<button class="btn btn-outline-info"
type="submit">
Previous
</button>
</form>
</li>
{{end}}
{{if .Data.Next}}
<li class="page-item">
<form enctype="application/x-www-form-urlencoded"
action="/form/send-pagination"
method="get">
<label>
<input type="hidden"
name="url"
value="{{.Data.Next}}">
</label>
<button class="btn btn-outline-info"
type="submit">
Next
</button>
</form>
</li>
{{end}}
</ul>
</nav>
{{end}}
@@ -0,0 +1,59 @@
{{define "components/PokemonCard.gohtml"}}
<div class="col-xl-3 col-lg-4 col-md-6 col-sm-12 col-xs-12"
align="center"
id="{{.Name}}">
<div class="card m-1 text-bg-secondary"
style="width: 18rem;"
onMouseOver="this.style.transform = ('scale(1.05)');this.style.transition='0.3s';"
onMouseOut="this.style.transform = ('scale(1)');this.style.transition='0.3s';"
align="start">
{{if ne .Image ""}}
<img src="{{.Image}}"
class="card-img-top"
alt="{{.Name}}">
{{else}}
<picture>
<!-- <source
srcset="https://res.cloudinary.com/dadobos/image/upload/v1669533239/web/qhvaith9pdepdylkekzl.webp"
type="image/webp"/> -->
<img src="https://res.cloudinary.com/dadobos/image/upload/v1669533239/web/qhvaith9pdepdylkekzl.png"
class="card-img-top"
alt="{{.Name}}"
loading="lazy">
</picture>
{{end}}
<div class="card-body">
<h5 class="card-title">
<span>Name: </span>
<span class="text-capitalize">{{.Name}}</span>
</h5>
<p class="card-text">
<div class="row">
<div class="col-6">
<strong>Weight: </strong>
<span>{{.Weight}}</span>
</div>
<div class="col-6">
<strong>Height:</strong>
<span>{{.Height}}</span>
</div>
</div>
<div class="row">
<div class="col">
<strong>Types: </strong>
<span>{{.Types}}</span>
</div>
</div>
<div class="row">
<div class="col">
<strong>Moves: </strong>
<p>{{.Moves}}</p>
</div>
</div>
</p>
</div>
</div>
</div>
{{end}}
-12
View File
@@ -1,12 +0,0 @@
{{define "views/CustomPokemons.gohtml"}}
{{template "components/Head.gohtml"}}
{{template "components/Navbar.gohtml" .NavigationLinks}}
<div class="container-xxl pt-5">
<h1>views/CustomPokemons.gohtml</h1>
</div>
{{template "components/Footer.gohtml"}}
{{end}}
@@ -0,0 +1,21 @@
{{define "views/PaginationPokemons.gohtml"}}
{{template "components/Head.gohtml"}}
{{template "components/Navbar.gohtml" .}}
<div class="container-fluid text-bg-dark">
<div class="container-xxl pt-5">
{{template "components/Pagination.gohtml" .}}
<div
class="row row-cols-auto g-2 align-items-center justify-content-center">
{{ range .Data.PokemonMap }}
{{template "components/PokemonCard.gohtml" .}}
{{ end }}
</div>
{{template "components/Pagination.gohtml" .}}
</div>
</div>
{{template "components/Footer.gohtml"}}
{{ end }}
+8 -48
View File
@@ -1,56 +1,16 @@
{{define "views/Pokemons.gohtml"}}
{{template "components/Head.gohtml"}}
{{template "components/Navbar.gohtml" .NavigationLinks}}
<div class="container-fluid bg-light bg-gradient">
<div class="container-xxl pt-5">
<div class="row row-cols-auto g-2 mt-5">
{{range .Data}}
<div class="col-xl-3 col-lg-4 col-md-6 col-sm-12" align="center" id="{{.Name}}">
<div class="card m-1" style="width: 18rem;" onMouseOver="this.style.transform = ('scale(1.1)');this.style.transition='0.3s';"
onMouseOut="this.style.transform = ('scale(1)');this.style.transition='0.3s';" align="start">
<img src="{{.Image}}" class="card-img-top" alt="{{.Name}}">
<div class="card-body">
<h5 class="card-title">
<span>Name: </span>
<span>{{.Name}}</span>
</h5>
<p class="card-text">
<div class="row">
<div class="col-6">
<strong>Weight: </strong>
<span>{{.Weight}}</span>
</div>
<div class="col-6">
<strong>Height:</strong>
<span>{{.Height}}</span>
</div>
</div>
<div class="row">
<div class="col">
<strong>Types: </strong>
<span>{{.Types}}</span>
</div>
</div>
<div class="row">
<div class="col">
<strong>Moves: </strong>
<p>{{.Moves}}</p>
</div>
</div>
</p>
</div>
{{template "components/Navbar.gohtml" .}}
<div class="container-fluid text-bg-dark">
<div class="container-xxl pt-5">
<div class="row row-cols-auto g-2 mt-5 align-items-center justify-content-center">
{{range .Data.PokemonMap}}
{{ template "components/PokemonCard.gohtml" . }}
{{end}}
</div>
</div>
{{end}}
</div>
</div>
</div>
{{template "components/Footer.gohtml"}}
{{template "components/Footer.gohtml" .Data.SortedPokemons}}
{{end}}