-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (57 loc) · 1.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"strconv"
"strings"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/template/html/v2"
"github.com/jaymo107/whos-that-pokemon/pokemon"
"github.com/jaymo107/whos-that-pokemon/storage"
)
func main() {
engine := html.New("./views", ".html")
repository := storage.NewSqlRepository()
pokeService := pokemon.NewPokemonService(
pokemon.PokemonServiceConfig{
Repository: repository,
},
)
app := fiber.New(fiber.Config{
Views: engine,
})
app.Get("/", func(c fiber.Ctx) error {
pokemon := pokeService.GetRandomPokemon()
return c.Render("index", fiber.Map{
"Name": pokemon.Name,
"Image": pokemon.Image,
"Id": pokemon.Id,
})
})
app.Get("/leaderboard", func(c fiber.Ctx) error {
pokemon := repository.All()
for i := range pokemon {
pokemon[i].FormatName()
}
return c.Render("leaderboard", fiber.Map{
"Pokemon": pokemon,
})
})
app.Post("/guess", func(c fiber.Ctx) error {
guess := c.FormValue("guess")
id, _ := strconv.Atoi(c.FormValue("id"))
pokemon, _ := repository.FindById(id)
fmt.Println("guess: ", guess, pokemon.Name, id)
template := "wrong"
if strings.EqualFold(pokemon.Name, guess) {
template = "correct"
pokeService.MarkCorrectGuess(id)
} else {
pokeService.MarkIncorrectGuess(id)
}
return c.Render(template, fiber.Map{
"Name": pokemon.Name,
"Image": pokemon.Image,
})
})
app.Listen(":8080")
}