first commit

This commit is contained in:
Sas Andy
2024-12-09 09:51:19 +07:00
commit ecc5dfd9c0
69 changed files with 5365 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
package doctor
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.DoctorStore
}
func NewHandler(store types.DoctorStore) *Handler {
return &Handler{store: store}
}
func (h *Handler) RegisterRoutes(router *mux.Router) {
doctorroutes := router.PathPrefix("/mddoctor").Subrouter()
doctorroutes.Use(auth.AuthMiddleware)
doctorroutes.HandleFunc("/searchdoctor", h.handlerSearchDoctor).Methods(http.MethodPost)
}
// SearchDoctor searches doctor by name, NIK, Doctor Code, DOB, or phone number
//
// @Summary Search Doctors listing
// @Description Search Doctors by name, NIK, Doctor Code, DOB, or phone number
// @Tags Doctor
// @Accept json
// @Param authorization header string true "Authorization token"
// @Param parameter body types.SearchDoctorPayload 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/mddoctor/searchdoctor [post]
func (h *Handler) handlerSearchDoctor(w http.ResponseWriter, r *http.Request) {
var payload types.SearchDoctorPayload
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.SearchDoctor(payload.Keyword, payload.Page, payload.Perpage)
if err != nil {
utils.WriteError(w, http.StatusInternalServerError, err)
return
}
utils.WriteJSON(w, http.StatusOK, response)
}

View File

@@ -0,0 +1,76 @@
package doctor
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) SearchDoctor(keyword string, page int, perpage int) ([]types.DoctorResult, error) {
var list_doctor []types.DoctorResult
var sqlFilter []string
if keyword != "" {
params := strings.Split(keyword, "+")
if len(params) > 0 && params[0] != "" {
sqlFilter = append(sqlFilter, "PersonName LIKE '%"+params[0]+"%'")
}
if len(params) > 1 && params[1] != "" {
sqlFilter = append(sqlFilter, "PersonIdentifierValue = '"+params[1]+"'")
}
if len(params) > 2 && params[2] != "" {
sqlFilter = append(sqlFilter, "DoctorCode = '"+params[2]+"'")
}
if len(params) > 3 && params[3] != "" {
sqlFilter = append(sqlFilter, "PersonBirthDate = '"+params[3]+"'")
}
if len(params) > 4 && params[4] != "" {
sqlFilter = append(sqlFilter, "PersonTelecomValue = '"+params[4]+"'")
}
}
doctor_tb := `DoctorID, DoctorCode, DoctorIHSNumber, DoctorPersonID`
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 ` + doctor_tb + `,` + person_tb + `,` + person_identifier_tb + `,` + person_address_tb + `,` + person_telecom_tb +
` FROM doctor
JOIN person ON PersonID = DoctorPersonID
JOIN person_identifier ON PersonIdentifierPersonID = PersonID
JOIN person_telecom ON PersonTelecomPersonID = PersonID
JOIN person_address ON PersonAddressPersonID = PersonID
WHERE DoctorIsActive = 'Y'`
if len(sqlFilter) > 0 {
sql += " AND " + strings.Join(sqlFilter, " AND ")
}
sql += " LIMIT ? OFFSET ?"
if err := s.db.Select(&list_doctor, sql, perpage, offset); err != nil {
return nil, err
}
return list_doctor, nil
}