From b780bb569bc623a36c81db6644cd1f4e43170542 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 3 May 2024 15:41:51 +0300 Subject: [PATCH] first modify --- pkg/models/branch.go | 13 ++++++++ pkg/services/branchservice.go | 61 +++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/pkg/models/branch.go b/pkg/models/branch.go index 98c1b26..407b25a 100644 --- a/pkg/models/branch.go +++ b/pkg/models/branch.go @@ -1,6 +1,19 @@ package models +import ( + "time" +) + type BranchRegisterBody struct { BranchName string `json:"branch_name" validate:"required"` CompanyID string `json:"company_id" bson:"company_id"` } + +type BranchInfo struct { + BranchName string `json:"branch_name" bson:"branch_name"` + BranchID string `json:"branch_id" bson:"branch_id"` + CompanyID string `json:"company_id" bson:"company_id"` + Owner []string `json:"owner" bson:"owner"` + CreateAt time.Time `json:"create_at" bson:"create_at"` + UpdateAt time.Time `json:"update_at" bson:"update_at"` +} diff --git a/pkg/services/branchservice.go b/pkg/services/branchservice.go index c56dfab..9967bf9 100644 --- a/pkg/services/branchservice.go +++ b/pkg/services/branchservice.go @@ -1,50 +1,57 @@ package services import ( - "bytes" "context" - "encoding/json" - "fmt" - "io" - "net/http" + "errors" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo" + "pkg.jojmatic.com/root/rpaas-core-service/pkg/logging" "pkg.jojmatic.com/root/rpaas-core-service/pkg/models" + "time" ) type BranchService interface { - RegisterBranch(endpoint string, body *models.BranchRegisterBody) (map[string]interface{}, error) + RegisterBranch(body *models.BranchRegisterBody, info *models.CompanyInfo) (*models.BranchInfo, error) } type BranchServiceImpl struct { - ctx context.Context + branchcollection *mongo.Collection + ctx context.Context } -func NewBranchService(ctx context.Context) *BranchServiceImpl { +func NewBranchService(collection *mongo.Collection, ctx context.Context) *BranchServiceImpl { return &BranchServiceImpl{ - ctx: ctx, + branchcollection: collection, + ctx: ctx, } } -func (bc *BranchServiceImpl) RegisterBranch(endpoint string, body *models.BranchRegisterBody) (map[string]interface{}, error) { +func (bc *BranchServiceImpl) RegisterBranch(body *models.BranchRegisterBody, info *models.CompanyInfo) (*models.BranchInfo, error) { - url := endpoint + "v1/branch/register" - branchInfo, err := json.Marshal(body) + var branchexisting *models.BranchInfo + err := bc.branchcollection.FindOne(bc.ctx, bson.M{"$and": []bson.M{{"branch_name": body.BranchName}, {"company_id": body.CompanyID}}}).Decode(&branchexisting) + if err == nil { + logging.Logger().Warn("Branch name already exists", err) + return nil, errors.New("branch name already exists") + } else if !errors.Is(err, mongo.ErrNoDocuments) { + logging.Logger().Error(err) + return nil, errors.New("InternalServerError") + } - resp, err := http.Post(url, "application/json", bytes.NewBuffer(branchInfo)) + branchInfo := &models.BranchInfo{ + BranchName: body.BranchName, + BranchID: primitive.NewObjectID().Hex(), + CompanyID: body.CompanyID, + Owner: info.Owner, + CreateAt: time.Now().UTC().Round(time.Second), + UpdateAt: time.Now().UTC().Round(time.Second), + } + + _, err = bc.branchcollection.InsertOne(bc.ctx, branchInfo) if err != nil { + logging.Logger().Error(err) 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 - + return branchInfo, nil }