From 49cb2de08fa4cc43932935dbf4f2e611f1949b5f Mon Sep 17 00:00:00 2001 From: nourbalaha Date: Thu, 30 May 2024 12:34:12 +0900 Subject: [PATCH 1/5] fix: sort models, groups and views by order --- server/internal/adapter/gql/loader_view.go | 8 ++++---- server/internal/usecase/interactor/group.go | 8 ++++++-- server/internal/usecase/interactor/model.go | 5 ++++- server/internal/usecase/interactor/view.go | 4 +++- server/pkg/group/list.go | 12 ++++++++++-- server/pkg/item/view/list.go | 12 ++++++++++-- 6 files changed, 37 insertions(+), 12 deletions(-) diff --git a/server/internal/adapter/gql/loader_view.go b/server/internal/adapter/gql/loader_view.go index c8d1fbf7f1..1e0d3a90aa 100644 --- a/server/internal/adapter/gql/loader_view.go +++ b/server/internal/adapter/gql/loader_view.go @@ -55,9 +55,9 @@ func (c *ViewLoader) FindByModel(ctx context.Context, modelID gqlmodel.ID) ([]*g if err != nil { return nil, err } - integrations := make([]*gqlmodel.View, 0, len(res)) - for _, i := range res { - integrations = append(integrations, gqlmodel.ToView(i)) + views := make([]*gqlmodel.View, 0, len(res)) + for _, v := range res { + views = append(views, gqlmodel.ToView(v)) } - return integrations, nil + return views, nil } diff --git a/server/internal/usecase/interactor/group.go b/server/internal/usecase/interactor/group.go index c301add22f..b74b6876ea 100644 --- a/server/internal/usecase/interactor/group.go +++ b/server/internal/usecase/interactor/group.go @@ -40,7 +40,9 @@ func (i Group) FindByIDs(ctx context.Context, ids id.GroupIDList, operator *usec } func (i Group) FindByProject(ctx context.Context, projectID id.ProjectID, operator *usecase.Operator) (group.List, error) { - return i.repos.Group.FindByProject(ctx, projectID) + g, err := i.repos.Group.FindByProject(ctx, projectID) + ordered := g.Ordered() + return ordered, err } func (i Group) FindByKey(ctx context.Context, pid id.ProjectID, group string, operator *usecase.Operator) (*group.Group, error) { @@ -218,7 +220,9 @@ func (i Group) FindByModel(ctx context.Context, modelID id.ModelID, operator *us gids = gids.Add(fg.Group()) } } - return i.repos.Group.FindByIDs(ctx, gids) + g, err := i.repos.Group.FindByIDs(ctx, gids) + ordered := g.Ordered() + return ordered, err }) } diff --git a/server/internal/usecase/interactor/model.go b/server/internal/usecase/interactor/model.go index a04056cb52..3c16c195e0 100644 --- a/server/internal/usecase/interactor/model.go +++ b/server/internal/usecase/interactor/model.go @@ -3,6 +3,7 @@ package interactor import ( "context" "errors" + "github.com/reearth/reearth-cms/server/internal/usecase" "github.com/reearth/reearth-cms/server/internal/usecase/gateway" "github.com/reearth/reearth-cms/server/internal/usecase/interfaces" @@ -38,7 +39,9 @@ func (i Model) FindByIDs(ctx context.Context, ids []id.ModelID, operator *usecas } func (i Model) FindByProject(ctx context.Context, projectID id.ProjectID, pagination *usecasex.Pagination, operator *usecase.Operator) (model.List, *usecasex.PageInfo, error) { - return i.repos.Model.FindByProject(ctx, projectID, pagination) + m, p, err := i.repos.Model.FindByProject(ctx, projectID, pagination) + ordered := m.Ordered() + return ordered, p, err } func (i Model) FindByKey(ctx context.Context, pid id.ProjectID, model string, operator *usecase.Operator) (*model.Model, error) { diff --git a/server/internal/usecase/interactor/view.go b/server/internal/usecase/interactor/view.go index 0fb9213b9e..03bf1997d5 100644 --- a/server/internal/usecase/interactor/view.go +++ b/server/internal/usecase/interactor/view.go @@ -33,7 +33,9 @@ func (i View) FindByIDs(ctx context.Context, IDs view.IDList, _ *usecase.Operato } func (i View) FindByModel(ctx context.Context, mID view.ModelID, _ *usecase.Operator) (view.List, error) { - return i.repos.View.FindByModel(ctx, mID) + v, err := i.repos.View.FindByModel(ctx, mID) + ordered := v.Ordered() + return ordered, err } func (i View) Create(ctx context.Context, param interfaces.CreateViewParam, op *usecase.Operator) (*view.View, error) { diff --git a/server/pkg/group/list.go b/server/pkg/group/list.go index 11f85e73ab..b921b687c4 100644 --- a/server/pkg/group/list.go +++ b/server/pkg/group/list.go @@ -24,8 +24,8 @@ func (l List) Projects() id.ProjectIDList { func (l List) AreGroupsInTheSameProject() bool { if len(l) == 0 { - return true - } + return true + } firstProjectID := l[0].Project().String() for _, g := range l[1:] { @@ -51,6 +51,14 @@ func (l List) OrderByIDs(ids id.GroupIDList) List { } } } + return res.Ordered() +} + +func (l List) Ordered() List { + res := slices.Clone(l) + slices.SortFunc(res, func(a, b *Group) int { + return a.Order() - b.Order() + }) return res } diff --git a/server/pkg/item/view/list.go b/server/pkg/item/view/list.go index e3e4adb3c8..b33f507c81 100644 --- a/server/pkg/item/view/list.go +++ b/server/pkg/item/view/list.go @@ -16,8 +16,8 @@ func (l List) Projects() id.ProjectIDList { func (l List) AreViewsInTheSameModel() bool { if len(l) == 0 { - return true - } + return true + } firstModelID := l[0].Model().String() for _, v := range l[1:] { @@ -47,5 +47,13 @@ func (l List) OrderByIDs(ids id.ViewIDList) List { } } } + return res.Ordered() +} + +func (l List) Ordered() List { + res := slices.Clone(l) + slices.SortFunc(res, func(a, b *View) int { + return a.Order() - b.Order() + }) return res } From 3f56e4eeaf0f4371402d3afa7e5b9a6a8be049ef Mon Sep 17 00:00:00 2001 From: nourbalaha Date: Thu, 30 May 2024 13:06:43 +0900 Subject: [PATCH 2/5] remove unnecessary code --- server/pkg/group/list.go | 2 +- server/pkg/item/view/list.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/pkg/group/list.go b/server/pkg/group/list.go index b921b687c4..5d4bf783e1 100644 --- a/server/pkg/group/list.go +++ b/server/pkg/group/list.go @@ -51,7 +51,7 @@ func (l List) OrderByIDs(ids id.GroupIDList) List { } } } - return res.Ordered() + return res } func (l List) Ordered() List { diff --git a/server/pkg/item/view/list.go b/server/pkg/item/view/list.go index b33f507c81..15bc935141 100644 --- a/server/pkg/item/view/list.go +++ b/server/pkg/item/view/list.go @@ -47,7 +47,7 @@ func (l List) OrderByIDs(ids id.ViewIDList) List { } } } - return res.Ordered() + return res } func (l List) Ordered() List { From e9f12322500409be1f8105eae4519bfef33b1dcb Mon Sep 17 00:00:00 2001 From: nourbalaha Date: Thu, 30 May 2024 13:15:44 +0900 Subject: [PATCH 3/5] add unit tests --- server/pkg/group/list_test.go | 11 +++++++++++ server/pkg/item/view/list_test.go | 19 +++++++++++++++++++ server/pkg/model/list_test.go | 15 ++++++++++++++- 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 server/pkg/item/view/list_test.go diff --git a/server/pkg/group/list_test.go b/server/pkg/group/list_test.go index b2831801a4..d1d7ab419b 100644 --- a/server/pkg/group/list_test.go +++ b/server/pkg/group/list_test.go @@ -52,3 +52,14 @@ func TestList_OrderByIDs(t *testing.T) { assert.Equal(t, List{group2, group1, group3}, groups.OrderByIDs(id.GroupIDList{group2.ID(), group1.ID(), group3.ID()})) assert.Equal(t, id.ProjectIDList{pid, pid, pid}, groups.Projects()) } + +func TestList_Ordered(t *testing.T) { + pid := id.NewProjectID() + g1 := New().NewID().Project(pid).Schema(id.NewSchemaID()).Key(key.New("key1")).Order(0).MustBuild() + g2 := New().NewID().Project(pid).Schema(id.NewSchemaID()).Key(key.New("key2")).Order(1).MustBuild() + g3 := New().NewID().Project(pid).Schema(id.NewSchemaID()).Key(key.New("key3")).Order(2).MustBuild() + groups := List{g3, g1, g2} + ordered := groups.Ordered() + assert.NotEqual(t, groups, ordered) + assert.Equal(t, List{g1, g2, g3}, ordered) +} diff --git a/server/pkg/item/view/list_test.go b/server/pkg/item/view/list_test.go new file mode 100644 index 0000000000..8617f0d56f --- /dev/null +++ b/server/pkg/item/view/list_test.go @@ -0,0 +1,19 @@ +package view + +import ( + "testing" + + "github.com/go-playground/assert/v2" + "github.com/reearth/reearth-cms/server/pkg/id" +) + +func TestList_Ordered(t *testing.T) { + pid := id.NewProjectID() + v1 := New().NewID().Project(pid).Model(id.NewModelID()).Schema(id.NewSchemaID()).Order(0).MustBuild() + v2 := New().NewID().Project(pid).Model(id.NewModelID()).Schema(id.NewSchemaID()).Order(1).MustBuild() + v3 := New().NewID().Project(pid).Model(id.NewModelID()).Schema(id.NewSchemaID()).Order(2).MustBuild() + views := List{v3, v1, v2} + ordered := views.Ordered() + assert.NotEqual(t, views, ordered) + assert.Equal(t, List{v1, v2, v3}, ordered) +} \ No newline at end of file diff --git a/server/pkg/model/list_test.go b/server/pkg/model/list_test.go index 814d757cbd..d2c2cda34a 100644 --- a/server/pkg/model/list_test.go +++ b/server/pkg/model/list_test.go @@ -1,9 +1,10 @@ package model import ( + "testing" + "github.com/reearth/reearth-cms/server/pkg/id" "github.com/reearth/reearth-cms/server/pkg/key" - "testing" "github.com/stretchr/testify/assert" ) @@ -66,3 +67,15 @@ func TestList_Remove(t *testing.T) { assert.Equal(t, List{mod1, mod2}, mods.Remove(mod3.ID())) assert.Equal(t, List{mod2, mod3}, mods.Remove(mod1.ID())) } + +func TestList_Ordered(t *testing.T) { + pid := id.NewProjectID() + m1 := New().NewID().Project(pid).Schema(id.NewSchemaID()).Key(key.New("key1")).Order(0).MustBuild() + m2 := New().NewID().Project(pid).Schema(id.NewSchemaID()).Key(key.New("key2")).Order(1).MustBuild() + m3 := New().NewID().Project(pid).Schema(id.NewSchemaID()).Key(key.New("key3")).Order(2).MustBuild() + models := List{m3, m1, m2} + ordered := models.Ordered() + assert.NotEqual(t, models, ordered) + assert.Equal(t, List{m1, m2, m3}, ordered) +} + From de99aa35096c5dc7afef04ded92c88b1532b47a0 Mon Sep 17 00:00:00 2001 From: nourbalaha Date: Thu, 30 May 2024 17:33:15 +0900 Subject: [PATCH 4/5] handle errors --- server/internal/usecase/interactor/group.go | 10 ++++++++-- server/internal/usecase/interactor/model.go | 5 ++++- server/internal/usecase/interactor/view.go | 5 ++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/server/internal/usecase/interactor/group.go b/server/internal/usecase/interactor/group.go index b74b6876ea..0e0b7d63c7 100644 --- a/server/internal/usecase/interactor/group.go +++ b/server/internal/usecase/interactor/group.go @@ -41,8 +41,11 @@ func (i Group) FindByIDs(ctx context.Context, ids id.GroupIDList, operator *usec func (i Group) FindByProject(ctx context.Context, projectID id.ProjectID, operator *usecase.Operator) (group.List, error) { g, err := i.repos.Group.FindByProject(ctx, projectID) + if err != nil { + return nil, err + } ordered := g.Ordered() - return ordered, err + return ordered, nil } func (i Group) FindByKey(ctx context.Context, pid id.ProjectID, group string, operator *usecase.Operator) (*group.Group, error) { @@ -221,8 +224,11 @@ func (i Group) FindByModel(ctx context.Context, modelID id.ModelID, operator *us } } g, err := i.repos.Group.FindByIDs(ctx, gids) + if err != nil { + return nil, err + } ordered := g.Ordered() - return ordered, err + return ordered, nil }) } diff --git a/server/internal/usecase/interactor/model.go b/server/internal/usecase/interactor/model.go index 3c16c195e0..c7cfc60816 100644 --- a/server/internal/usecase/interactor/model.go +++ b/server/internal/usecase/interactor/model.go @@ -40,8 +40,11 @@ func (i Model) FindByIDs(ctx context.Context, ids []id.ModelID, operator *usecas func (i Model) FindByProject(ctx context.Context, projectID id.ProjectID, pagination *usecasex.Pagination, operator *usecase.Operator) (model.List, *usecasex.PageInfo, error) { m, p, err := i.repos.Model.FindByProject(ctx, projectID, pagination) + if err != nil { + return nil, nil, err + } ordered := m.Ordered() - return ordered, p, err + return ordered, p, nil } func (i Model) FindByKey(ctx context.Context, pid id.ProjectID, model string, operator *usecase.Operator) (*model.Model, error) { diff --git a/server/internal/usecase/interactor/view.go b/server/internal/usecase/interactor/view.go index 03bf1997d5..9b7574362a 100644 --- a/server/internal/usecase/interactor/view.go +++ b/server/internal/usecase/interactor/view.go @@ -34,8 +34,11 @@ func (i View) FindByIDs(ctx context.Context, IDs view.IDList, _ *usecase.Operato func (i View) FindByModel(ctx context.Context, mID view.ModelID, _ *usecase.Operator) (view.List, error) { v, err := i.repos.View.FindByModel(ctx, mID) + if err != nil { + return nil, err + } ordered := v.Ordered() - return ordered, err + return ordered, nil } func (i View) Create(ctx context.Context, param interfaces.CreateViewParam, op *usecase.Operator) (*view.View, error) { From 19473771cc5053df9ac26198d73f12a52fba02c2 Mon Sep 17 00:00:00 2001 From: nourbalaha Date: Thu, 30 May 2024 17:57:49 +0900 Subject: [PATCH 5/5] refactor --- server/internal/usecase/interactor/group.go | 6 ++---- server/internal/usecase/interactor/model.go | 3 +-- server/internal/usecase/interactor/view.go | 3 +-- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/server/internal/usecase/interactor/group.go b/server/internal/usecase/interactor/group.go index 0e0b7d63c7..2058603c97 100644 --- a/server/internal/usecase/interactor/group.go +++ b/server/internal/usecase/interactor/group.go @@ -44,8 +44,7 @@ func (i Group) FindByProject(ctx context.Context, projectID id.ProjectID, operat if err != nil { return nil, err } - ordered := g.Ordered() - return ordered, nil + return g.Ordered(), nil } func (i Group) FindByKey(ctx context.Context, pid id.ProjectID, group string, operator *usecase.Operator) (*group.Group, error) { @@ -227,8 +226,7 @@ func (i Group) FindByModel(ctx context.Context, modelID id.ModelID, operator *us if err != nil { return nil, err } - ordered := g.Ordered() - return ordered, nil + return g.Ordered(), nil }) } diff --git a/server/internal/usecase/interactor/model.go b/server/internal/usecase/interactor/model.go index c7cfc60816..b3f2ac22f5 100644 --- a/server/internal/usecase/interactor/model.go +++ b/server/internal/usecase/interactor/model.go @@ -43,8 +43,7 @@ func (i Model) FindByProject(ctx context.Context, projectID id.ProjectID, pagina if err != nil { return nil, nil, err } - ordered := m.Ordered() - return ordered, p, nil + return m.Ordered(), p, nil } func (i Model) FindByKey(ctx context.Context, pid id.ProjectID, model string, operator *usecase.Operator) (*model.Model, error) { diff --git a/server/internal/usecase/interactor/view.go b/server/internal/usecase/interactor/view.go index 9b7574362a..3dfcff8070 100644 --- a/server/internal/usecase/interactor/view.go +++ b/server/internal/usecase/interactor/view.go @@ -37,8 +37,7 @@ func (i View) FindByModel(ctx context.Context, mID view.ModelID, _ *usecase.Oper if err != nil { return nil, err } - ordered := v.Ordered() - return ordered, nil + return v.Ordered(), nil } func (i View) Create(ctx context.Context, param interfaces.CreateViewParam, op *usecase.Operator) (*view.View, error) {