89 lines
2.0 KiB
Go
89 lines
2.0 KiB
Go
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
|
|
}
|