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..2058603c97 100644 --- a/server/internal/usecase/interactor/group.go +++ b/server/internal/usecase/interactor/group.go @@ -40,7 +40,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) { - return i.repos.Group.FindByProject(ctx, projectID) + g, err := i.repos.Group.FindByProject(ctx, projectID) + if err != nil { + return nil, err + } + return g.Ordered(), nil } func (i Group) FindByKey(ctx context.Context, pid id.ProjectID, group string, operator *usecase.Operator) (*group.Group, error) { @@ -218,7 +222,11 @@ 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) + if err != nil { + return nil, err + } + return g.Ordered(), nil }) } diff --git a/server/internal/usecase/interactor/model.go b/server/internal/usecase/interactor/model.go index a04056cb52..b3f2ac22f5 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,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) { - return i.repos.Model.FindByProject(ctx, projectID, pagination) + m, p, err := i.repos.Model.FindByProject(ctx, projectID, pagination) + if err != nil { + return nil, nil, err + } + 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 0fb9213b9e..3dfcff8070 100644 --- a/server/internal/usecase/interactor/view.go +++ b/server/internal/usecase/interactor/view.go @@ -33,7 +33,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) { - return i.repos.View.FindByModel(ctx, mID) + v, err := i.repos.View.FindByModel(ctx, mID) + if err != nil { + return nil, err + } + return v.Ordered(), nil } 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..5d4bf783e1 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:] { @@ -54,6 +54,14 @@ func (l List) OrderByIDs(ids id.GroupIDList) List { return res } +func (l List) Ordered() List { + res := slices.Clone(l) + slices.SortFunc(res, func(a, b *Group) int { + return a.Order() - b.Order() + }) + return res +} + func (l List) SchemaIDs() id.SchemaIDList { var schemaIds id.SchemaIDList for _, group := range l { 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.go b/server/pkg/item/view/list.go index e3e4adb3c8..15bc935141 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:] { @@ -49,3 +49,11 @@ func (l List) OrderByIDs(ids id.ViewIDList) List { } return res } + +func (l List) Ordered() List { + res := slices.Clone(l) + slices.SortFunc(res, func(a, b *View) int { + return a.Order() - b.Order() + }) + return res +} 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) +} +