From 5ceca6af39fafad17d5f3123aec224484a1ea32d Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 3 May 2024 14:57:06 +0300 Subject: [PATCH] inital commit --- pkg/services/branchservice.go | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/pkg/services/branchservice.go b/pkg/services/branchservice.go index 5e568ea..c56dfab 100644 --- a/pkg/services/branchservice.go +++ b/pkg/services/branchservice.go @@ -1 +1,50 @@ package services + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "pkg.jojmatic.com/root/rpaas-core-service/pkg/models" +) + +type BranchService interface { + RegisterBranch(endpoint string, body *models.BranchRegisterBody) (map[string]interface{}, error) +} + +type BranchServiceImpl struct { + ctx context.Context +} + +func NewBranchService(ctx context.Context) *BranchServiceImpl { + return &BranchServiceImpl{ + ctx: ctx, + } +} + +func (bc *BranchServiceImpl) RegisterBranch(endpoint string, body *models.BranchRegisterBody) (map[string]interface{}, error) { + + url := endpoint + "v1/branch/register" + branchInfo, err := json.Marshal(body) + + resp, err := http.Post(url, "application/json", bytes.NewBuffer(branchInfo)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("register company failed, status code: %d", resp.StatusCode) + } + + var datajson map[string]interface{} + + data, _ := io.ReadAll(resp.Body) + if err := json.Unmarshal(data, &datajson); err != nil { + return nil, err + } + return datajson, err + +}