Skip to content

Commit

Permalink
fix(server): sort models, groups and views by order (#1166)
Browse files Browse the repository at this point in the history
* fix: sort models, groups and views by order

* remove unnecessary code

* add unit tests

* handle errors

* refactor
  • Loading branch information
nourbalaha authored May 30, 2024
1 parent 5a0608c commit e186bed
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 13 deletions.
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)
}

0 comments on commit e186bed

Please sign in to comment.