Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): sort models, groups and views by order #1166

Merged
merged 7 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions server/internal/adapter/gql/loader_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 10 additions & 2 deletions server/internal/usecase/interactor/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
})
}

Expand Down
7 changes: 6 additions & 1 deletion server/internal/usecase/interactor/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion server/internal/usecase/interactor/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 10 additions & 2 deletions server/pkg/group/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:] {
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions server/pkg/group/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 10 additions & 2 deletions server/pkg/item/view/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:] {
Expand Down Expand Up @@ -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
}
19 changes: 19 additions & 0 deletions server/pkg/item/view/list_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 14 additions & 1 deletion server/pkg/model/list_test.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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)
}

Loading