Skip to content

Commit

Permalink
dao, api: rest: Prevent of updating/deleting example task components
Browse files Browse the repository at this point in the history
  • Loading branch information
ish-hcc committed Nov 1, 2024
1 parent 8511f2d commit 5fd3258
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions dao/taskComponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TaskComponentCreate(taskComponent *model.TaskComponent) (*model.TaskCompone

taskComponent.CreatedAt = now
taskComponent.UpdatedAt = now
taskComponent.IsExample = false

result := db.DB.Session(&gorm.Session{SkipHooks: true}).Create(taskComponent)
err := result.Error
Expand All @@ -76,6 +77,10 @@ func TaskComponentCreate(taskComponent *model.TaskComponent) (*model.TaskCompone
}

func TaskComponentUpdate(taskComponent *model.TaskComponent) error {
if taskComponent.IsExample {
return errors.New("example task component can't be updated")
}

taskComponent.UpdatedAt = time.Now()

result := db.DB.Model(&model.TaskComponent{}).Where("id = ?", taskComponent.ID).Updates(taskComponent)
Expand All @@ -88,6 +93,10 @@ func TaskComponentUpdate(taskComponent *model.TaskComponent) error {
}

func TaskComponentDelete(taskComponent *model.TaskComponent) error {
if taskComponent.IsExample {
return errors.New("example task component can't be deleted")
}

result := db.DB.Delete(taskComponent)
err := result.Error
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions db/taskComponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ func TaskComponentInit() error {
taskComponent.UpdatedAt = now
}

taskComponent.IsExample = true

if err := DB.Session(&gorm.Session{SkipHooks: true}).Save(taskComponent).Error; err != nil {
return fmt.Errorf("failed to save task component: %v", err)
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,9 @@ const docTemplate = `{
"id": {
"type": "string"
},
"is_example": {
"type": "boolean"
},
"name": {
"type": "string"
},
Expand Down
3 changes: 3 additions & 0 deletions pkg/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,9 @@
"id": {
"type": "string"
},
"is_example": {
"type": "boolean"
},
"name": {
"type": "string"
},
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ definitions:
type: string
id:
type: string
is_example:
type: boolean
name:
type: string
updated_at:
Expand Down
1 change: 1 addition & 0 deletions pkg/api/rest/model/taskComponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type TaskComponent struct {
Data TaskComponentData `gorm:"column:data" json:"data" mapstructure:"data" validate:"required"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime:false" json:"created_at" mapstructure:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at;autoCreateTime:false" json:"updated_at" mapstructure:"updated_at"`
IsExample bool `gorm:"column:is_example" json:"is_example" mapstructure:"is_example"`
}

type CreateTaskComponentReq struct {
Expand Down

0 comments on commit 5fd3258

Please sign in to comment.