Files
ris-backend-go/services/patient/patient.routes.go
2024-12-09 09:51:19 +07:00

99 lines
3.2 KiB
Go

package patient
import (
"fmt"
"net/http"
"github.com/go-playground/validator/v10"
"github.com/gorilla/mux"
"sismedika.com/sas/westone/services/auth"
"sismedika.com/sas/westone/types"
"sismedika.com/sas/westone/utils"
)
type Handler struct {
store types.PatientStore
}
func NewHandler(store types.PatientStore) *Handler {
return &Handler{store: store}
}
func (h *Handler) RegisterRoutes(router *mux.Router) {
patientroute := router.PathPrefix("/mdpatient").Subrouter()
patientroute.Use(auth.AuthMiddleware)
patientroute.HandleFunc("/searchpatient", h.handlerSearchPatient).Methods(http.MethodPost)
patientroute.HandleFunc("/searchpatientold", h.handlerSearchPatientOld).Methods(http.MethodPost)
}
// SearchPatient searches patients by name, NIK, registration number, DOB, or phone number
//
// @Summary Search patient listing
// @Description Search patients by name, NIK, registration number, DOB, or phone number
// @Tags Patient
// @Accept json
// @Param authorization header string true "Authorization token"
// @Param parameter body types.SearchPatientPayload true "parameter"
// @Produce json
// @Success 200 {object} map[string]any
// @Failure 400 {object} map[string]any
// @Failure 500 {object} map[string]any
// @Router /westone/api/v1/mdpatient/searchpatient [post]
func (h *Handler) handlerSearchPatient(w http.ResponseWriter, r *http.Request) {
var payload types.SearchPatientPayload
if err := utils.ParseJSON(r, &payload); err != nil {
utils.WriteError(w, http.StatusBadRequest, err)
return
}
if err := utils.Validate.Struct(payload); err != nil {
erros := err.(validator.ValidationErrors)
utils.WriteError(w, http.StatusBadRequest, fmt.Errorf("invalid payload: %v", erros))
return
}
response, err := h.store.SearchPatient(payload.Keyword, payload.Page, payload.Perpage)
if err != nil {
utils.WriteError(w, http.StatusInternalServerError, err)
return
}
utils.WriteJSON(w, http.StatusOK, response)
}
// SearchPatientOld searches patients by name, NIK, registration number, DOB, or phone number
//
// @Summary Search patient listing old methods
// @Description Search patients by name, NIK, registration number, DOB, or phone number
// @Tags Patient
// @Accept json
// @Param authorization header string true "Authorization token"
// @Param parameter body types.SearchPatientPayload true "parameter"
// @Produce json
// @Success 200 {object} map[string]any
// @Failure 400 {object} map[string]any
// @Failure 500 {object} map[string]any
// @Router /westone/api/v1/mdpatient/searchpatientold [post]
func (h *Handler) handlerSearchPatientOld(w http.ResponseWriter, r *http.Request) {
var payload types.SearchPatientPayload
if err := utils.ParseJSON(r, &payload); err != nil {
utils.WriteError(w, http.StatusBadRequest, err)
return
}
if err := utils.Validate.Struct(payload); err != nil {
erros := err.(validator.ValidationErrors)
utils.WriteError(w, http.StatusBadRequest, fmt.Errorf("invalid payload: %v", erros))
return
}
response, err := h.store.SearchPatientOld(payload.Keyword, payload.Page, payload.Perpage)
if err != nil {
utils.WriteError(w, http.StatusInternalServerError, err)
return
}
utils.WriteJSON(w, http.StatusOK, response)
}