first commit
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
package location
|
||||
|
||||
// LocationSearchParams menampung kriteria pencarian Ruangan/Lokasi Satu Sehat
|
||||
type LocationSearchParams struct {
|
||||
Name string `form:"name"`
|
||||
Organization string `form:"organization"` // ID Faskes pembuat
|
||||
Identifier string `form:"identifier"` // Format {System}|{Value}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package location
|
||||
|
||||
// Konstanta standar untuk sistem identifier Location di Satu Sehat.
|
||||
const (
|
||||
IdentifierSystemLocation = "http://sys-ids.kemkes.go.id/location"
|
||||
PhysicalTypeSystem = "http://terminology.hl7.org/CodeSystem/location-physical-type"
|
||||
)
|
||||
@@ -0,0 +1,90 @@
|
||||
package location
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"service/internal/interfaces/satusehat"
|
||||
"service/pkg/errors"
|
||||
"service/pkg/logger"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
GetByID(ctx context.Context, id string) (map[string]interface{}, error)
|
||||
Search(ctx context.Context, params LocationSearchParams) (map[string]interface{}, error)
|
||||
Create(ctx context.Context, payload interface{}) (map[string]interface{}, error)
|
||||
Update(ctx context.Context, id string, payload interface{}) (map[string]interface{}, error)
|
||||
Patch(ctx context.Context, id string, payload interface{}) (map[string]interface{}, error)
|
||||
}
|
||||
|
||||
type repository struct {
|
||||
client satusehat.SatuSehatClient
|
||||
}
|
||||
|
||||
func NewRepository(client satusehat.SatuSehatClient) Repository {
|
||||
return &repository{client: client}
|
||||
}
|
||||
|
||||
func parseResponse(body []byte) (map[string]interface{}, error) {
|
||||
var res map[string]interface{}
|
||||
if err := json.Unmarshal(body, &res); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse SatuSehat response: %w", err)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (r *repository) GetByID(ctx context.Context, id string) (map[string]interface{}, error) {
|
||||
endpoint := fmt.Sprintf("/Location/%s", id)
|
||||
respBytes, err := r.client.DoRequest(ctx, "GET", endpoint, nil)
|
||||
if err != nil {
|
||||
logger.Default().Error("Gagal mencari Location berdasarkan ID", logger.ErrorField(err))
|
||||
return nil, errors.InternalError().Message("Gagal mencari Location").Cause(err).Build()
|
||||
}
|
||||
return parseResponse(respBytes)
|
||||
}
|
||||
|
||||
func (r *repository) Search(ctx context.Context, params LocationSearchParams) (map[string]interface{}, error) {
|
||||
q := url.Values{}
|
||||
if params.Name != "" {
|
||||
q.Add("name", params.Name)
|
||||
}
|
||||
if params.Organization != "" {
|
||||
q.Add("organization", params.Organization)
|
||||
}
|
||||
if params.Identifier != "" {
|
||||
q.Add("identifier", params.Identifier)
|
||||
}
|
||||
|
||||
endpoint := "/Location?" + q.Encode()
|
||||
respBytes, err := r.client.DoRequest(ctx, "GET", endpoint, nil)
|
||||
if err != nil {
|
||||
logger.Default().Error("Gagal mencari Location", logger.ErrorField(err))
|
||||
return nil, errors.InternalError().Message("Gagal melakukan pencarian Location").Cause(err).Build()
|
||||
}
|
||||
return parseResponse(respBytes)
|
||||
}
|
||||
|
||||
func (r *repository) Create(ctx context.Context, payload interface{}) (map[string]interface{}, error) {
|
||||
respBytes, err := r.client.DoRequest(ctx, "POST", "/Location", payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return parseResponse(respBytes)
|
||||
}
|
||||
|
||||
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (map[string]interface{}, error) {
|
||||
respBytes, err := r.client.DoRequest(ctx, "PUT", fmt.Sprintf("/Location/%s", id), payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return parseResponse(respBytes)
|
||||
}
|
||||
func (r *repository) Patch(ctx context.Context, id string, payload interface{}) (map[string]interface{}, error) {
|
||||
respBytes, err := r.client.DoRequest(ctx, "PATCH", fmt.Sprintf("/Location/%s", id), payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return parseResponse(respBytes)
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package location
|
||||
|
||||
import "context"
|
||||
|
||||
type Service interface {
|
||||
GetByID(ctx context.Context, id string) (map[string]interface{}, error)
|
||||
Search(ctx context.Context, params LocationSearchParams) (map[string]interface{}, error)
|
||||
Create(ctx context.Context, payload interface{}) (map[string]interface{}, error)
|
||||
Update(ctx context.Context, id string, payload interface{}) (map[string]interface{}, error)
|
||||
Patch(ctx context.Context, id string, payload interface{}) (map[string]interface{}, error)
|
||||
}
|
||||
|
||||
type service struct {
|
||||
repo Repository
|
||||
}
|
||||
|
||||
func NewService(repo Repository) Service {
|
||||
return &service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *service) GetByID(ctx context.Context, id string) (map[string]interface{}, error) {
|
||||
return s.repo.GetByID(ctx, id)
|
||||
}
|
||||
|
||||
func (s *service) Search(ctx context.Context, params LocationSearchParams) (map[string]interface{}, error) {
|
||||
return s.repo.Search(ctx, params)
|
||||
}
|
||||
|
||||
func (s *service) Create(ctx context.Context, payload interface{}) (map[string]interface{}, error) {
|
||||
return s.repo.Create(ctx, payload)
|
||||
}
|
||||
func (s *service) Update(ctx context.Context, id string, payload interface{}) (map[string]interface{}, error) {
|
||||
return s.repo.Update(ctx, id, payload)
|
||||
}
|
||||
func (s *service) Patch(ctx context.Context, id string, payload interface{}) (map[string]interface{}, error) {
|
||||
return s.repo.Patch(ctx, id, payload)
|
||||
}
|
||||
Reference in New Issue
Block a user