add ParamToInt method
This commit is contained in:
parent
c7024d7f4e
commit
b3ab98f9a2
@ -3,6 +3,7 @@ package domains
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -14,3 +15,22 @@ func (bh *BaseHandler) ToJSON(w http.ResponseWriter, statusCode int, data any) {
|
|||||||
w.WriteHeader(statusCode)
|
w.WriteHeader(statusCode)
|
||||||
json.NewEncoder(w).Encode(data)
|
json.NewEncoder(w).Encode(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bh *BaseHandler) ParamToInt(param string, defaultValue ...int) int {
|
||||||
|
if param == "" {
|
||||||
|
if len(defaultValue) > 0 {
|
||||||
|
return defaultValue[0]
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
value, err := strconv.Atoi(param)
|
||||||
|
if err != nil {
|
||||||
|
if len(defaultValue) > 0 {
|
||||||
|
return defaultValue[0]
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|||||||
@ -10,7 +10,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"servicea/internal/app"
|
"servicea/internal/app"
|
||||||
"servicea/internal/domains"
|
"servicea/internal/domains"
|
||||||
"strconv"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -80,34 +79,13 @@ func (h *Handler) IngestExcel(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) {
|
||||||
queryParams := r.URL.Query()
|
queryParams := r.URL.Query()
|
||||||
pageInt := int(1)
|
|
||||||
limitInt := int(10)
|
|
||||||
|
|
||||||
page := queryParams.Get("page")
|
|
||||||
limit := queryParams.Get("limit")
|
|
||||||
|
|
||||||
if page != "" {
|
|
||||||
p, err := strconv.Atoi(page)
|
|
||||||
if err == nil {
|
|
||||||
pageInt = p
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if limit != "" {
|
|
||||||
l, err := strconv.Atoi(limit)
|
|
||||||
if err == nil {
|
|
||||||
limitInt = l
|
|
||||||
} else {
|
|
||||||
limitInt = 10
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
params := GetMeteoData{
|
params := GetMeteoData{
|
||||||
Location: queryParams.Get("city"),
|
Location: queryParams.Get("city"),
|
||||||
From: queryParams.Get("from"),
|
From: queryParams.Get("from"),
|
||||||
To: queryParams.Get("to"),
|
To: queryParams.Get("to"),
|
||||||
Page: pageInt,
|
Page: h.ParamToInt(queryParams.Get("page"), 1),
|
||||||
Limit: limitInt,
|
Limit: h.ParamToInt(queryParams.Get("limit"), 10),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := params.Validate(); err != nil {
|
if err := params.Validate(); err != nil {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user