123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package controller
- import (
- "fmt"
- "git.cestong.com.cn/cecf/cecf-golib/pkg/apollo"
- "git.cestong.com.cn/cecf/cecf-golib/pkg/errors"
- gin2 "git.cestong.com.cn/cecf/cecf-golib/pkg/gin"
- "github.com/gin-gonic/gin"
- "github.com/go-playground/validator/v10"
- "gorm.io/gorm"
- )
- type ConfigItemController struct {
- db *gorm.DB
- val *validator.Validate
- apolloCli *apollo.HTTPClient
- }
- func NewConfigItemController(db *gorm.DB, val *validator.Validate, apolloCli *apollo.HTTPClient) *ConfigItemController {
- return &ConfigItemController{db: db, val: val, apolloCli: apolloCli}
- }
- type CreateConfigItemReqVO struct {
- Block string `json:"block"`
- Key string `json:"key" validate:"min=1"`
- Value string `json:"value"`
- Comment string `json:"comment"`
- ConstraintIDList []uint `json:"constraintIDList"`
- }
- func (cic *ConfigItemController) Create(c *gin.Context) (interface{}, error) {
- appID, namespace := c.Param("appID"), c.Param("namespaceID")
- var req CreateConfigItemReqVO
- if errParse := c.ShouldBindJSON(&req); errParse != nil {
- return nil, errors.ErrBadRequest
- }
- if errVal := cic.val.Struct(&req); errVal != nil {
- return nil, errors.ErrBadRequest
- }
- // call apollo create item
- apolloKey := fmt.Sprintf("%s.%s", req.Block, req.Key)
- if errCreateApollo := cic.apolloCli.CreateConfigItem(appID, namespace, apolloKey,
- req.Value); errCreateApollo != nil {
- return nil, errCreateApollo
- }
- // save constraint list to db
- return gin2.SuccessGeneralVO(), nil
- }
|