first commit
This commit is contained in:
98
services/patient/patient.routes.go
Normal file
98
services/patient/patient.routes.go
Normal file
@@ -0,0 +1,98 @@
|
||||
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)
|
||||
}
|
||||
161
services/patient/patient.store.go
Normal file
161
services/patient/patient.store.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package patient
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"sismedika.com/sas/westone/types"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
func NewStore(db *sqlx.DB) *Store {
|
||||
return &Store{db: db}
|
||||
}
|
||||
|
||||
func (s *Store) GetPatientByID(id int) (*types.Patient, error) {
|
||||
patient := new(types.Patient)
|
||||
|
||||
qry := `
|
||||
SELECT
|
||||
PatientID
|
||||
PatientPersonID,
|
||||
PatientManaginOrganizationID,
|
||||
PatientHISNumber,
|
||||
PatientRegNumber
|
||||
FROM patient
|
||||
WHERE PatientID = ?
|
||||
`
|
||||
if err := s.db.Get(patient, qry, id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return patient, nil
|
||||
}
|
||||
|
||||
func (s *Store) SearchPatient(keyword string, page int, perpage int) ([]types.PatientResult, error) {
|
||||
var list_patient []types.PatientResult
|
||||
var sqlName, sqlNIK, sqlReg, sqlDob, sqlhp string
|
||||
|
||||
// params nama+nik+noreg+dob+nohp
|
||||
if keyword != "" {
|
||||
params := strings.Split(keyword, "+")
|
||||
if len(params) > 0 && params[0] != "" {
|
||||
// sqlName = " AND PersonName LIKE '%" + params[0] + "%' "
|
||||
sqlName = " WHERE MATCH(PersonNameFulltext) AGAINST('" + params[0] + "' IN BOOLEAN MODE) "
|
||||
}
|
||||
if len(params) > 1 && params[1] != "" {
|
||||
sqlNIK = " AND PersonIdentifierValue = '" + params[1] + "' "
|
||||
}
|
||||
if len(params) > 2 && params[2] != "" {
|
||||
sqlReg = " AND PatientRegNumber = '" + params[2] + "' "
|
||||
}
|
||||
if len(params) > 3 && params[3] != "" {
|
||||
sqlDob = " AND PersonBirthDate = '" + params[3] + "' "
|
||||
}
|
||||
if len(params) > 4 && params[4] != "" {
|
||||
sqlhp = " AND PersonTelecomValue = '" + params[4] + "' "
|
||||
}
|
||||
}
|
||||
|
||||
patient_tb := `PatientID, PatientPersonID, PatientManaginOrganizationID, PatientHISNumber, PatientRegNumber`
|
||||
|
||||
person_tb := `PersonID, PersonName, PersonSalutation, PersonPrefix, PersonSuffix, PersonGender, PersonBirthPlace, PersonBirthDate,
|
||||
PersonReligionCode, PersonReligionSystem, PersonBloodTypeCode, PersonBloodTypeSystem, PersonRhesusCode, PersonRhesusSystem,
|
||||
PersonEducationCode, PersonEducationSystem, PersonJobClassCode, PersonJobClassSystem, PersonEtnicityCode, PersonEtnicitySystem,
|
||||
PersonMaritalBirth, PersonMaritalStatus`
|
||||
|
||||
person_address_tb := `PersonAddressID, PersonAddressPersonID, PersonAddressIsDefault, PersonAddressUse, PersonAddressType, PersonAddressText,
|
||||
PersonAddressLine1, PersonAddressLine2, PersonAddressRegionalCd, PersonAddressRT, PersonAddressRW, PersonAddressCity,
|
||||
PersonAddressDistrict, PersonAddressState, PersonAddressCountry`
|
||||
|
||||
person_identifier_tb := `PersonIdentifierID, PersonIdentifierPersonID, PersonIdentifierUse, PersonIdentifierTypeCode,
|
||||
PersonIdentifierTypeSystem, PersonIdentifierCode, PersonIdentifierSystem, PersonIdentifierValue`
|
||||
|
||||
person_telecom_tb := `PersonTelecomID, PersonTelecomPersonID, PersonTelecomSystem, PersonTelecomValue, PersonTelecomUse`
|
||||
|
||||
offset := (page - 1) * perpage
|
||||
sql := `SELECT ` + patient_tb + `,` + person_tb + `,` + person_identifier_tb + `,` + person_address_tb + `,` + person_telecom_tb +
|
||||
` FROM patient
|
||||
JOIN person ON PersonID = PatientPersonID AND PatientIsActive = 'Y'` + sqlDob + sqlReg + `
|
||||
JOIN person_identifier ON PersonIdentifierPersonID = PersonID` + sqlNIK + `
|
||||
JOIN person_telecom ON PersonTelecomPersonID = PersonID` + sqlhp + `
|
||||
JOIN person_address ON PersonAddressPersonID = PersonID
|
||||
` + sqlName + `
|
||||
LIMIT ? OFFSET ?`
|
||||
|
||||
if err := s.db.Select(&list_patient, sql, perpage, offset); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return list_patient, nil
|
||||
}
|
||||
|
||||
func (s *Store) SearchPatientOld(keyword string, page int, perpage int) ([]types.PatientResult, error) {
|
||||
var list_patient []types.PatientResult
|
||||
// var sqlFilter []string
|
||||
var sqlName, sqlNIK, sqlReg, sqlDob, sqlhp string
|
||||
|
||||
// params nama+nik+noreg+dob+nohp
|
||||
if keyword != "" {
|
||||
params := strings.Split(keyword, "+")
|
||||
if len(params) > 0 && params[0] != "" {
|
||||
// sqlFilter = append(sqlFilter, "PersonName LIKE '%"+params[0]+"%'")
|
||||
sqlName = " AND PersonName LIKE '%" + params[0] + "%' "
|
||||
}
|
||||
if len(params) > 1 && params[1] != "" {
|
||||
// sqlFilter = append(sqlFilter, "PersonIdentifierValue = '"+params[1]+"'")
|
||||
sqlNIK = " AND PersonIdentifierValue = '" + params[1] + "' "
|
||||
}
|
||||
if len(params) > 2 && params[2] != "" {
|
||||
// sqlFilter = append(sqlFilter, "PatientRegNumber = '"+params[2]+"'")
|
||||
sqlReg = " AND PatientRegNumber = '" + params[2] + "' "
|
||||
}
|
||||
if len(params) > 3 && params[3] != "" {
|
||||
// sqlFilter = append(sqlFilter, "PersonBirthDate = '"+params[3]+"'")
|
||||
sqlDob = " AND PersonBirthDate = '" + params[3] + "' "
|
||||
}
|
||||
if len(params) > 4 && params[4] != "" {
|
||||
// sqlFilter = append(sqlFilter, "PersonTelecomValue = '"+params[4]+"'")
|
||||
sqlhp = " AND PersonTelecomValue = '" + params[4] + "' "
|
||||
}
|
||||
}
|
||||
|
||||
patient_tb := `PatientID, PatientPersonID, PatientManaginOrganizationID, PatientHISNumber, PatientRegNumber`
|
||||
|
||||
person_tb := `PersonID, PersonName, PersonSalutation, PersonPrefix, PersonSuffix, PersonGender, PersonBirthPlace, PersonBirthDate,
|
||||
PersonReligionCode, PersonReligionSystem, PersonBloodTypeCode, PersonBloodTypeSystem, PersonRhesusCode, PersonRhesusSystem,
|
||||
PersonEducationCode, PersonEducationSystem, PersonJobClassCode, PersonJobClassSystem, PersonEtnicityCode, PersonEtnicitySystem,
|
||||
PersonMaritalBirth, PersonMaritalStatus`
|
||||
|
||||
person_address_tb := `PersonAddressID, PersonAddressPersonID, PersonAddressIsDefault, PersonAddressUse, PersonAddressType, PersonAddressText,
|
||||
PersonAddressLine1, PersonAddressLine2, PersonAddressRegionalCd, PersonAddressRT, PersonAddressRW, PersonAddressCity,
|
||||
PersonAddressDistrict, PersonAddressState, PersonAddressCountry`
|
||||
|
||||
person_identifier_tb := `PersonIdentifierID, PersonIdentifierPersonID, PersonIdentifierUse, PersonIdentifierTypeCode,
|
||||
PersonIdentifierTypeSystem, PersonIdentifierCode, PersonIdentifierSystem, PersonIdentifierValue`
|
||||
|
||||
person_telecom_tb := `PersonTelecomID, PersonTelecomPersonID, PersonTelecomSystem, PersonTelecomValue, PersonTelecomUse`
|
||||
|
||||
offset := (page - 1) * perpage
|
||||
sql := `SELECT ` + patient_tb + `,` + person_tb + `,` + person_identifier_tb + `,` + person_address_tb + `,` + person_telecom_tb +
|
||||
` FROM patient
|
||||
JOIN person ON PersonID = PatientPersonID AND PatientIsActive = 'Y'` + sqlName + sqlDob + sqlReg + `
|
||||
JOIN person_identifier ON PersonIdentifierPersonID = PersonID` + sqlNIK + `
|
||||
JOIN person_telecom ON PersonTelecomPersonID = PersonID` + sqlhp + `
|
||||
JOIN person_address ON PersonAddressPersonID = PersonID
|
||||
LIMIT ? OFFSET ?`
|
||||
|
||||
// if len(sqlFilter) > 0 {
|
||||
// sql += " AND " + strings.Join(sqlFilter, " AND ")
|
||||
// }
|
||||
// sql += " "
|
||||
|
||||
if err := s.db.Select(&list_patient, sql, perpage, offset); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return list_patient, nil
|
||||
}
|
||||
Reference in New Issue
Block a user