Skip to content

Commit

Permalink
Merge pull request #4 from teambition/feature/setting
Browse files Browse the repository at this point in the history
add setting params
  • Loading branch information
zensh authored May 13, 2020
2 parents 24f6484 + 377672a commit fa8ceee
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/api/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
15 changes: 13 additions & 2 deletions src/bll/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bll

import (
"context"
"strings"

"github.com/teambition/gear"
"github.com/teambition/urbs-setting/src/model"
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
59 changes: 59 additions & 0 deletions src/tpl/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit fa8ceee

Please sign in to comment.