-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
51 lines (40 loc) · 1.43 KB
/
web.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
package handler
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
"github.com/hiago-balbino/random-luck/v2/internal/game"
"github.com/hiago-balbino/random-luck/v2/internal/pkg/apperrors"
"github.com/hiago-balbino/random-luck/v2/internal/pkg/logger"
)
// web is a struct that implements the Handler interface.
type web struct {
randomizer game.GameRandomizer
}
// NewWeb is a constructor for creating a new Web handler instance.
func NewWeb(randomize game.GameRandomizer) web {
return web{randomizer: randomize}
}
// Process is a function implementation to execute calls to create random luck numbers.
func (w web) Process(c *gin.Context) {
var request requestSchema
if err := c.BindQuery(&request); err != nil {
log.Error("error binding query params", logger.FieldError(err))
c.HTML(http.StatusBadRequest, "error.html", gin.H{"message": err.Error()})
return
}
games, err := w.randomizer.Randomize(int(request.AmountOfGames), int(request.AmountOfNumbersPerGame))
if err != nil {
if errors.Is(err, apperrors.ErrBase) {
c.HTML(http.StatusBadRequest, "error.html", gin.H{"message": err.Error()})
return
}
c.HTML(http.StatusInternalServerError, "error.html", gin.H{"message": err.Error()})
return
}
response := make([]responseSchema, 0)
for _, game := range games {
response = append(response, responseSchema{GameID: game.ID, Numbers: game.Numbers})
}
c.HTML(http.StatusOK, "games.html", gin.H{"games": response})
}