diff --git a/src/api/setting.go b/src/api/setting.go index 872ceae..d0125de 100644 --- a/src/api/setting.go +++ b/src/api/setting.go @@ -45,12 +45,12 @@ func (a *Setting) Create(ctx *gear.Context) error { return err } - body := tpl.NameDescBody{} + body := tpl.SettingCreateBody{} if err := ctx.ParseBody(&body); err != nil { return err } - res, err := a.blls.Setting.Create(ctx, req.Product, req.Module, body.Name, body.Desc) + res, err := a.blls.Setting.Create(ctx, req.Product, req.Module, &body) if err != nil { return err } diff --git a/src/bll/setting.go b/src/bll/setting.go index 6d6231d..724d300 100644 --- a/src/bll/setting.go +++ b/src/bll/setting.go @@ -2,6 +2,7 @@ package bll import ( "context" + "strings" "github.com/teambition/gear" "github.com/teambition/urbs-setting/src/model" @@ -83,7 +84,7 @@ func (b *Setting) Get(ctx context.Context, productName, moduleName, settingName } // Create 创建功能模块配置项 -func (b *Setting) Create(ctx context.Context, productName, moduleName, settingName, desc string) (*tpl.SettingInfoRes, error) { +func (b *Setting) Create(ctx context.Context, productName, moduleName string, body *tpl.SettingCreateBody) (*tpl.SettingInfoRes, error) { productID, err := b.ms.Product.AcquireID(ctx, productName) if err != nil { return nil, err @@ -94,7 +95,17 @@ func (b *Setting) Create(ctx context.Context, productName, moduleName, settingNa return nil, err } - setting := &schema.Setting{ModuleID: module.ID, Name: settingName, Desc: desc} + setting := &schema.Setting{ModuleID: module.ID, Name: body.Name, Desc: body.Desc} + if body.Channels != nil { + setting.Channels = strings.Join(*body.Channels, ",") + } + if body.Clients != nil { + setting.Clients = strings.Join(*body.Clients, ",") + } + if body.Values != nil { + setting.Values = strings.Join(*body.Values, ",") + } + if err = b.ms.Setting.Create(ctx, setting); err != nil { return nil, err } diff --git a/src/tpl/setting.go b/src/tpl/setting.go index 2d18a58..2c3acfa 100644 --- a/src/tpl/setting.go +++ b/src/tpl/setting.go @@ -10,6 +10,65 @@ import ( "github.com/teambition/urbs-setting/src/service" ) +// SettingCreateBody ... +type SettingCreateBody struct { + Name string `json:"name"` + Desc string `json:"desc"` + Channels *[]string `json:"channels"` + Clients *[]string `json:"clients"` + Values *[]string `json:"values"` +} + +// Validate 实现 gear.BodyTemplate。 +func (t *SettingCreateBody) Validate() error { + if !validNameReg.MatchString(t.Name) { + return gear.ErrBadRequest.WithMsgf("invalid name: %s", t.Name) + } + if len(t.Desc) > 1022 { + return gear.ErrBadRequest.WithMsgf("desc too long: %d (<= 1022)", len(t.Desc)) + } + if t.Channels != nil { + if len(*t.Channels) > 5 { + return gear.ErrBadRequest.WithMsgf("too many channels: %d", len(*t.Channels)) + } + if !SortStringsAndCheck(*t.Channels) { + return gear.ErrBadRequest.WithMsgf("invalid channels: %v", *t.Channels) + } + for _, channel := range *t.Channels { + if !StringSliceHas(conf.Config.Channels, channel) { + return gear.ErrBadRequest.WithMsgf("invalid channel: %s", channel) + } + } + } + if t.Clients != nil { + if len(*t.Clients) > 10 { + return gear.ErrBadRequest.WithMsgf("too many clients: %d", len(*t.Clients)) + } + if !SortStringsAndCheck(*t.Clients) { + return gear.ErrBadRequest.WithMsgf("invalid clients: %v", *t.Clients) + } + for _, client := range *t.Clients { + if !StringSliceHas(conf.Config.Clients, client) { + return gear.ErrBadRequest.WithMsgf("invalid client: %s", client) + } + } + } + if t.Values != nil { + if len(*t.Values) > 10 { + return gear.ErrBadRequest.WithMsgf("too many values: %d", len(*t.Clients)) + } + if !SortStringsAndCheck(*t.Values) { + return gear.ErrBadRequest.WithMsgf("invalid values: %v", *t.Values) + } + for _, value := range *t.Values { + if !validValueReg.MatchString(value) { + return gear.ErrBadRequest.WithMsgf("invalid value: %s", value) + } + } + } + return nil +} + // SettingUpdateBody ... type SettingUpdateBody struct { Desc *string `json:"desc"`