94 lines
3.1 KiB
Go
94 lines
3.1 KiB
Go
package imagingstudy
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/url"
|
|
"service/internal/interfaces/satusehat"
|
|
"service/pkg/errors"
|
|
)
|
|
|
|
// Repository mendefinisikan kontrak untuk operasi penyimpanan data ImagingStudy.
|
|
type Repository interface {
|
|
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
|
|
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
|
|
Patch(ctx context.Context, id string, payload ImagingStudyPatchRequest) (*satusehat.FHIRResponse, error)
|
|
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
|
|
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
|
|
}
|
|
|
|
type repository struct {
|
|
client satusehat.SatuSehatClient
|
|
}
|
|
|
|
// NewRepository membuat repositori ImagingStudy baru.
|
|
func NewRepository(client satusehat.SatuSehatClient) Repository {
|
|
return &repository{client: client}
|
|
}
|
|
|
|
// hiddenCtx membungkus context untuk mencegah *HTTP client*
|
|
// melakukan type-assertion ke *gin.Context dan melakukan auto-print error.
|
|
type hiddenCtx struct {
|
|
context.Context
|
|
}
|
|
|
|
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
|
|
resp, err := r.client.DoRequest(hiddenCtx{ctx}, method, endpoint, req)
|
|
if err != nil {
|
|
return nil, errors.InternalError().
|
|
Message("Failed to execute request to SatuSehat").
|
|
Cause(err).
|
|
Build()
|
|
}
|
|
|
|
var result map[string]interface{}
|
|
if err := json.Unmarshal(resp, &result); err != nil {
|
|
return nil, errors.InternalError().
|
|
Message("Failed to parse SatuSehat response").
|
|
Cause(err).
|
|
Metadata("raw_response", string(resp)).
|
|
Build()
|
|
}
|
|
|
|
// Tangkap respons OperationOutcome dan serahkan ke Parser terpusat
|
|
if resourceType, ok := result["resourceType"].(string); ok && resourceType == "OperationOutcome" {
|
|
return nil, errors.ParseSatuSehatError(result)
|
|
}
|
|
|
|
var resourceID string
|
|
if id, ok := result["id"].(string); ok {
|
|
resourceID = id
|
|
}
|
|
|
|
return &satusehat.FHIRResponse{
|
|
ID: resourceID,
|
|
FullResponse: result,
|
|
RawResponse: resp,
|
|
}, nil
|
|
}
|
|
|
|
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
|
|
return r.executeRequest(ctx, "POST", "/ImagingStudy", payload)
|
|
}
|
|
|
|
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
|
|
endpoint := fmt.Sprintf("/ImagingStudy/%s", id)
|
|
return r.executeRequest(ctx, "PUT", endpoint, payload)
|
|
}
|
|
|
|
func (r *repository) Patch(ctx context.Context, id string, payload ImagingStudyPatchRequest) (*satusehat.FHIRResponse, error) {
|
|
endpoint := fmt.Sprintf("/ImagingStudy/%s", id)
|
|
return r.executeRequest(ctx, "PATCH", endpoint, payload)
|
|
}
|
|
|
|
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
|
|
endpoint := fmt.Sprintf("/ImagingStudy/%s", id)
|
|
return r.executeRequest(ctx, "GET", endpoint, nil)
|
|
}
|
|
|
|
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
|
|
endpoint := fmt.Sprintf("/ImagingStudy?%s", queryParams.Encode())
|
|
return r.executeRequest(ctx, "GET", endpoint, nil)
|
|
}
|