Skip to content

Commit b6a3520

Browse files
committed
[REFT]: update CreatePipelineRequest to use models and add validation
1 parent bbfc09c commit b6a3520

File tree

6 files changed

+31
-12
lines changed

6 files changed

+31
-12
lines changed

backend/internal/frontend/pipeline/handler.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,18 @@ func (f *FrontendPipelineHandler) GetAllPipelines(w http.ResponseWriter, r *http
3636
}
3737

3838
func (f *FrontendPipelineHandler) CreatePipeline(w http.ResponseWriter, r *http.Request) {
39-
var req CreatePipelineRequest
39+
var req models.CreatePipelineRequest
4040

4141
if err := utils.UnmarshalJSONRequest(r, &req); err != nil {
4242
utils.SendJSONError(w, http.StatusBadRequest, fmt.Sprintf("Invalid payload: %v", err))
4343
return
4444
}
4545

46+
if err := utils.ValidatePipelineRequest(&req); err != nil {
47+
utils.SendJSONError(w, http.StatusBadRequest, fmt.Sprintf("Invalid pipeline request: %v", err))
48+
return
49+
}
50+
4651
utils.Logger.Info(fmt.Sprintf("Received request to create pipeline: %s", req.Name))
4752

4853
pipelineId, err := f.FrontendPipelineService.CreatePipeline(req)

backend/internal/frontend/pipeline/model.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
package frontendpipeline
22

3-
import "github.com/ctrlb-hq/ctrlb-control-plane/backend/internal/models"
4-
5-
type CreatePipelineRequest struct {
6-
Name string `json:"name"`
7-
CreatedBy string `json:"created_by"`
8-
AgentIDs []int `json:"agent_ids"`
9-
PipelineGraph models.PipelineGraph `json:"pipeline_graph"`
10-
}
11-
123
type Pipeline struct {
134
ID int `json:"id"`
145
Name string `json:"name"`

backend/internal/frontend/pipeline/repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (f *FrontendPipelineRepository) GetPipelineInfo(pipelineId int) (*PipelineI
7979
return pipelineInfo, nil
8080
}
8181

82-
func (f *FrontendPipelineRepository) CreatePipeline(createPipelineRequest CreatePipelineRequest) (string, error) {
82+
func (f *FrontendPipelineRepository) CreatePipeline(createPipelineRequest models.CreatePipelineRequest) (string, error) {
8383
tx, err := f.db.Begin()
8484
if err != nil {
8585
return "", fmt.Errorf("failed to begin transaction: %w", err)

backend/internal/frontend/pipeline/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (f *FrontendPipelineService) GetPipelineInfo(pipelineId int) (*PipelineInfo
3434
return f.FrontendPipelineRepository.GetPipelineInfo(pipelineId)
3535
}
3636

37-
func (f *FrontendPipelineService) CreatePipeline(createPipelineRequest CreatePipelineRequest) (string, error) {
37+
func (f *FrontendPipelineService) CreatePipeline(createPipelineRequest models.CreatePipelineRequest) (string, error) {
3838
return f.FrontendPipelineRepository.CreatePipeline(createPipelineRequest)
3939
}
4040

backend/internal/models/pipeline.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package models
22

3+
type CreatePipelineRequest struct {
4+
Name string `json:"name"`
5+
CreatedBy string `json:"created_by"`
6+
AgentIDs []int `json:"agent_ids"`
7+
PipelineGraph PipelineGraph `json:"pipeline_graph"`
8+
}
9+
310
// Struct for pipeline component (Node)
411
type PipelineComponent struct {
512
ComponentID int `json:"component_id"`

backend/internal/utils/validator.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,19 @@ func IsUniqueViolation(err error) bool {
4949
}
5050
return false
5151
}
52+
53+
func ValidatePipelineRequest(request *models.CreatePipelineRequest) error {
54+
if request.Name == "" {
55+
return fmt.Errorf("pipeline name cannot be empty")
56+
}
57+
if request.CreatedBy == "" {
58+
return fmt.Errorf("created by cannot be empty")
59+
}
60+
if request.PipelineGraph.Nodes == nil {
61+
return fmt.Errorf("pipeline nodes cannot be empty")
62+
}
63+
if request.PipelineGraph.Edges == nil {
64+
return fmt.Errorf("pipeline edges cannot be empty")
65+
}
66+
return nil
67+
}

0 commit comments

Comments
 (0)