Skip to content

Commit

Permalink
add label&setting's users and. groups API
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Apr 24, 2020
1 parent 58d28ab commit bfcbfcc
Show file tree
Hide file tree
Showing 30 changed files with 445 additions and 76 deletions.
4 changes: 2 additions & 2 deletions src/api/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ func TestGroupAPIs(t *testing.T) {
assert.Equal(int64(0), count)
})

t.Run("should work with sync_lt query", func(t *testing.T) {
t.Run("should work with syncLt query", func(t *testing.T) {
assert := assert.New(t)

var count int64
Expand Down Expand Up @@ -591,7 +591,7 @@ func TestGroupAPIs(t *testing.T) {
assert.Nil(tt.DB.Table(`user_group`).Where("group_id = ?", group.ID).Count(&count).Error)
assert.Equal(int64(10), count)

res, err = request.Delete(fmt.Sprintf("%s/v1/groups/%s/members?sync_lt=%d", tt.Host, group.UID, syncAt)).
res, err = request.Delete(fmt.Sprintf("%s/v1/groups/%s/members?syncLt=%d", tt.Host, group.UID, syncAt)).
End()
assert.Nil(err)
assert.Equal(200, res.StatusCode)
Expand Down
26 changes: 26 additions & 0 deletions src/api/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,29 @@ func (a *Label) DeleteRule(ctx *gear.Context) error {
}
return ctx.OkJSON(res)
}

// ListUsers ..
func (a *Label) ListUsers(ctx *gear.Context) error {
req := tpl.ProductLabelURL{}
if err := ctx.ParseURL(&req); err != nil {
return err
}
res, err := a.blls.Label.ListUsers(ctx, req.Product, req.Label, req.Pagination)
if err != nil {
return err
}
return ctx.OkJSON(res)
}

// ListGroups ..
func (a *Label) ListGroups(ctx *gear.Context) error {
req := tpl.ProductLabelURL{}
if err := ctx.ParseURL(&req); err != nil {
return err
}
res, err := a.blls.Label.ListGroups(ctx, req.Product, req.Label, req.Pagination)
if err != nil {
return err
}
return ctx.OkJSON(res)
}
4 changes: 2 additions & 2 deletions src/api/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestLabelAPIs(t *testing.T) {

text, err := res.Text()
assert.Nil(err)
assert.True(strings.Contains(text, `"offline_at":null`))
assert.True(strings.Contains(text, `"offlineAt":null`))
assert.True(strings.Contains(text, `"hid"`))
assert.False(strings.Contains(text, `"id"`))

Expand Down Expand Up @@ -153,7 +153,7 @@ func TestLabelAPIs(t *testing.T) {

text, err := res.Text()
assert.Nil(err)
assert.True(strings.Contains(text, `"offline_at":null`))
assert.True(strings.Contains(text, `"offlineAt":null`))
assert.False(strings.Contains(text, `"id"`))

json := tpl.LabelInfoRes{}
Expand Down
4 changes: 2 additions & 2 deletions src/api/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestModuleAPIs(t *testing.T) {

text, err := res.Text()
assert.Nil(err)
assert.True(strings.Contains(text, `"offline_at":null`))
assert.True(strings.Contains(text, `"offlineAt":null`))
assert.False(strings.Contains(text, `"id"`))

json := tpl.ModuleRes{}
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestModuleAPIs(t *testing.T) {

text, err := res.Text()
assert.Nil(err)
assert.True(strings.Contains(text, `"offline_at":null`))
assert.True(strings.Contains(text, `"offlineAt":null`))
assert.False(strings.Contains(text, `"id"`))

json := tpl.ModuleRes{}
Expand Down
8 changes: 4 additions & 4 deletions src/api/product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func TestProductAPIs(t *testing.T) {

text, err := res.Text()
assert.Nil(err)
assert.True(strings.Contains(text, `"offline_at":null`))
assert.True(strings.Contains(text, `"deleted_at":null`))
assert.True(strings.Contains(text, `"offlineAt":null`))
assert.True(strings.Contains(text, `"deletedAt":null`))
assert.False(strings.Contains(text, `"id"`))

json := tpl.ProductRes{}
Expand Down Expand Up @@ -183,8 +183,8 @@ func TestProductAPIs(t *testing.T) {

text, err := res.Text()
assert.Nil(err)
assert.True(strings.Contains(text, `"offline_at":null`))
assert.True(strings.Contains(text, `"deleted_at":null`))
assert.True(strings.Contains(text, `"offlineAt":null`))
assert.True(strings.Contains(text, `"deletedAt":null`))
assert.False(strings.Contains(text, `"id"`))

json := tpl.ProductRes{}
Expand Down
8 changes: 8 additions & 0 deletions src/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ func newRouters(apis *APIs) []*gear.Router {
routerV1.Post("/products/:product/modules/:module/settings/:setting/rules", apis.Setting.CreateRule)
// 读取指定产品功能模块配置项的灰度发布规则列表
routerV1.Get("/products/:product/modules/:module/settings/:setting/rules", apis.Setting.ListRules)
// 读取指定产品功能模块配置项的用户列表
routerV1.Get("/products/:product/modules/:module/settings/:setting/users", apis.Setting.ListUsers)
// 读取指定产品功能模块配置项的群组列表
routerV1.Get("/products/:product/modules/:module/settings/:setting/groups", apis.Setting.ListGroups)
// 更新指定产品功能模块配置项的指定灰度发布规则
routerV1.Put("/settings/:hid/rules/:ruleHID", apis.Setting.UpdateRule)
// 删除指定产品功能模块配置项的指定灰度发布规则
Expand All @@ -176,6 +180,10 @@ func newRouters(apis *APIs) []*gear.Router {
routerV1.Post("/products/:product/labels/:label/rules", apis.Label.CreateRule)
// 读取指定产品灰度标签的灰度发布规则列表
routerV1.Get("/products/:product/labels/:label/rules", apis.Label.ListRules)
// 读取指定产品灰度标签的用户列表
routerV1.Get("/products/:product/labels/:label/users", apis.Label.ListUsers)
// 读取指定产品灰度标签的群组列表
routerV1.Get("/products/:product/labels/:label/groups", apis.Label.ListGroups)
// 更新指定产品灰度标签的指定灰度发布规则
routerV1.Put("/labels/:hid/rules/:ruleHID", apis.Label.UpdateRule)
// 删除指定产品灰度标签的指定灰度发布规则
Expand Down
26 changes: 26 additions & 0 deletions src/api/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,29 @@ func (a *Setting) DeleteRule(ctx *gear.Context) error {
}
return ctx.OkJSON(res)
}

// ListUsers ..
func (a *Setting) ListUsers(ctx *gear.Context) error {
req := tpl.ProductModuleSettingURL{}
if err := ctx.ParseURL(&req); err != nil {
return err
}
res, err := a.blls.Setting.ListUsers(ctx, req.Product, req.Module, req.Setting, req.Pagination)
if err != nil {
return err
}
return ctx.OkJSON(res)
}

// ListGroups ..
func (a *Setting) ListGroups(ctx *gear.Context) error {
req := tpl.ProductModuleSettingURL{}
if err := ctx.ParseURL(&req); err != nil {
return err
}
res, err := a.blls.Setting.ListGroups(ctx, req.Product, req.Module, req.Setting, req.Pagination)
if err != nil {
return err
}
return ctx.OkJSON(res)
}
4 changes: 2 additions & 2 deletions src/api/setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestSettingAPIs(t *testing.T) {

text, err := res.Text()
assert.Nil(err)
assert.True(strings.Contains(text, `"offline_at":null`))
assert.True(strings.Contains(text, `"offlineAt":null`))
assert.False(strings.Contains(text, `"id"`))

json := tpl.SettingInfoRes{}
Expand Down Expand Up @@ -202,7 +202,7 @@ func TestSettingAPIs(t *testing.T) {

text, err := res.Text()
assert.Nil(err)
assert.True(strings.Contains(text, `"offline_at":null`))
assert.True(strings.Contains(text, `"offlineAt":null`))
assert.False(strings.Contains(text, `"id"`))

json := tpl.SettingInfoRes{}
Expand Down
50 changes: 50 additions & 0 deletions src/bll/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,53 @@ func (b *Label) DeleteRule(ctx context.Context, labelID, ruleID int64) (*tpl.Boo

return res, nil
}

// ListUsers 返回产品下灰度标签的用户列表
func (b *Label) ListUsers(ctx context.Context, productName, labelName string, pg tpl.Pagination) (*tpl.LabelUsersInfoRes, error) {
productID, err := b.ms.Product.AcquireID(ctx, productName)
if err != nil {
return nil, err
}

label, err := b.ms.Label.Acquire(ctx, productID, labelName)
if err != nil {
return nil, err
}

data, err := b.ms.Label.ListUsers(ctx, label.ID, pg)
if err != nil {
return nil, err
}
res := &tpl.LabelUsersInfoRes{Result: data}
// res.TotalSize = total
if len(res.Result) > pg.PageSize {
res.NextPageToken = tpl.IDToPageToken(res.Result[pg.PageSize].ID)
res.Result = res.Result[:pg.PageSize]
}
return res, nil
}

// ListGroups 返回产品下灰度标签的群组列表
func (b *Label) ListGroups(ctx context.Context, productName, labelName string, pg tpl.Pagination) (*tpl.LabelGroupsInfoRes, error) {
productID, err := b.ms.Product.AcquireID(ctx, productName)
if err != nil {
return nil, err
}

label, err := b.ms.Label.Acquire(ctx, productID, labelName)
if err != nil {
return nil, err
}

data, err := b.ms.Label.ListGroups(ctx, label.ID, pg)
if err != nil {
return nil, err
}
res := &tpl.LabelGroupsInfoRes{Result: data}
// res.TotalSize = total
if len(res.Result) > pg.PageSize {
res.NextPageToken = tpl.IDToPageToken(res.Result[pg.PageSize].ID)
res.Result = res.Result[:pg.PageSize]
}
return res, nil
}
60 changes: 60 additions & 0 deletions src/bll/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,63 @@ func (b *Setting) DeleteRule(ctx context.Context, settingID, ruleID int64) (*tpl

return res, nil
}

// ListUsers 返回产品下功能配置项的用户列表
func (b *Setting) ListUsers(ctx context.Context, productName, moduleName, settingName string, pg tpl.Pagination) (*tpl.SettingUsersInfoRes, error) {
productID, err := b.ms.Product.AcquireID(ctx, productName)
if err != nil {
return nil, err
}

module, err := b.ms.Module.Acquire(ctx, productID, moduleName)
if err != nil {
return nil, err
}

setting, err := b.ms.Setting.Acquire(ctx, module.ID, settingName)
if err != nil {
return nil, err
}

data, err := b.ms.Setting.ListUsers(ctx, setting.ID, pg)
if err != nil {
return nil, err
}
res := &tpl.SettingUsersInfoRes{Result: data}
// res.TotalSize = total
if len(res.Result) > pg.PageSize {
res.NextPageToken = tpl.IDToPageToken(res.Result[pg.PageSize].ID)
res.Result = res.Result[:pg.PageSize]
}
return res, nil
}

// ListGroups 返回产品下功能配置项的群组列表
func (b *Setting) ListGroups(ctx context.Context, productName, moduleName, settingName string, pg tpl.Pagination) (*tpl.SettingGroupsInfoRes, error) {
productID, err := b.ms.Product.AcquireID(ctx, productName)
if err != nil {
return nil, err
}

module, err := b.ms.Module.Acquire(ctx, productID, moduleName)
if err != nil {
return nil, err
}

setting, err := b.ms.Setting.Acquire(ctx, module.ID, settingName)
if err != nil {
return nil, err
}

data, err := b.ms.Setting.ListGroups(ctx, setting.ID, pg)
if err != nil {
return nil, err
}
res := &tpl.SettingGroupsInfoRes{Result: data}
// res.TotalSize = total
if len(res.Result) > pg.PageSize {
res.NextPageToken = tpl.IDToPageToken(res.Result[pg.PageSize].ID)
res.Result = res.Result[:pg.PageSize]
}
return res, nil
}
59 changes: 59 additions & 0 deletions src/model/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/jinzhu/gorm"
"github.com/teambition/gear"
"github.com/teambition/urbs-setting/src/schema"
"github.com/teambition/urbs-setting/src/service"
"github.com/teambition/urbs-setting/src/tpl"
)

Expand Down Expand Up @@ -251,3 +252,61 @@ func (m *Label) AcquireRelease(ctx context.Context, labelID int64) (int64, error
}
return label.Release, nil
}

const listLabelUsersSQL = "select t1.`id`, t1.`created_at`, t1.`rls`, t2.`uid` " +
"from `user_label` t1, `urbs_user` t2 " +
"where t1.`label_id` = ? and t1.`id` <= ? and t1.`user_id` = t2.`id` " +
"order by t1.`id` desc " +
"limit ?"

// ListUsers ...
func (m *Label) ListUsers(ctx context.Context, labelID int64, pg tpl.Pagination) ([]tpl.LabelUserInfo, error) {
data := []tpl.LabelUserInfo{}
cursor := pg.TokenToID(true)

rows, err := m.DB.Raw(listLabelUsersSQL, labelID, cursor, pg.PageSize+1).Rows()
defer rows.Close()

if err != nil {
return nil, err
}

for rows.Next() {
info := tpl.LabelUserInfo{}
if err := rows.Scan(&info.ID, &info.AssignedAt, &info.Release, &info.User); err != nil {
return nil, err
}
info.LabelHID = service.IDToHID(labelID, "label")
data = append(data, info)
}
return data, err
}

const listLabelGroupsSQL = "select t1.`id`, t1.`created_at`, t1.`rls`, t2.`uid`, t2.`kind`, t2.`description`, t2.`status` " +
"from `group_label` t1, `urbs_group` t2 " +
"where t1.`label_id` = ? and t1.`id` <= ? and t1.`group_id` = t2.`id` " +
"order by t1.`id` desc " +
"limit ?"

// ListGroups ...
func (m *Label) ListGroups(ctx context.Context, labelID int64, pg tpl.Pagination) ([]tpl.LabelGroupInfo, error) {
data := []tpl.LabelGroupInfo{}
cursor := pg.TokenToID(true)

rows, err := m.DB.Raw(listLabelGroupsSQL, labelID, cursor, pg.PageSize+1).Rows()
defer rows.Close()

if err != nil {
return nil, err
}

for rows.Next() {
info := tpl.LabelGroupInfo{}
if err := rows.Scan(&info.ID, &info.AssignedAt, &info.Release, &info.Group, &info.Kind, &info.Desc, &info.Status); err != nil {
return nil, err
}
info.LabelHID = service.IDToHID(labelID, "label")
data = append(data, info)
}
return data, err
}
Loading

0 comments on commit bfcbfcc

Please sign in to comment.