Files
simrsx-be/internal/interface/simgos-sync-handler/old/patient/handler.go
T
2025-11-28 19:43:53 +07:00

107 lines
2.5 KiB
Go

package patient
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
p "simrs-vx/internal/domain/simgos-entities/m-pasien"
cfg "simrs-vx/internal/infra/sync-cfg"
rw "github.com/karincake/risoles"
)
type myBase struct{}
var O myBase
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, "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()
// TODO DELETE BELOW,
var (
personId uint = 68
patientId uint = 34
)
patient.Person_Id = &personId
patient.Id = patientId
patient.Person.Id = personId
// TODO DELETE ABOVE
jsonPatient, err := json.MarshalIndent(patient, "", " ")
if err != nil {
fmt.Println(err)
}
fmt.Println(string(jsonPatient))
url := fmt.Sprintf("%s%v", "patient/", patient.Id)
fmt.Println(url)
reqBody := bytes.NewBuffer(jsonPatient)
err = send(http.MethodPatch, url, 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%v", "patient/", *patient.Person_Id), reqBody)
if err != nil {
fmt.Println("request error:", err)
}
}
func (obj myBase) CreateLog(w http.ResponseWriter, r *http.Request) {
}
func send(method string, endpoint string, body *bytes.Buffer) error {
var url string = cfg.O.NewHost + endpoint
req, err := http.NewRequest(method, url, body)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Sync-Source", cfg.O.OldSource)
req.Header.Set("X-Sync-SecretKey", cfg.O.NewSecretKey)
req.Header.Set("X-Sync-UserName", "dave")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
respBody, _ := io.ReadAll(resp.Body)
fmt.Println(resp.StatusCode, string(respBody), method, url)
return nil
}