forked from ebhomengo/niki
71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
package analytic
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/patientapp/service/entity"
|
|
)
|
|
|
|
func writeJSON(w http.ResponseWriter, status int, v any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
_ = json.NewEncoder(w).Encode(v)
|
|
}
|
|
|
|
func writeError(w http.ResponseWriter, status int, msg string) {
|
|
writeJSON(w, status, map[string]any{"error": msg})
|
|
}
|
|
|
|
func parseInt(q urlValues, key string) (int, error) {
|
|
val := strings.TrimSpace(q.Get(key))
|
|
if val == "" {
|
|
return 0, nil
|
|
}
|
|
return strconv.Atoi(val)
|
|
}
|
|
|
|
type urlValues interface {
|
|
Get(key string) string
|
|
}
|
|
|
|
func parseIntPtr(q urlValues, key string) (*int, error) {
|
|
val := strings.TrimSpace(q.Get(key))
|
|
if val == "" {
|
|
return nil, nil
|
|
}
|
|
i, err := strconv.Atoi(val)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &i, nil
|
|
}
|
|
|
|
func parseSexPtr(q urlValues, key string) (*entity.Sex, error) {
|
|
val := strings.TrimSpace(q.Get(key))
|
|
if val == "" {
|
|
return nil, nil
|
|
}
|
|
s := entity.Sex(val)
|
|
if !s.SexValidation() {
|
|
return nil, strconv.ErrSyntax
|
|
}
|
|
return &s, nil
|
|
}
|
|
|
|
func parseMapLevel(q urlValues, key string) (entity.MapLevel, error) {
|
|
val := strings.TrimSpace(q.Get(key))
|
|
if val == "" {
|
|
return "", strconv.ErrSyntax
|
|
}
|
|
l := entity.MapLevel(val)
|
|
switch l {
|
|
case entity.MapLevelCity, entity.MapLevelProvince, entity.MapLevelCountry:
|
|
return l, nil
|
|
default:
|
|
return "", strconv.ErrSyntax
|
|
}
|
|
}
|