forked from ebhomengo/niki
156 lines
3.3 KiB
Go
156 lines
3.3 KiB
Go
package analytic
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/patientapp/delivery/http/analytic/dto"
|
|
svc "git.gocasts.ir/ebhomengo/niki/patientapp/service/analytic"
|
|
)
|
|
|
|
type Handler struct {
|
|
service svc.Service
|
|
}
|
|
|
|
func NewHandler(service svc.Service) *Handler {
|
|
return &Handler{
|
|
service: service,
|
|
}
|
|
}
|
|
|
|
func (h *Handler) Health(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = w.Write([]byte("ok"))
|
|
}
|
|
|
|
func (h *Handler) PatientsAnalytic(w http.ResponseWriter, r *http.Request) {
|
|
q := r.URL.Query()
|
|
|
|
minAge, err := parseIntPtr(q, "minAge")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid minAge")
|
|
return
|
|
}
|
|
maxAge, err := parseIntPtr(q, "maxAge")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid maxAge")
|
|
return
|
|
}
|
|
|
|
sex, err := parseSexPtr(q, "sex")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid sex")
|
|
return
|
|
}
|
|
|
|
provinceID, err := parseIntPtr(q, "provinceId")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid provinceId")
|
|
return
|
|
}
|
|
cityID, err := parseIntPtr(q, "cityId")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid cityId")
|
|
return
|
|
}
|
|
|
|
search := strings.TrimSpace(q.Get("search"))
|
|
var searchPtr *string
|
|
if search != "" {
|
|
searchPtr = &search
|
|
}
|
|
|
|
limit, err := parseInt(q, "limit")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid limit")
|
|
return
|
|
}
|
|
offset, err := parseInt(q, "offset")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid offset")
|
|
return
|
|
}
|
|
|
|
pId := int64(*provinceID)
|
|
cId := int64(*cityID)
|
|
|
|
req := dto.ListPatientAnalyticRequest{
|
|
MinAge: minAge,
|
|
MaxAge: maxAge,
|
|
Sex: sex,
|
|
Province: &pId,
|
|
City: &cId,
|
|
Search: searchPtr,
|
|
Limit: limit,
|
|
Offset: offset,
|
|
}
|
|
|
|
response, err := h.service.List(r.Context(), req)
|
|
if err != nil {
|
|
log.Println("List error:", err)
|
|
writeError(w, http.StatusInternalServerError, fmt.Errorf("List error: %w", err).Error())
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, response)
|
|
}
|
|
|
|
func (h *Handler) PatientsMapSummary(w http.ResponseWriter, r *http.Request) {
|
|
q := r.URL.Query()
|
|
|
|
level, err := parseMapLevel(q, "level")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid level (city|province|country)")
|
|
return
|
|
}
|
|
|
|
parentID, err := parseIntPtr(q, "parentId")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid parentId")
|
|
return
|
|
}
|
|
|
|
minAge, err := parseIntPtr(q, "minAge")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid minAge")
|
|
return
|
|
}
|
|
maxAge, err := parseIntPtr(q, "maxAge")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid maxAge")
|
|
return
|
|
}
|
|
|
|
sex, err := parseSexPtr(q, "sex")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid sex")
|
|
return
|
|
}
|
|
|
|
search := strings.TrimSpace(q.Get("search"))
|
|
var searchPtr *string
|
|
if search != "" {
|
|
searchPtr = &search
|
|
}
|
|
|
|
req := dto.GetPatientMapSummaryRequest{
|
|
Level: level,
|
|
ParentID: parentID,
|
|
MinAge: minAge,
|
|
MaxAge: maxAge,
|
|
Sex: sex,
|
|
Search: searchPtr,
|
|
}
|
|
|
|
resp, svcErr := h.service.GetMapSummary(r.Context(), req)
|
|
if svcErr != nil {
|
|
log.Println("GetMapSummary error:", svcErr)
|
|
writeError(w, http.StatusInternalServerError, svcErr.Error())
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|