Sync old-to-new patient handler
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
package patient
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
rw "github.com/karincake/risoles"
|
||||
|
||||
p "simrs-vx/internal/domain/simgos-entities/patient"
|
||||
)
|
||||
|
||||
type myBase struct{}
|
||||
|
||||
var O myBase
|
||||
|
||||
const baseUrl string = "http://localhost:8000/v1/"
|
||||
|
||||
func (obj myBase) Create(w http.ResponseWriter, r *http.Request) {
|
||||
dto := p.MPasienDto{}
|
||||
if !rw.ValidateStructByIOR(w, r.Body, &dto) {
|
||||
return
|
||||
}
|
||||
// translate m_pasien ke Patient
|
||||
patient := dto.ToPatient()
|
||||
jsonPatient, err := json.Marshal(patient)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
// fmt.Println(string(jsonPatient))
|
||||
// kirim request ke api sim-baru
|
||||
reqBody := bytes.NewBuffer(jsonPatient)
|
||||
err = send(http.MethodPost, baseUrl+"patient", reqBody)
|
||||
if err != nil {
|
||||
fmt.Println("request error:", err)
|
||||
}
|
||||
|
||||
}
|
||||
func (obj myBase) Update(w http.ResponseWriter, r *http.Request) {
|
||||
dto := p.MPasienDto{}
|
||||
if !rw.ValidateStructByIOR(w, r.Body, &dto) {
|
||||
return
|
||||
}
|
||||
patient := dto.ToPatient()
|
||||
jsonPatient, err := json.Marshal(patient)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
reqBody := bytes.NewBuffer(jsonPatient)
|
||||
err = send(http.MethodPatch, fmt.Sprintf("%s%s%v", baseUrl, "patient/", patient.Id), reqBody)
|
||||
if err != nil {
|
||||
fmt.Println("request error:", err)
|
||||
}
|
||||
}
|
||||
func (obj myBase) Delete(w http.ResponseWriter, r *http.Request) {
|
||||
dto := p.MPasienDto{}
|
||||
if !rw.ValidateStructByIOR(w, r.Body, &dto) {
|
||||
return
|
||||
}
|
||||
patient := dto.ToPatient()
|
||||
jsonPatient, err := json.Marshal(patient)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
reqBody := bytes.NewBuffer(jsonPatient)
|
||||
err = send(http.MethodDelete, fmt.Sprintf("%s%s%v", baseUrl, "patient/", patient.Id), reqBody)
|
||||
if err != nil {
|
||||
fmt.Println("request error:", err)
|
||||
}
|
||||
}
|
||||
func (obj myBase) CreateLog(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
|
||||
func send(method string, url string, body *bytes.Buffer) error {
|
||||
req, err := http.NewRequest(method, url, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return nil
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
/******************** infra ********************/
|
||||
gs "simrs-vx/internal/infra/gorm-setting"
|
||||
simgosdb "simrs-vx/internal/infra/simgos-db"
|
||||
|
||||
/******************** pkg ********************/
|
||||
cmw "simrs-vx/pkg/cors-manager-mw"
|
||||
lh "simrs-vx/pkg/lang-helper"
|
||||
@@ -20,6 +21,7 @@ import (
|
||||
"simrs-vx/internal/interface/main-handler/home"
|
||||
division "simrs-vx/internal/interface/simgos-sync-handler/division"
|
||||
installation "simrs-vx/internal/interface/simgos-sync-handler/installation"
|
||||
oldPatient "simrs-vx/internal/interface/simgos-sync-handler/old/patient"
|
||||
patient "simrs-vx/internal/interface/simgos-sync-handler/patient"
|
||||
specialist "simrs-vx/internal/interface/simgos-sync-handler/specialist"
|
||||
subspecialist "simrs-vx/internal/interface/simgos-sync-handler/subspecialist"
|
||||
@@ -48,5 +50,8 @@ func SetRoutes() http.Handler {
|
||||
hc.SyncCrud(r, prefix+"/v1/patient", patient.O)
|
||||
r.HandleFunc(fmt.Sprintf("GET %s/v1/patient-nomr-generator", prefix), patient.O.GenerateNomr)
|
||||
|
||||
/*********** Source old-to-new ****************/
|
||||
oldPrefix := "/old-to-new"
|
||||
hc.SyncCrud(r, oldPrefix+"/v1/patient", oldPatient.O)
|
||||
return cmw.SetCors(handlerlogger.SetLog(r))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user