From 9c1e42c8378d4b2eda34c0fd834e98ad1c36ae31 Mon Sep 17 00:00:00 2001 From: "mallikarjun.b-r" Date: Fri, 26 Mar 2021 11:13:24 +0530 Subject: [PATCH] Split Repository Based On Entities --- main.go | 2 +- repository/db.go | 40 +---- repository/postgres.go | 154 ++---------------- repository/service_request_repository.go | 35 ++++ .../service_request_repository_postgres.go | 45 +++++ repository/step_status_repository.go | 38 +++++ repository/step_status_repository_postgres.go | 67 ++++++++ repository/work_flow_repository.go | 34 ++++ repository/work_flow_repository_postgres.go | 57 +++++++ services/abstract_test.go | 36 ++-- services/async_resume_worker.go | 2 +- services/async_resume_worker_test.go | 3 + services/service_request_service.go | 6 +- services/service_request_service_test.go | 3 +- services/step_status_service.go | 8 +- services/workflow_service.go | 8 +- 16 files changed, 328 insertions(+), 210 deletions(-) create mode 100644 repository/service_request_repository.go create mode 100644 repository/service_request_repository_postgres.go create mode 100644 repository/step_status_repository.go create mode 100644 repository/step_status_repository_postgres.go create mode 100644 repository/work_flow_repository.go create mode 100644 repository/work_flow_repository_postgres.go diff --git a/main.go b/main.go index e0c1b57..6070bb6 100644 --- a/main.go +++ b/main.go @@ -22,7 +22,7 @@ func main() { log.SetLevel(logLevel) log.Info("Pinging DB...") - err = repository.GetDB().Ping() + err = repository.Ping() if err != nil { log.Fatalf("DB ping failed: %s", err) } diff --git a/repository/db.go b/repository/db.go index b174f67..7ff19d8 100644 --- a/repository/db.go +++ b/repository/db.go @@ -2,45 +2,13 @@ package repository import ( "clamp-core/config" - "clamp-core/models" - - "github.com/google/uuid" + "fmt" ) -// DBInterface provides a collection of method signatures that needs to be implemented for a specific database. -type DBInterface interface { - SaveServiceRequest(*models.ServiceRequest) (*models.ServiceRequest, error) - FindServiceRequestByID(uuid.UUID) (*models.ServiceRequest, error) - SaveWorkflow(*models.Workflow) (*models.Workflow, error) - FindWorkflowByName(string) (*models.Workflow, error) - SaveStepStatus(*models.StepsStatus) (*models.StepsStatus, error) - FindStepStatusByServiceRequestID(serviceRequestID uuid.UUID) ([]*models.StepsStatus, error) - FindStepStatusByServiceRequestIDAndStatus(serviceRequestID uuid.UUID, status models.Status) ([]*models.StepsStatus, error) - FindStepStatusByServiceRequestIDAndStepIDAndStatus( - serviceRequestID uuid.UUID, stepID int, status models.Status) (*models.StepsStatus, error) - FindAllStepStatusByServiceRequestIDAndStepID(serviceRequestID uuid.UUID, stepID int) ([]*models.StepsStatus, error) - GetWorkflows(pageNumber int, pageSize int, sortBy models.SortByFields) ([]*models.Workflow, int, error) - FindServiceRequestsByWorkflowName(workflowName string, pageNumber int, pageSize int) ([]*models.ServiceRequest, error) - DeleteWorkflowByName(string) error - Ping() error -} - -var db DBInterface - -func init() { +func Ping() error { switch config.ENV.DBDriver { case "postgres": - db = &postgres{} + return pgDB.Ping() } -} - -// GetDB returns the initialized database implementations. Currently only postgres is implemented. -func GetDB() DBInterface { - return db -} - -// SetDB is used to update the db object with custom implementations. -// It is used in tests to override the actual db implementations with mock implementations -func SetDB(dbImpl DBInterface) { - db = dbImpl + return fmt.Errorf("Unsupported Database %s", config.ENV.DBDriver) } diff --git a/repository/postgres.go b/repository/postgres.go index a85d3e2..3413f70 100644 --- a/repository/postgres.go +++ b/repository/postgres.go @@ -2,21 +2,15 @@ package repository import ( "clamp-core/config" - "clamp-core/models" "context" - "errors" "strings" "sync" "github.com/go-pg/pg/v9" - "github.com/google/uuid" log "github.com/sirupsen/logrus" ) -// reference human readable keys to DB key values -var keyReferences = map[string]string{"id": "id", "created_at": "created_at", "name": "name"} - var singletonOnce sync.Once type dbLogger struct{} @@ -65,148 +59,17 @@ func GetPostgresOptions() *pg.Options { } } -type postgres struct { +type postgresDB struct { db *pg.DB } -func (p *postgres) FindServiceRequestsByWorkflowName(workflowName string, pageNumber int, pageSize int) ([]*models.ServiceRequest, error) { - var pgServiceRequests []models.PGServiceRequest - err := p.getDB().Model(&pgServiceRequests). - Where("workflow_name = ?", workflowName). - Offset(pageSize * pageNumber). - Limit(pageSize). - Select() - var workflows []*models.ServiceRequest - if err == nil { - for _, pgServiceRequest := range pgServiceRequests { - workflows = append(workflows, pgServiceRequest.ToServiceRequest()) - } - } - return workflows, err -} - -func (p *postgres) FindAllStepStatusByServiceRequestIDAndStepID(serviceRequestID uuid.UUID, stepID int) ([]*models.StepsStatus, error) { - var pgStepStatus []models.PGStepStatus - err := p.getDB().Model(&pgStepStatus).Where("service_request_id = ? and step_id = ?", serviceRequestID, stepID).Select() - var stepStatuses []*models.StepsStatus - if err == nil { - for i := range pgStepStatus { - stepStatuses = append(stepStatuses, pgStepStatus[i].ToStepStatus()) - } - } - return stepStatuses, err -} - -func (p *postgres) FindStepStatusByServiceRequestIDAndStepIDAndStatus( - serviceRequestID uuid.UUID, stepID int, status models.Status) (*models.StepsStatus, error) { - var pgStepStatus models.PGStepStatus - var stepStatuses models.StepsStatus - err := p.getDB().Model(&pgStepStatus).Where("service_request_id = ? and step_id = ? and status = ?", - serviceRequestID, stepID, status).Select() - if err != nil { - return &stepStatuses, err - } - return pgStepStatus.ToStepStatus(), err -} - -func (p *postgres) FindStepStatusByServiceRequestIDAndStatus( - serviceRequestID uuid.UUID, status models.Status) ([]*models.StepsStatus, error) { - var pgStepStatus []models.PGStepStatus - var stepStatuses []*models.StepsStatus - err := p.getDB().Model(&pgStepStatus).Where("service_request_id = ? and status = ?", serviceRequestID, status). - Order("created_at ASC").Select() - if err != nil { - return stepStatuses, err - } - for i := range pgStepStatus { - stepStatuses = append(stepStatuses, pgStepStatus[i].ToStepStatus()) - } - return stepStatuses, err -} - -func (p *postgres) FindStepStatusByServiceRequestID(serviceRequestID uuid.UUID) ([]*models.StepsStatus, error) { - var pgStepStatus []models.PGStepStatus - err := p.getDB().Model(&pgStepStatus).Where("service_request_id = ?", serviceRequestID).Order("created_at ASC").Select() - var stepStatuses []*models.StepsStatus - if err == nil { - for i := range pgStepStatus { - stepStatuses = append(stepStatuses, pgStepStatus[i].ToStepStatus()) - } - } - return stepStatuses, err -} - -func (p *postgres) SaveStepStatus(stepStatus *models.StepsStatus) (*models.StepsStatus, error) { - pgStepStatusReq := stepStatus.ToPgStepStatus() - err := p.getDB().Insert(pgStepStatusReq) - return pgStepStatusReq.ToStepStatus(), err -} - -func (p *postgres) FindWorkflowByName(workflowName string) (*models.Workflow, error) { - pgWorkflow := new(models.PGWorkflow) - err := p.getDB().Model(pgWorkflow).Where("name = ?", workflowName).Select() - return pgWorkflow.ToWorkflow(), err -} - -func (p *postgres) DeleteWorkflowByName(workflowName string) error { - _, err := p.getDB().Model((*models.PGWorkflow)(nil)).Where("name = ?", workflowName).Delete() - return err -} - -func (p *postgres) SaveWorkflow(workflowReq *models.Workflow) (*models.Workflow, error) { - pgWorkflow := workflowReq.ToPGWorkflow() - log.Debugf("pgworfklow: %v", pgWorkflow) - err := p.getDB().Insert(pgWorkflow) - return pgWorkflow.ToWorkflow(), err -} - -func (p *postgres) FindServiceRequestByID(serviceRequestID uuid.UUID) (*models.ServiceRequest, error) { - pgServiceRequest := &models.PGServiceRequest{ID: serviceRequestID} - err := p.getDB().Select(pgServiceRequest) - if err != nil { - panic(err) - } - return pgServiceRequest.ToServiceRequest(), err -} - -func (p *postgres) SaveServiceRequest(serviceReq *models.ServiceRequest) (*models.ServiceRequest, error) { - pgServReq := serviceReq.ToPgServiceRequest() - db := p.getDB() - err := db.Insert(pgServReq) - return pgServReq.ToServiceRequest(), err -} +var pgDB postgresDB -func (p *postgres) GetWorkflows(pageNumber int, pageSize int, sortFields models.SortByFields) ([]*models.Workflow, int, error) { - var pgWorkflows []models.PGWorkflow - query := p.getDB().Model(&pgWorkflows) - for _, sortField := range sortFields { - reference, found := keyReferences[sortField.Key] - if !found { - return []*models.Workflow{}, 0, errors.New("undefined key reference used") - } - order := sortField.Order - if found { - query = query.Order(reference + " " + order) - } - } - totalWorkflowsCount, err := query.Offset(pageSize * (pageNumber - 1)). - Limit(pageSize).SelectAndCount() - if err != nil { - return []*models.Workflow{}, 0, err - } - var workflows []*models.Workflow - for i := range pgWorkflows { - workflows = append(workflows, pgWorkflows[i].ToWorkflow()) - } - return workflows, totalWorkflowsCount, err +func init() { + pgDB = postgresDB{} } -func (p *postgres) Ping() error { - _, err := p.getDB().Exec("SELECT 1") - return err -} - -func (p *postgres) getDB() *pg.DB { +func (p *postgresDB) GetDB() *pg.DB { singletonOnce.Do(func() { log.Info("Connecting to DB") p.db = connectDB() @@ -214,7 +77,12 @@ func (p *postgres) getDB() *pg.DB { return p.db } -func (p *postgres) closeDB() { +func (p *postgresDB) Ping() error { + _, err := p.GetDB().Exec("SELECT 1") + return err +} + +func (p *postgresDB) closeDB() { if p.db != nil { log.Info("Disconnecting from DB") p.db.Close() diff --git a/repository/service_request_repository.go b/repository/service_request_repository.go new file mode 100644 index 0000000..121d816 --- /dev/null +++ b/repository/service_request_repository.go @@ -0,0 +1,35 @@ +package repository + +import ( + "clamp-core/config" + "clamp-core/models" + + "github.com/google/uuid" +) + +// DBInterface provides a collection of method signatures that needs to be implemented for a specific database. +type ServiceRequestRepository interface { + SaveServiceRequest(*models.ServiceRequest) (*models.ServiceRequest, error) + FindServiceRequestByID(uuid.UUID) (*models.ServiceRequest, error) + FindServiceRequestsByWorkflowName(workflowName string, pageNumber int, pageSize int) ([]*models.ServiceRequest, error) +} + +var serviceRequestRepository ServiceRequestRepository + +func init() { + switch config.ENV.DBDriver { + case "postgres": + serviceRequestRepository = &servicerequestrepositorypostgres{} + } +} + +// GetDB returns the initialized database implementations. Currently only postgres is implemented. +func GetServiceRequestRepository() ServiceRequestRepository { + return serviceRequestRepository +} + +// SetDB is used to update the db object with custom implementations. +// It is used in tests to override the actual db implementations with mock implementations +func SetServiceRequestRepository(serviceRequestRepositoryImpl ServiceRequestRepository) { + serviceRequestRepository = serviceRequestRepositoryImpl +} diff --git a/repository/service_request_repository_postgres.go b/repository/service_request_repository_postgres.go new file mode 100644 index 0000000..58c940c --- /dev/null +++ b/repository/service_request_repository_postgres.go @@ -0,0 +1,45 @@ +package repository + +import ( + "clamp-core/models" + + "github.com/google/uuid" + "github.com/prometheus/common/log" +) + +type servicerequestrepositorypostgres struct { +} + +func (servicerequestrepository *servicerequestrepositorypostgres) SaveServiceRequest(serviceReq *models.ServiceRequest) (*models.ServiceRequest, error) { + pgServReq := serviceReq.ToPgServiceRequest() + db := pgDB.GetDB() + err := db.Insert(pgServReq) + return pgServReq.ToServiceRequest(), err +} + +func (servicerequestrepository *servicerequestrepositorypostgres) FindServiceRequestsByWorkflowName(workflowName string, pageNumber int, pageSize int) ([]*models.ServiceRequest, error) { + var pgServiceRequests []models.PGServiceRequest + err := pgDB.GetDB().Model(&pgServiceRequests). + Where("WORKFLOW_NAME = ?", workflowName). + Offset(pageSize * pageNumber). + Limit(pageSize). + Select() + var workflows []*models.ServiceRequest + log.Info("workflow name %s %v", workflowName, pgServiceRequests) + + if err == nil { + for _, pgServiceRequest := range pgServiceRequests { + workflows = append(workflows, pgServiceRequest.ToServiceRequest()) + } + } + return workflows, err +} + +func (servicerequestrepository *servicerequestrepositorypostgres) FindServiceRequestByID(serviceRequestID uuid.UUID) (*models.ServiceRequest, error) { + pgServiceRequest := &models.PGServiceRequest{ID: serviceRequestID} + err := pgDB.GetDB().Select(pgServiceRequest) + if err != nil { + panic(err) + } + return pgServiceRequest.ToServiceRequest(), err +} diff --git a/repository/step_status_repository.go b/repository/step_status_repository.go new file mode 100644 index 0000000..bf0511b --- /dev/null +++ b/repository/step_status_repository.go @@ -0,0 +1,38 @@ +package repository + +import ( + "clamp-core/config" + "clamp-core/models" + + "github.com/google/uuid" +) + +// DBInterface provides a collection of method signatures that needs to be implemented for a specific database. +type StepStatusRepository interface { + SaveStepStatus(*models.StepsStatus) (*models.StepsStatus, error) + FindStepStatusByServiceRequestID(serviceRequestID uuid.UUID) ([]*models.StepsStatus, error) + FindStepStatusByServiceRequestIDAndStatus(serviceRequestID uuid.UUID, status models.Status) ([]*models.StepsStatus, error) + FindStepStatusByServiceRequestIDAndStepIDAndStatus( + serviceRequestID uuid.UUID, stepID int, status models.Status) (*models.StepsStatus, error) + FindAllStepStatusByServiceRequestIDAndStepID(serviceRequestID uuid.UUID, stepID int) ([]*models.StepsStatus, error) +} + +var stepStatusRepository StepStatusRepository + +func init() { + switch config.ENV.DBDriver { + case "postgres": + stepStatusRepository = &stepstatusrepositorypostgres{} + } +} + +// GetDB returns the initialized database implementations. Currently only postgres is implemented. +func GetStepStatusRepository() StepStatusRepository { + return stepStatusRepository +} + +// SetDB is used to update the db object with custom implementations. +// It is used in tests to override the actual db implementations with mock implementations +func SetStepStatusRepository(stepStatusRepository StepStatusRepository) { + stepStatusRepository = stepStatusRepository +} diff --git a/repository/step_status_repository_postgres.go b/repository/step_status_repository_postgres.go new file mode 100644 index 0000000..83af001 --- /dev/null +++ b/repository/step_status_repository_postgres.go @@ -0,0 +1,67 @@ +package repository + +import ( + "clamp-core/models" + + "github.com/google/uuid" +) + +type stepstatusrepositorypostgres struct { +} + +func (stepstatusrepository *stepstatusrepositorypostgres) FindAllStepStatusByServiceRequestIDAndStepID(serviceRequestID uuid.UUID, stepID int) ([]*models.StepsStatus, error) { + var pgStepStatus []models.PGStepStatus + err := pgDB.GetDB().Model(&pgStepStatus).Where("service_request_id = ? and step_id = ?", serviceRequestID, stepID).Select() + var stepStatuses []*models.StepsStatus + if err == nil { + for i := range pgStepStatus { + stepStatuses = append(stepStatuses, pgStepStatus[i].ToStepStatus()) + } + } + return stepStatuses, err +} + +func (stepstatusrepository *stepstatusrepositorypostgres) FindStepStatusByServiceRequestIDAndStepIDAndStatus( + serviceRequestID uuid.UUID, stepID int, status models.Status) (*models.StepsStatus, error) { + var pgStepStatus models.PGStepStatus + var stepStatuses models.StepsStatus + err := pgDB.GetDB().Model(&pgStepStatus).Where("service_request_id = ? and step_id = ? and status = ?", + serviceRequestID, stepID, status).Select() + if err != nil { + return &stepStatuses, err + } + return pgStepStatus.ToStepStatus(), err +} + +func (stepstatusrepository *stepstatusrepositorypostgres) FindStepStatusByServiceRequestIDAndStatus( + serviceRequestID uuid.UUID, status models.Status) ([]*models.StepsStatus, error) { + var pgStepStatus []models.PGStepStatus + var stepStatuses []*models.StepsStatus + err := pgDB.GetDB().Model(&pgStepStatus).Where("service_request_id = ? and status = ?", serviceRequestID, status). + Order("created_at ASC").Select() + if err != nil { + return stepStatuses, err + } + for i := range pgStepStatus { + stepStatuses = append(stepStatuses, pgStepStatus[i].ToStepStatus()) + } + return stepStatuses, err +} + +func (stepstatusrepository *stepstatusrepositorypostgres) FindStepStatusByServiceRequestID(serviceRequestID uuid.UUID) ([]*models.StepsStatus, error) { + var pgStepStatus []models.PGStepStatus + err := pgDB.GetDB().Model(&pgStepStatus).Where("service_request_id = ?", serviceRequestID).Order("created_at ASC").Select() + var stepStatuses []*models.StepsStatus + if err == nil { + for i := range pgStepStatus { + stepStatuses = append(stepStatuses, pgStepStatus[i].ToStepStatus()) + } + } + return stepStatuses, err +} + +func (stepstatusrepository *stepstatusrepositorypostgres) SaveStepStatus(stepStatus *models.StepsStatus) (*models.StepsStatus, error) { + pgStepStatusReq := stepStatus.ToPgStepStatus() + err := pgDB.GetDB().Insert(pgStepStatusReq) + return pgStepStatusReq.ToStepStatus(), err +} diff --git a/repository/work_flow_repository.go b/repository/work_flow_repository.go new file mode 100644 index 0000000..31e5fad --- /dev/null +++ b/repository/work_flow_repository.go @@ -0,0 +1,34 @@ +package repository + +import ( + "clamp-core/config" + "clamp-core/models" +) + +// DBInterface provides a collection of method signatures that needs to be implemented for a specific database. +type WorkFlowRepository interface { + SaveWorkflow(*models.Workflow) (*models.Workflow, error) + FindWorkflowByName(string) (*models.Workflow, error) + GetWorkflows(pageNumber int, pageSize int, sortBy models.SortByFields) ([]*models.Workflow, int, error) + DeleteWorkflowByName(string) error +} + +var workFlowRepository WorkFlowRepository + +func init() { + switch config.ENV.DBDriver { + case "postgres": + workFlowRepository = &workflowrepositorypostgres{} + } +} + +// GetDB returns the initialized database implementations. Currently only postgres is implemented. +func GetWorkFlowRepository() WorkFlowRepository { + return workFlowRepository +} + +// SetDB is used to update the db object with custom implementations. +// It is used in tests to override the actual db implementations with mock implementations +func SetWorkFlowRepository(workFlowRepositoryImpl WorkFlowRepository) { + workFlowRepository = workFlowRepositoryImpl +} diff --git a/repository/work_flow_repository_postgres.go b/repository/work_flow_repository_postgres.go new file mode 100644 index 0000000..be45be2 --- /dev/null +++ b/repository/work_flow_repository_postgres.go @@ -0,0 +1,57 @@ +package repository + +import ( + "clamp-core/models" + "errors" + + "github.com/prometheus/common/log" +) + +type workflowrepositorypostgres struct { +} + +var keyReferences = map[string]string{"id": "id", "created_at": "created_at", "name": "name"} + +func (workflowrepository *workflowrepositorypostgres) FindWorkflowByName(workflowName string) (*models.Workflow, error) { + pgWorkflow := new(models.PGWorkflow) + + err := pgDB.GetDB().Model(pgWorkflow).Where("name = ?", workflowName).Select() + return pgWorkflow.ToWorkflow(), err +} + +func (workflowrepository *workflowrepositorypostgres) DeleteWorkflowByName(workflowName string) error { + _, err := pgDB.GetDB().Model((*models.PGWorkflow)(nil)).Where("name = ?", workflowName).Delete() + return err +} + +func (workflowrepository *workflowrepositorypostgres) SaveWorkflow(workflowReq *models.Workflow) (*models.Workflow, error) { + pgWorkflow := workflowReq.ToPGWorkflow() + log.Debugf("pgworfklow: %v", pgWorkflow) + err := pgDB.GetDB().Insert(pgWorkflow) + return pgWorkflow.ToWorkflow(), err +} + +func (workflowrepository *workflowrepositorypostgres) GetWorkflows(pageNumber int, pageSize int, sortFields models.SortByFields) ([]*models.Workflow, int, error) { + var pgWorkflows []models.PGWorkflow + query := pgDB.GetDB().Model(&pgWorkflows) + for _, sortField := range sortFields { + reference, found := keyReferences[sortField.Key] + if !found { + return []*models.Workflow{}, 0, errors.New("undefined key reference used") + } + order := sortField.Order + if found { + query = query.Order(reference + " " + order) + } + } + totalWorkflowsCount, err := query.Offset(pageSize * (pageNumber - 1)). + Limit(pageSize).SelectAndCount() + if err != nil { + return []*models.Workflow{}, 0, err + } + var workflows []*models.Workflow + for i := range pgWorkflows { + workflows = append(workflows, pgWorkflows[i].ToWorkflow()) + } + return workflows, totalWorkflowsCount, err +} diff --git a/services/abstract_test.go b/services/abstract_test.go index 52555f6..a6db4ae 100644 --- a/services/abstract_test.go +++ b/services/abstract_test.go @@ -7,25 +7,27 @@ import ( "github.com/google/uuid" ) -type mockDB struct{} +type mockDBServiceRequest struct{} +type mockDBWorkFlow struct{} +type mockDBStepStatus struct{} -func (m mockDB) DeleteWorkflowByName(workflowName string) error { +func (m mockDBWorkFlow) DeleteWorkflowByName(workflowName string) error { return deleteWorkflowByNameMock(workflowName) } -func (m mockDB) FindServiceRequestsByWorkflowName(workflowName string, pageNumber int, pageSize int) ([]*models.ServiceRequest, error) { +func (m mockDBServiceRequest) FindServiceRequestsByWorkflowName(workflowName string, pageNumber int, pageSize int) ([]*models.ServiceRequest, error) { return findServiceRequestsByWorkflowName(workflowName, pageNumber, pageSize) } -func (m mockDB) GetWorkflows(pageNumber int, pageSize int, sortBy models.SortByFields) ([]*models.Workflow, int, error) { +func (m mockDBWorkFlow) GetWorkflows(pageNumber int, pageSize int, sortBy models.SortByFields) ([]*models.Workflow, int, error) { return getWorkflowsMock(pageNumber, pageSize, sortBy) } -func (m mockDB) FindStepStatusByServiceRequestIDAndStepIDAndStatus(serviceRequestID uuid.UUID, stepID int, status models.Status) (*models.StepsStatus, error) { +func (m mockDBStepStatus) FindStepStatusByServiceRequestIDAndStepIDAndStatus(serviceRequestID uuid.UUID, stepID int, status models.Status) (*models.StepsStatus, error) { return findStepStatusByServiceRequestIDAndStepIDAndStatusMock(serviceRequestID, stepID, status) } -func (m mockDB) FindStepStatusByServiceRequestIDAndStepNameAndStatus(serviceRequestID uuid.UUID, stepName string, status models.Status) (*models.StepsStatus, error) { +func (m mockDBStepStatus) FindStepStatusByServiceRequestIDAndStepNameAndStatus(serviceRequestID uuid.UUID, stepName string, status models.Status) (*models.StepsStatus, error) { return findStepStatusByServiceRequestIDAndStepNameAndStatusMock(serviceRequestID, stepName, status) } @@ -43,42 +45,44 @@ var findStepStatusByServiceRequestIDAndStepIDAndStatusMock func(serviceRequestID var getWorkflowsMock func(pageNumber int, pageSize int, sortBy models.SortByFields) ([]*models.Workflow, int, error) var deleteWorkflowByNameMock func(workflowName string) error -func (m mockDB) SaveServiceRequest(serReq *models.ServiceRequest) (*models.ServiceRequest, error) { +func (m mockDBServiceRequest) SaveServiceRequest(serReq *models.ServiceRequest) (*models.ServiceRequest, error) { return saveServiceRequestMock(serReq) } -func (m mockDB) FindServiceRequestByID(id uuid.UUID) (*models.ServiceRequest, error) { +func (m mockDBServiceRequest) FindServiceRequestByID(id uuid.UUID) (*models.ServiceRequest, error) { return findServiceRequestByIDMock(id) } -func (m mockDB) SaveWorkflow(workflow *models.Workflow) (*models.Workflow, error) { +func (m mockDBWorkFlow) SaveWorkflow(workflow *models.Workflow) (*models.Workflow, error) { return SaveWorkflowMock(workflow) } -func (m mockDB) FindWorkflowByName(workflowName string) (*models.Workflow, error) { +func (m mockDBWorkFlow) FindWorkflowByName(workflowName string) (*models.Workflow, error) { return findWorkflowByNameMock(workflowName) } -func (m mockDB) SaveStepStatus(stepStatus *models.StepsStatus) (*models.StepsStatus, error) { +func (m mockDBStepStatus) SaveStepStatus(stepStatus *models.StepsStatus) (*models.StepsStatus, error) { return saveStepStatusMock(stepStatus) } -func (m mockDB) FindStepStatusByServiceRequestID(serviceRequestID uuid.UUID) ([]*models.StepsStatus, error) { +func (m mockDBStepStatus) FindStepStatusByServiceRequestID(serviceRequestID uuid.UUID) ([]*models.StepsStatus, error) { return findStepStatusByServiceRequestIDMock(serviceRequestID) } -func (m mockDB) FindStepStatusByServiceRequestIDAndStatus(serviceRequestID uuid.UUID, status models.Status) ([]*models.StepsStatus, error) { +func (m mockDBStepStatus) FindStepStatusByServiceRequestIDAndStatus(serviceRequestID uuid.UUID, status models.Status) ([]*models.StepsStatus, error) { return findStepStatusByServiceRequestIDAndStatusMock(serviceRequestID, status) } -func (m mockDB) FindAllStepStatusByServiceRequestIDAndStepID(serviceRequestID uuid.UUID, stepID int) ([]*models.StepsStatus, error) { +func (m mockDBStepStatus) FindAllStepStatusByServiceRequestIDAndStepID(serviceRequestID uuid.UUID, stepID int) ([]*models.StepsStatus, error) { return findAllStepStatusByServiceRequestIDAndStepIDMock(serviceRequestID, stepID) } -func (m mockDB) Ping() error { +func (m mockDBStepStatus) Ping() error { return nil } func init() { - repository.SetDB(&mockDB{}) + repository.SetServiceRequestRepository(&mockDBServiceRequest{}) + repository.SetWorkFlowRepository(&mockDBWorkFlow{}) + repository.SetStepStatusRepository(&mockDBStepStatus{}) } diff --git a/services/async_resume_worker.go b/services/async_resume_worker.go index e509df1..9d3844b 100644 --- a/services/async_resume_worker.go +++ b/services/async_resume_worker.go @@ -42,7 +42,7 @@ func resumeSteps(workerID int, resumeStepsChannel <-chan models.AsyncStepRespons log.Infof("%s : Started listening to resume steps channel", prefix) for stepResponse := range resumeStepsChannel { prefix = fmt.Sprintf("%s [REQUEST_ID: %s]", prefix, stepResponse.ServiceRequestID) - log.Debugf("%s : Received step response : %v", prefix, stepResponse) + log.Infof("%s : Received step response : %v", prefix, stepResponse) currentStepStatusArr, _ := FindAllStepStatusByServiceRequestIDAndStepID(stepResponse.ServiceRequestID, stepResponse.StepID) var currentStepStatus *models.StepsStatus for _, stepStatus := range currentStepStatusArr { diff --git a/services/async_resume_worker_test.go b/services/async_resume_worker_test.go index af3da69..5465496 100644 --- a/services/async_resume_worker_test.go +++ b/services/async_resume_worker_test.go @@ -3,6 +3,7 @@ package services import ( "clamp-core/executors" "clamp-core/models" + "clamp-core/repository" "clamp-core/utils" "testing" "time" @@ -193,6 +194,8 @@ func TestShouldAddFailureResponseFromAsyncStepResponseToChannel(t *testing.T) { return statuses, err } + repository.SetStepStatusRepository(&mockDBStepStatus{}) + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { AddStepResponseToResumeChannel(&tt.args.asyncStepResponseReq) diff --git a/services/service_request_service.go b/services/service_request_service.go index f1edd4c..06323fb 100644 --- a/services/service_request_service.go +++ b/services/service_request_service.go @@ -11,7 +11,7 @@ import ( // FindServiceRequestByID is used to fetch service requests by their ID values func FindServiceRequestByID(serviceRequestID uuid.UUID) (*models.ServiceRequest, error) { log.Debugf("Find service Request request by id: %s", serviceRequestID) - serviceRequest, err := repository.GetDB().FindServiceRequestByID(serviceRequestID) + serviceRequest, err := repository.GetServiceRequestRepository().FindServiceRequestByID(serviceRequestID) if err != nil { log.Errorf("No record found with given service request id %s", serviceRequestID) } @@ -21,7 +21,7 @@ func FindServiceRequestByID(serviceRequestID uuid.UUID) (*models.ServiceRequest, // SaveServiceRequest is used to save the created service requests to DB func SaveServiceRequest(serviceReq *models.ServiceRequest) (*models.ServiceRequest, error) { log.Debugf("Saving service request: %v", serviceReq) - serviceRequest, err := repository.GetDB().SaveServiceRequest(serviceReq) + serviceRequest, err := repository.GetServiceRequestRepository().SaveServiceRequest(serviceReq) if err != nil { log.Errorf("Failed saving service request %v, error: %s", serviceRequest, err.Error()) } @@ -31,7 +31,7 @@ func SaveServiceRequest(serviceReq *models.ServiceRequest) (*models.ServiceReque // FindServiceRequestByWorkflowName fetches all ServiceRequests that are associated to a workflow type func FindServiceRequestByWorkflowName(workflowName string, pageNumber int, pageSize int) ([]*models.ServiceRequest, error) { log.Debugf("Getting service request by workflow name: %s", workflowName) - serviceRequests, err := repository.GetDB().FindServiceRequestsByWorkflowName(workflowName, pageNumber, pageSize) + serviceRequests, err := repository.GetServiceRequestRepository().FindServiceRequestsByWorkflowName(workflowName, pageNumber, pageSize) if err != nil { log.Errorf("Failed to fetch service requests by workflow nam: %s for pageNumber: %d, pageSize: %d", workflowName, pageNumber, pageSize) } diff --git a/services/service_request_service_test.go b/services/service_request_service_test.go index 7785427..67f7b27 100644 --- a/services/service_request_service_test.go +++ b/services/service_request_service_test.go @@ -2,7 +2,6 @@ package services import ( "clamp-core/models" - "clamp-core/repository" "errors" "testing" @@ -51,7 +50,7 @@ func TestShouldFailToSaveServiceRequestAndThrowError(t *testing.T) { } func TestFindByID(t *testing.T) { - repository.SetDB(&mockDB{}) + serviceReq := models.ServiceRequest{ ID: uuid.UUID{}, } diff --git a/services/step_status_service.go b/services/step_status_service.go index f2b013d..fc1d553 100644 --- a/services/step_status_service.go +++ b/services/step_status_service.go @@ -29,7 +29,7 @@ var ( func SaveStepStatus(stepStatusReq *models.StepsStatus) (*models.StepsStatus, error) { log.Debugf("Saving step status : %v", stepStatusReq) - stepStatusReq, err := repository.GetDB().SaveStepStatus(stepStatusReq) + stepStatusReq, err := repository.GetStepStatusRepository().SaveStepStatus(stepStatusReq) if err != nil { log.Errorf("Failed saving step status : %v, %s", stepStatusReq, err.Error()) } @@ -46,7 +46,7 @@ func SaveStepStatus(stepStatusReq *models.StepsStatus) (*models.StepsStatus, err func FindStepStatusByServiceRequestID(serviceRequestID uuid.UUID) ([]*models.StepsStatus, error) { log.Debugf("Find step statues by request id : %s ", serviceRequestID) - stepsStatuses, err := repository.GetDB().FindStepStatusByServiceRequestID(serviceRequestID) + stepsStatuses, err := repository.GetStepStatusRepository().FindStepStatusByServiceRequestID(serviceRequestID) if err != nil { log.Errorf("No record found with given service request id %s", serviceRequestID) return []*models.StepsStatus{}, err @@ -56,7 +56,7 @@ func FindStepStatusByServiceRequestID(serviceRequestID uuid.UUID) ([]*models.Ste func FindStepStatusByServiceRequestIDAndStatus(serviceRequestID uuid.UUID, status models.Status) ([]*models.StepsStatus, error) { log.Debugf("Find step statues by request id : %s ", serviceRequestID) - stepsStatuses, err := repository.GetDB().FindStepStatusByServiceRequestIDAndStatus(serviceRequestID, status) + stepsStatuses, err := repository.GetStepStatusRepository().FindStepStatusByServiceRequestIDAndStatus(serviceRequestID, status) if err != nil { log.Errorf("No record found with given service request id %s", serviceRequestID) return []*models.StepsStatus{}, err @@ -66,7 +66,7 @@ func FindStepStatusByServiceRequestIDAndStatus(serviceRequestID uuid.UUID, statu func FindAllStepStatusByServiceRequestIDAndStepID(serviceRequestID uuid.UUID, stepID int) ([]*models.StepsStatus, error) { log.Debugf("Find all step statues by request id : %s and step id : %d", serviceRequestID, stepID) - stepsStatuses, err := repository.GetDB().FindAllStepStatusByServiceRequestIDAndStepID(serviceRequestID, stepID) + stepsStatuses, err := repository.GetStepStatusRepository().FindAllStepStatusByServiceRequestIDAndStepID(serviceRequestID, stepID) if err != nil { log.Errorf("No record found with given service request id %s", serviceRequestID) return []*models.StepsStatus{}, err diff --git a/services/workflow_service.go b/services/workflow_service.go index e636d4f..fea6602 100644 --- a/services/workflow_service.go +++ b/services/workflow_service.go @@ -9,7 +9,7 @@ import ( func SaveWorkflow(workflowReq *models.Workflow) (*models.Workflow, error) { log.Debugf("Saving worflow %v", workflowReq) - workflow, err := repository.GetDB().SaveWorkflow(workflowReq) + workflow, err := repository.GetWorkFlowRepository().SaveWorkflow(workflowReq) if err != nil { log.Errorf("Failed to save workflow: %v, error: %s", workflow, err.Error()) } else { @@ -20,7 +20,7 @@ func SaveWorkflow(workflowReq *models.Workflow) (*models.Workflow, error) { func FindWorkflowByName(workflowName string) (*models.Workflow, error) { log.Debugf("Finding workflow by name : %s", workflowName) - workflow, err := repository.GetDB().FindWorkflowByName(workflowName) + workflow, err := repository.GetWorkFlowRepository().FindWorkflowByName(workflowName) if err != nil { log.Errorf("No record found with given workflow name %s, error: %s", workflowName, err.Error()) } @@ -31,7 +31,7 @@ func FindWorkflowByName(workflowName string) (*models.Workflow, error) { // This method is not exposed an an API. It is implemented for running a test scenario. func DeleteWorkflowByName(workflowName string) error { log.Debugf("Deleting workflow by name : %s", workflowName) - err := repository.GetDB().DeleteWorkflowByName(workflowName) + err := repository.GetWorkFlowRepository().DeleteWorkflowByName(workflowName) if err != nil { log.Debugf("No record found with given workflow name %s, error: %s", workflowName, err.Error()) } @@ -43,7 +43,7 @@ func DeleteWorkflowByName(workflowName string) error { // Also supports filters func GetWorkflows(pageNumber int, pageSize int, sortBy models.SortByFields) ([]*models.Workflow, int, error) { log.Debugf("Getting workflows for pageNumber: %d, pageSize: %d", pageNumber, pageSize) - workflows, totalWorkflowsCount, err := repository.GetDB().GetWorkflows(pageNumber, pageSize, sortBy) + workflows, totalWorkflowsCount, err := repository.GetWorkFlowRepository().GetWorkflows(pageNumber, pageSize, sortBy) if err != nil { log.Debugf("Failed to fetch worflows for pageNumber: %d, pageSize: %d, sortBy %v", pageNumber, pageSize, sortBy) }