first commit
This commit is contained in:
57
services/person/person.routes.go
Normal file
57
services/person/person.routes.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package person
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"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.PersonStore
|
||||
terminology types.TerminologyStore
|
||||
}
|
||||
|
||||
func NewHandler(store types.PersonStore, terminology types.TerminologyStore) *Handler {
|
||||
return &Handler{store: store, terminology: terminology}
|
||||
}
|
||||
|
||||
func (h *Handler) RegisterRoutes(router *mux.Router) {
|
||||
personroute := router.PathPrefix("/mdperson").Subrouter()
|
||||
personroute.Use(auth.AuthMiddleware)
|
||||
|
||||
personroute.HandleFunc("/get/{personID}", h.handleGetPersonByID).Methods(http.MethodGet)
|
||||
}
|
||||
|
||||
func (h *Handler) handleGetPersonByID(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
param, ok := vars["personID"]
|
||||
if !ok {
|
||||
utils.WriteError(w, http.StatusBadRequest, fmt.Errorf("missing params person id"))
|
||||
return
|
||||
}
|
||||
|
||||
personID, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
utils.WriteError(w, http.StatusBadRequest, fmt.Errorf("invalid params person id"))
|
||||
return
|
||||
}
|
||||
|
||||
person, err := h.store.GetPersonByID(personID)
|
||||
if err != nil {
|
||||
utils.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
json, err := h.getDisplayTerminology(*person)
|
||||
if err != nil {
|
||||
utils.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
utils.WriteJSON(w, http.StatusOK, json)
|
||||
}
|
||||
66
services/person/person.services.go
Normal file
66
services/person/person.services.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package person
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"sismedika.com/sas/westone/types"
|
||||
"sismedika.com/sas/westone/utils"
|
||||
)
|
||||
|
||||
func (h *Handler) getDisplayTerminology(person types.Person) (*types.PersonJSON, error) {
|
||||
ret := new(types.PersonJSON)
|
||||
err := utils.MatchStruct(person, ret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if person.PersonBloodTypeCode != "" {
|
||||
blood, err := h.terminology.GetTerminologyDisplay(person.PersonBloodTypeCode, person.PersonBloodTypeSystem)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("blood")
|
||||
}
|
||||
ret.PersonBlood = *blood
|
||||
}
|
||||
|
||||
if person.PersonEducationCode != "" {
|
||||
education, err := h.terminology.GetTerminologyDisplay(person.PersonEducationCode, person.PersonEducationSystem)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("edu")
|
||||
}
|
||||
ret.PersonEducation = *education
|
||||
}
|
||||
|
||||
if person.PersonEtnicityCode != "" {
|
||||
etnicity, err := h.terminology.GetTerminologyDisplay(person.PersonEtnicityCode, person.PersonEtnicitySystem)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("etni")
|
||||
}
|
||||
ret.PersonEtnicity = *etnicity
|
||||
}
|
||||
|
||||
if person.PersonJobClassCode != "" {
|
||||
job, err := h.terminology.GetTerminologyDisplay(person.PersonJobClassCode, person.PersonJobClassSystem)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("job")
|
||||
}
|
||||
ret.PersonJobClass = *job
|
||||
}
|
||||
|
||||
if person.PersonReligionCode != "" {
|
||||
religion, err := h.terminology.GetTerminologyDisplay(person.PersonReligionCode, person.PersonReligionSystem)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reli")
|
||||
}
|
||||
ret.PersonReligion = *religion
|
||||
}
|
||||
|
||||
if person.PersonRhesusCode != "" {
|
||||
rhesus, err := h.terminology.GetTerminologyDisplay(person.PersonRhesusCode, person.PersonRhesusSystem)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("rhes")
|
||||
}
|
||||
ret.PersonRhesus = *rhesus
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
50
services/person/person.store.go
Normal file
50
services/person/person.store.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package person
|
||||
|
||||
import (
|
||||
"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) GetPersonByID(id int) (*types.Person, error) {
|
||||
person := new(types.Person)
|
||||
|
||||
qry := `
|
||||
SELECT
|
||||
PersonID,
|
||||
PersonName,
|
||||
PersonSalutation,
|
||||
PersonPrefix,
|
||||
PersonSuffix,
|
||||
PersonGender,
|
||||
PersonBirthPlace,
|
||||
PersonBirthDate,
|
||||
PersonReligionCode,
|
||||
PersonReligionSystem,
|
||||
PersonBloodTypeCode,
|
||||
PersonBloodTypeSystem,
|
||||
PersonRhesusCode,
|
||||
PersonRhesusSystem,
|
||||
PersonEducationCode,
|
||||
PersonEducationSystem,
|
||||
PersonJobClassCode,
|
||||
PersonJobClassSystem,
|
||||
PersonEtnicityCode,
|
||||
PersonEtnicitySystem,
|
||||
PersonMaritalBirth,
|
||||
PersonMaritalStatus
|
||||
FROM person WHERE PersonID = ?
|
||||
`
|
||||
if err := s.db.Get(person, qry, id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return person, nil
|
||||
}
|
||||
Reference in New Issue
Block a user