Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
tianxiaoliang committed Feb 5, 2020
1 parent 3057f26 commit 8f9b25d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/model/db_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type KVDoc struct {

}

//ViewDoc is db struct
//ViewDoc is db struct, it saves user's custom view name and criteria
type ViewDoc struct {
ID string `json:"id,omitempty" bson:"id,omitempty" yaml:"id,omitempty" swag:"string"`
Display string `json:"display,omitempty" yaml:"display,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions pkg/model/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ type LabelHistoryResponse struct {

//ViewResponse represents the view list
type ViewResponse struct {
Total int `json:"total,omitempty"`
Data []*ViewDoc `json:"data,omitempty"`
Total int `json:"total,omitempty"`
Data []*ViewDoc `json:"data,omitempty"`
}
12 changes: 9 additions & 3 deletions server/service/mongo/view/view_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (s *Service) Create(ctx context.Context, viewDoc *model.ViewDoc, options ..
return viewDoc, nil
}

//Update is only able to update name and criteria
func (s *Service) Update(ctx context.Context, viewDoc *model.ViewDoc) error {
if viewDoc.Domain == "" {
return session.ErrMissingDomain
Expand Down Expand Up @@ -104,8 +105,11 @@ func (s *Service) Update(ctx context.Context, viewDoc *model.ViewDoc) error {
openlogging.Error("can not update view: " + err.Error())
return session.ErrViewUpdate
}
//TODO delete and create a new view
return nil
}

//List return all view user created
func (s *Service) List(ctx context.Context, domain, project string, opts ...service.FindOption) (*model.ViewResponse, error) {
option := service.FindOptions{}
for _, o := range opts {
Expand Down Expand Up @@ -136,7 +140,9 @@ func (s *Service) List(ctx context.Context, domain, project string, opts ...serv
}
return result, nil
}
func (s *Service) GetContent(ctx context.Context, id, domain, project string, opts ...service.FindOption) (*model.ViewResponse, error) {

//GetContent query view's kv data
func (s *Service) GetContent(ctx context.Context, id, domain, project string, opts ...service.FindOption) (*model.KVResponse, error) {
option := service.FindOptions{}
for _, o := range opts {
o(&option)
Expand All @@ -154,9 +160,9 @@ func (s *Service) GetContent(ctx context.Context, id, domain, project string, op
openlogging.Error("can not find view content: " + err.Error())
return nil, session.ErrViewFinding
}
result := &model.ViewResponse{}
result := &model.KVResponse{}
for cur.Next(ctx) {
v := &model.ViewDoc{}
v := &model.KVDoc{}
if err := cur.Decode(v); err != nil {
openlogging.Error("decode error: " + err.Error())
return nil, err
Expand Down
11 changes: 9 additions & 2 deletions server/service/mongo/view/view_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestGet(t *testing.T) {
})

svc := &view.Service{}
t.Run("create and get view", func(t *testing.T) {
t.Run("create and get view content", func(t *testing.T) {
view1, err := svc.Create(context.TODO(), &model.ViewDoc{
Display: "timeout_config",
Project: "test",
Expand All @@ -97,10 +97,17 @@ func TestGet(t *testing.T) {
resp1, err := svc.GetContent(context.TODO(), view1.ID, "default", "test")
assert.NoError(t, err)
assert.Equal(t, 2, len(resp1.Data))
assert.Equal(t, "timeout", resp1.Data[0].Key)

resp2, err := svc.GetContent(context.TODO(), view2.ID, "default", "test")
assert.NoError(t, err)
assert.Equal(t, 3, len(resp2.Data))
assert.Equal(t, "mall", resp1.Data[0].Labels["app"])
t.Log(resp2.Data)
})
t.Run(" list view", func(t *testing.T) {
r, err := svc.List(context.TODO(), "default", "test")
assert.NoError(t, err)
assert.Equal(t, 2, len(r.Data))
})

}
1 change: 1 addition & 0 deletions server/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Revision interface {
type Label interface {
CreateOrUpdate(ctx context.Context, label *model.LabelDoc) (*model.LabelDoc, error)
}

//View create update and get view data
type View interface {
Create(ctx context.Context, viewDoc *model.ViewDoc, options ...FindOption) error
Expand Down

0 comments on commit 8f9b25d

Please sign in to comment.