From 2b5619c822fe9616fe6ab498f67e9ed69d0b06a0 Mon Sep 17 00:00:00 2001 From: rot1024 Date: Thu, 6 Oct 2022 12:47:34 +0900 Subject: [PATCH 1/8] domain model Co-authored-by: Go Nakagawa --- server/pkg/project/builder.go | 5 ++ server/pkg/project/builder_test.go | 9 +++ server/pkg/project/project.go | 10 +++ server/pkg/project/project_test.go | 4 +- server/pkg/project/publication.go | 44 +++++++++++++ server/pkg/project/publication_test.go | 85 ++++++++++++++++++++++++++ 6 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 server/pkg/project/publication.go create mode 100644 server/pkg/project/publication_test.go diff --git a/server/pkg/project/builder.go b/server/pkg/project/builder.go index 7302ab14fb..bb22d4bffc 100644 --- a/server/pkg/project/builder.go +++ b/server/pkg/project/builder.go @@ -79,3 +79,8 @@ func (b *Builder) Workspace(team WorkspaceID) *Builder { b.p.workspaceID = team return b } + +func (b *Builder) Publication(publication *Publication) *Builder { + b.p.publication = publication + return b +} diff --git a/server/pkg/project/builder_test.go b/server/pkg/project/builder_test.go index d1fc48d764..0bbaa9dab8 100644 --- a/server/pkg/project/builder_test.go +++ b/server/pkg/project/builder_test.go @@ -90,6 +90,15 @@ func TestBuilder_UpdatedAt(t *testing.T) { assert.True(t, reflect.DeepEqual(res.UpdatedAt(), d)) } +func TestBuilder_Publication(t *testing.T) { + var tb = New().NewID() + p := &Publication{} + res := tb.Publication(p) + assert.Equal(t, &Builder{ + p: &Project{id: tb.p.id, publication: p}, + }, res) +} + func TestBuilder_Build(t *testing.T) { d := time.Date(1900, 1, 1, 00, 00, 0, 1, time.UTC) i, _ := url.Parse("ttt://xxx.aa/") diff --git a/server/pkg/project/project.go b/server/pkg/project/project.go index e4b2484aaf..a9fed2415a 100644 --- a/server/pkg/project/project.go +++ b/server/pkg/project/project.go @@ -22,6 +22,7 @@ type Project struct { alias string imageURL *url.URL updatedAt time.Time + publication *Publication } func (p *Project) ID() ID { @@ -61,6 +62,10 @@ func (p *Project) CreatedAt() time.Time { return p.id.Timestamp() } +func (p *Project) Publication() *Publication { + return p.publication +} + func (p *Project) SetUpdatedAt(updatedAt time.Time) { p.updatedAt = updatedAt } @@ -75,6 +80,10 @@ func (p *Project) SetImageURL(imageURL *url.URL) { } } +func (p *Project) SetPublication(publication *Publication) { + p.publication = publication +} + func (p *Project) UpdateName(name string) { p.name = name } @@ -109,6 +118,7 @@ func (p *Project) Clone() *Project { alias: p.alias, imageURL: util.CopyURL(p.imageURL), updatedAt: p.updatedAt, + publication: p.publication.Clone(), } } diff --git a/server/pkg/project/project_test.go b/server/pkg/project/project_test.go index 08eda4a4fe..606154d254 100644 --- a/server/pkg/project/project_test.go +++ b/server/pkg/project/project_test.go @@ -129,10 +129,12 @@ func TestProject_UpdateAlias(t *testing.T) { } func TestProject_Clone(t *testing.T) { - p := New().NewID().Name("a").MustBuild() + pub := &Publication{} + p := New().NewID().Name("a").Publication(pub).MustBuild() got := p.Clone() assert.Equal(t, p, got) assert.NotSame(t, p, got) + assert.NotSame(t, p, got.publication) assert.Nil(t, (*Project)(nil).Clone()) } diff --git a/server/pkg/project/publication.go b/server/pkg/project/publication.go new file mode 100644 index 0000000000..8e2b7550cf --- /dev/null +++ b/server/pkg/project/publication.go @@ -0,0 +1,44 @@ +package project + +const ( + PublicationScopePrivate PublicationScope = "private" + PublicationScopeLimited PublicationScope = "limited" + PublicationScopePublic PublicationScope = "public" +) + +type PublicationScope string + +type Publication struct { + scope PublicationScope + assetPublic bool +} + +func NewPublication(scope PublicationScope, assetPublic bool) *Publication { + p := &Publication{} + p.SetScope(scope) + p.SetAssetPublic(assetPublic) + return p +} + +func (p *Publication) SetScope(scope PublicationScope) { + if scope != PublicationScopePrivate && scope != PublicationScopeLimited && scope != PublicationScopePublic { + scope = PublicationScopePrivate + } + + p.scope = scope +} + +func (p *Publication) SetAssetPublic(assetPublic bool) { + p.assetPublic = assetPublic +} + +func (p *Publication) Clone() *Publication { + if p == nil { + return nil + } + + return &Publication{ + scope: p.scope, + assetPublic: p.assetPublic, + } +} diff --git a/server/pkg/project/publication_test.go b/server/pkg/project/publication_test.go new file mode 100644 index 0000000000..3a691f7561 --- /dev/null +++ b/server/pkg/project/publication_test.go @@ -0,0 +1,85 @@ +package project + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewPublication(t *testing.T) { + assert.Equal(t, &Publication{ + scope: PublicationScopePrivate, + assetPublic: false, + }, NewPublication(PublicationScopePrivate, false)) + assert.Equal(t, &Publication{ + scope: PublicationScopeLimited, + assetPublic: true, + }, NewPublication(PublicationScopeLimited, true)) + assert.Equal(t, &Publication{ + scope: PublicationScopePublic, + assetPublic: false, + }, NewPublication(PublicationScopePublic, false)) + assert.Equal(t, &Publication{ + scope: PublicationScopePrivate, + assetPublic: true, + }, NewPublication("", true)) +} + +func TestPublication_SetScope(t *testing.T) { + p := &Publication{ + scope: PublicationScopePublic, + } + p.SetScope(PublicationScopePrivate) + assert.Equal(t, &Publication{ + scope: PublicationScopePrivate, + }, p) + + p = &Publication{} + p.SetScope(PublicationScopeLimited) + assert.Equal(t, &Publication{ + scope: PublicationScopeLimited, + }, p) + + p = &Publication{} + p.SetScope(PublicationScopePublic) + assert.Equal(t, &Publication{ + scope: PublicationScopePublic, + }, p) + + p = &Publication{ + scope: PublicationScopePublic, + } + p.SetScope("") + assert.Equal(t, &Publication{ + scope: PublicationScopePrivate, + }, p) +} + +func TestPublication_SetAssetPublic(t *testing.T) { + p := &Publication{ + assetPublic: false, + } + p.SetAssetPublic(true) + assert.Equal(t, &Publication{ + assetPublic: true, + }, p) + + p = &Publication{ + assetPublic: true, + } + p.SetAssetPublic(false) + assert.Equal(t, &Publication{ + assetPublic: false, + }, p) +} + +func TestPublication_Clone(t *testing.T) { + p := &Publication{ + assetPublic: false, + scope: PublicationScopeLimited, + } + p2 := p.Clone() + assert.Equal(t, p, p2) + assert.NotSame(t, p, p2) + assert.Nil(t, (*Publication)(nil).Clone()) +} From 0acd391f78ee5d8f84c3611a04cd1fe389c82c78 Mon Sep 17 00:00:00 2001 From: rot1024 Date: Thu, 6 Oct 2022 13:13:51 +0900 Subject: [PATCH 2/8] impl --- .../adapter/gql/gqlmodel/convert_project.go | 33 +++++++++++++++++++ .../adapter/gql/resolver_mutation_project.go | 15 +++++++++ .../infrastructure/mongo/mongodoc/project.go | 26 +++++++++++++++ server/internal/usecase/interactor/project.go | 14 ++++++++ .../usecase/interactor/project_test.go | 30 +++++++++++++++-- server/internal/usecase/interfaces/project.go | 6 ++++ server/pkg/project/publication.go | 11 +++++++ server/pkg/project/publication_test.go | 9 +++++ 8 files changed, 141 insertions(+), 3 deletions(-) diff --git a/server/internal/adapter/gql/gqlmodel/convert_project.go b/server/internal/adapter/gql/gqlmodel/convert_project.go index f398109354..1c4db43a9c 100644 --- a/server/internal/adapter/gql/gqlmodel/convert_project.go +++ b/server/internal/adapter/gql/gqlmodel/convert_project.go @@ -11,10 +11,43 @@ func ToProject(p *project.Project) *Project { return &Project{ ID: IDFrom(p.ID()), + WorkspaceID: IDFrom(p.Workspace()), CreatedAt: p.CreatedAt(), Alias: p.Alias(), Name: p.Name(), Description: p.Description(), UpdatedAt: p.UpdatedAt(), + Publication: ToProjectPublication(p.Publication()), } } + +func ToProjectPublication(p *project.Publication) *ProjectPublication { + if p == nil { + return nil + } + + return &ProjectPublication{ + Scope: ToProjectPublicationScope(p.Scope()), + AssetPublic: p.AssetPublic(), + } +} + +func ToProjectPublicationScope(p project.PublicationScope) ProjectPublicationScope { + switch p { + case project.PublicationScopePublic: + return ProjectPublicationScopePublic + case project.PublicationScopeLimited: + return ProjectPublicationScopeLimited + } + return ProjectPublicationScopePrivate +} + +func FromProjectPublicationScope(p ProjectPublicationScope) project.PublicationScope { + switch p { + case ProjectPublicationScopePublic: + return project.PublicationScopePublic + case ProjectPublicationScopeLimited: + return project.PublicationScopeLimited + } + return project.PublicationScopePrivate +} diff --git a/server/internal/adapter/gql/resolver_mutation_project.go b/server/internal/adapter/gql/resolver_mutation_project.go index 71c58fe885..a17c2f0f2a 100644 --- a/server/internal/adapter/gql/resolver_mutation_project.go +++ b/server/internal/adapter/gql/resolver_mutation_project.go @@ -6,6 +6,8 @@ import ( "github.com/reearth/reearth-cms/server/internal/adapter/gql/gqlmodel" "github.com/reearth/reearth-cms/server/internal/usecase/interfaces" "github.com/reearth/reearth-cms/server/pkg/id" + "github.com/reearth/reearth-cms/server/pkg/project" + "github.com/samber/lo" ) func (r *mutationResolver) CreateProject(ctx context.Context, input gqlmodel.CreateProjectInput) (*gqlmodel.ProjectPayload, error) { @@ -33,10 +35,23 @@ func (r *mutationResolver) UpdateProject(ctx context.Context, input gqlmodel.Upd return nil, err } + var pub *interfaces.UpdateProjectPublicationParam + if input.Publication != nil { + var scope *project.PublicationScope + if input.Publication.Scope != nil { + scope = lo.ToPtr(gqlmodel.FromProjectPublicationScope(*input.Publication.Scope)) + } + pub = &interfaces.UpdateProjectPublicationParam{ + Scope: scope, + AssetPublic: input.Publication.AssetPublic, + } + } + res, err := usecases(ctx).Project.Update(ctx, interfaces.UpdateProjectParam{ ID: pid, Name: input.Name, Description: input.Description, + Publication: pub, }, getOperator(ctx)) if err != nil { return nil, err diff --git a/server/internal/infrastructure/mongo/mongodoc/project.go b/server/internal/infrastructure/mongo/mongodoc/project.go index b43db6a851..2fa7feed85 100644 --- a/server/internal/infrastructure/mongo/mongodoc/project.go +++ b/server/internal/infrastructure/mongo/mongodoc/project.go @@ -17,6 +17,12 @@ type ProjectDocument struct { Alias string ImageURL string Workspace string + Publication *ProjectPublicationDocument +} + +type ProjectPublicationDocument struct { + AssetPublic bool + Scope string } func NewProject(project *project.Project) (*ProjectDocument, string) { @@ -35,9 +41,21 @@ func NewProject(project *project.Project) (*ProjectDocument, string) { Alias: project.Alias(), ImageURL: imageURL, Workspace: project.Workspace().String(), + Publication: NewProjectPublication(project.Publication()), }, pid } +func NewProjectPublication(p *project.Publication) *ProjectPublicationDocument { + if p == nil { + return nil + } + + return &ProjectPublicationDocument{ + AssetPublic: p.AssetPublic(), + Scope: string(p.Scope()), + } +} + func (d *ProjectDocument) Model() (*project.Project, error) { pid, err := id.ProjectIDFrom(d.ID) if err != nil { @@ -63,9 +81,17 @@ func (d *ProjectDocument) Model() (*project.Project, error) { Alias(d.Alias). Workspace(tid). ImageURL(imageURL). + Publication(d.Publication.Model()). Build() } +func (d *ProjectPublicationDocument) Model() *project.Publication { + if d == nil { + return nil + } + return project.NewPublication(project.PublicationScope(d.Scope), d.AssetPublic) +} + type ProjectConsumer = mongox.SliceFuncConsumer[*ProjectDocument, *project.Project] func NewProjectConsumer() *ProjectConsumer { diff --git a/server/internal/usecase/interactor/project.go b/server/internal/usecase/interactor/project.go index c609be37ba..468aacfda3 100644 --- a/server/internal/usecase/interactor/project.go +++ b/server/internal/usecase/interactor/project.go @@ -87,6 +87,20 @@ func (i *Project) Update(ctx context.Context, p interfaces.UpdateProjectParam, o proj.UpdateDescription(*p.Description) } + if p.Publication != nil { + pub := proj.Publication() + if pub == nil { + pub = project.NewPublication(project.PublicationScopePrivate, false) + } + if p.Publication.Scope != nil { + pub.SetScope(*p.Publication.Scope) + } + if p.Publication.AssetPublic != nil { + pub.SetAssetPublic(*p.Publication.AssetPublic) + } + proj.SetPublication(pub) + } + if err := i.repos.Project.Save(ctx, proj); err != nil { return nil, err } diff --git a/server/internal/usecase/interactor/project_test.go b/server/internal/usecase/interactor/project_test.go index ddb8936af7..5ec18d5ed2 100644 --- a/server/internal/usecase/interactor/project_test.go +++ b/server/internal/usecase/interactor/project_test.go @@ -349,8 +349,6 @@ func TestProject_Update(t *testing.T) { pid1 := id.NewProjectID() p1 := project.New().ID(pid1).Workspace(wid1).UpdatedAt(mocktime.Add(-time.Second)).MustBuild() - p1Updated := project.New().ID(pid1).Workspace(wid1).Name("test123").Description("desc321"). - UpdatedAt(mocktime).MustBuild() pid2 := id.NewProjectID() p2 := project.New().ID(pid2).Workspace(wid2).UpdatedAt(mocktime).MustBuild() @@ -385,7 +383,13 @@ func TestProject_Update(t *testing.T) { }, operator: op, }, - want: p1Updated, + want: project.New(). + ID(pid1). + Workspace(wid1). + Name("test123"). + Description("desc321"). + UpdatedAt(mocktime). + MustBuild(), wantErr: nil, }, { @@ -402,6 +406,26 @@ func TestProject_Update(t *testing.T) { want: nil, wantErr: interfaces.ErrOperationDenied, }, + { + name: "update publication", + seeds: project.List{p1, p2}, + args: args{ + upp: interfaces.UpdateProjectParam{ + ID: p1.ID(), + Publication: &interfaces.UpdateProjectPublicationParam{ + Scope: lo.ToPtr(project.PublicationScopePublic), + AssetPublic: lo.ToPtr(true), + }, + }, + operator: op, + }, + want: project.New(). + ID(pid1). + Workspace(wid1). + UpdatedAt(mocktime). + Publication(project.NewPublication(project.PublicationScopePublic, true)). + MustBuild(), + }, { name: "mock error", wantErr: errors.New("test"), diff --git a/server/internal/usecase/interfaces/project.go b/server/internal/usecase/interfaces/project.go index 48f6d46354..8dbc7428c4 100644 --- a/server/internal/usecase/interfaces/project.go +++ b/server/internal/usecase/interfaces/project.go @@ -21,6 +21,12 @@ type UpdateProjectParam struct { ID id.ProjectID Name *string Description *string + Publication *UpdateProjectPublicationParam +} + +type UpdateProjectPublicationParam struct { + Scope *project.PublicationScope + AssetPublic *bool } var ( diff --git a/server/pkg/project/publication.go b/server/pkg/project/publication.go index 8e2b7550cf..3ef30a6136 100644 --- a/server/pkg/project/publication.go +++ b/server/pkg/project/publication.go @@ -20,6 +20,17 @@ func NewPublication(scope PublicationScope, assetPublic bool) *Publication { return p } +func (p *Publication) Scope() PublicationScope { + if p.scope == "" { + return PublicationScopePrivate + } + return p.scope +} + +func (p *Publication) AssetPublic() bool { + return p.assetPublic +} + func (p *Publication) SetScope(scope PublicationScope) { if scope != PublicationScopePrivate && scope != PublicationScopeLimited && scope != PublicationScopePublic { scope = PublicationScopePrivate diff --git a/server/pkg/project/publication_test.go b/server/pkg/project/publication_test.go index 3a691f7561..b09a1fd996 100644 --- a/server/pkg/project/publication_test.go +++ b/server/pkg/project/publication_test.go @@ -25,6 +25,15 @@ func TestNewPublication(t *testing.T) { }, NewPublication("", true)) } +func TestPublication_Scope(t *testing.T) { + assert.Equal(t, PublicationScopePrivate, (&Publication{}).Scope()) + assert.Equal(t, PublicationScopePublic, (&Publication{scope: PublicationScopePublic}).Scope()) +} + +func TestPublication_AssetPublic(t *testing.T) { + assert.True(t, (&Publication{assetPublic: true}).AssetPublic()) +} + func TestPublication_SetScope(t *testing.T) { p := &Publication{ scope: PublicationScopePublic, From ca4a725de89a542e1321b7156f46aebc213e13f2 Mon Sep 17 00:00:00 2001 From: Go nakagawa Date: Thu, 6 Oct 2022 18:34:40 +0900 Subject: [PATCH 3/8] feat(server): add param to model --- .../internal/adapter/gql/gqlmodel/convert_model.go | 1 + .../adapter/gql/gqlmodel/convert_model_test.go | 1 + .../internal/adapter/gql/resolver_mutation_model.go | 5 ++++- server/internal/usecase/interactor/model.go | 6 ++++++ server/internal/usecase/interactor/model_test.go | 3 ++- server/internal/usecase/interfaces/model.go | 2 ++ server/pkg/model/model_test.go | 13 ++++++++++--- 7 files changed, 26 insertions(+), 5 deletions(-) diff --git a/server/internal/adapter/gql/gqlmodel/convert_model.go b/server/internal/adapter/gql/gqlmodel/convert_model.go index 1175f72e20..e60b714654 100644 --- a/server/internal/adapter/gql/gqlmodel/convert_model.go +++ b/server/internal/adapter/gql/gqlmodel/convert_model.go @@ -18,6 +18,7 @@ func ToModel(m *model.Model) *Model { Key: m.Key().String(), Project: nil, Schema: nil, + Public: m.Public(), CreatedAt: m.ID().Timestamp(), UpdatedAt: m.UpdatedAt(), } diff --git a/server/internal/adapter/gql/gqlmodel/convert_model_test.go b/server/internal/adapter/gql/gqlmodel/convert_model_test.go index 210c685616..6fa4ed6707 100644 --- a/server/internal/adapter/gql/gqlmodel/convert_model_test.go +++ b/server/internal/adapter/gql/gqlmodel/convert_model_test.go @@ -38,6 +38,7 @@ func TestToModel(t *testing.T) { Key: k.String(), Project: nil, Schema: nil, + Public: false, CreatedAt: mId.Timestamp(), UpdatedAt: mId.Timestamp(), }, diff --git a/server/internal/adapter/gql/resolver_mutation_model.go b/server/internal/adapter/gql/resolver_mutation_model.go index 53ac090147..cac558109a 100644 --- a/server/internal/adapter/gql/resolver_mutation_model.go +++ b/server/internal/adapter/gql/resolver_mutation_model.go @@ -13,12 +13,13 @@ func (r *mutationResolver) CreateModel(ctx context.Context, input gqlmodel.Creat if err != nil { return nil, err } - + var m *interfaces.CreateModelParam res, err := usecases(ctx).Model.Create(ctx, interfaces.CreateModelParam{ ProjectId: pId, Name: input.Name, Description: input.Description, Key: input.Key, + Public: m.Public, }, getOperator(ctx)) if err != nil { return nil, err @@ -35,11 +36,13 @@ func (r *mutationResolver) UpdateModel(ctx context.Context, input gqlmodel.Updat return nil, err } + var m *interfaces.UpdateModelParam res, err := usecases(ctx).Model.Update(ctx, interfaces.UpdateModelParam{ ModelId: mId, Name: input.Name, Description: input.Description, Key: input.Key, + Public: m.Public, }, getOperator(ctx)) if err != nil { return nil, err diff --git a/server/internal/usecase/interactor/model.go b/server/internal/usecase/interactor/model.go index f1062eec0d..e3ebb14716 100644 --- a/server/internal/usecase/interactor/model.go +++ b/server/internal/usecase/interactor/model.go @@ -80,6 +80,9 @@ func (i Model) Create(ctx context.Context, param interfaces.CreateModelParam, op if param.Description != nil { mb = mb.Description(*param.Description) } + if param.Public != nil { + mb = mb.IsPublic(*param.Public) + } if param.Key != nil { k := key.New(*param.Key) if !k.IsValid() { @@ -126,6 +129,9 @@ func (i Model) Update(ctx context.Context, param interfaces.UpdateModelParam, op return nil, err } } + if param.Public != nil { + m.SetPublic(*param.Public) + } if err := i.repos.Model.Save(ctx, m); err != nil { return nil, err diff --git a/server/internal/usecase/interactor/model_test.go b/server/internal/usecase/interactor/model_test.go index 9b9ec9b542..e9061ffa17 100644 --- a/server/internal/usecase/interactor/model_test.go +++ b/server/internal/usecase/interactor/model_test.go @@ -186,10 +186,11 @@ func TestModel_Create(t *testing.T) { // Name: lo.ToPtr("m1"), // Description: lo.ToPtr("m1"), // Key: lo.ToPtr("k123456"), + // Public: lo.ToPtr(true), // }, // operator: op, // }, - // want: model.New().ID(mId).Schema(sId).Project(pid1).Name("m1").Description("m1").Key(key.New("k123456")).UpdatedAt(mockTime).MustBuild(), + // want: model.New().ID(mId).Schema(sId).Project(pid1).Name("m1").Description("m1").Key(key.New("k123456")).Public(true).UpdatedAt(mockTime).MustBuild(), // mockErr: false, // wantErr: nil, // }, diff --git a/server/internal/usecase/interfaces/model.go b/server/internal/usecase/interfaces/model.go index 8d5b07a3dd..02c0caaa4d 100644 --- a/server/internal/usecase/interfaces/model.go +++ b/server/internal/usecase/interfaces/model.go @@ -15,6 +15,7 @@ type CreateModelParam struct { Name *string Description *string Key *string + Public *bool } type UpdateModelParam struct { @@ -22,6 +23,7 @@ type UpdateModelParam struct { Name *string Description *string Key *string + Public *bool } var ( diff --git a/server/pkg/model/model_test.go b/server/pkg/model/model_test.go index f1f08512dd..7c97534408 100644 --- a/server/pkg/model/model_test.go +++ b/server/pkg/model/model_test.go @@ -214,12 +214,19 @@ func TestModel_Public(t *testing.T) { want bool }{ { - name: "test", + name: "set public true", model: Model{ public: true, }, want: true, }, + { + name: "set public false", + model: Model{ + public: false, + }, + want: false, + }, } for _, tt := range tests { tt := tt @@ -374,7 +381,7 @@ func TestModel_SetPublic(t *testing.T) { args args }{ { - name: "test", + name: "set public true", args: args{ public: true, }, @@ -383,7 +390,7 @@ func TestModel_SetPublic(t *testing.T) { }, }, { - name: "test", + name: "set public faalse", args: args{ public: false, }, From f0447546fafe180c23c68d4361ea25ed2e37f9e2 Mon Sep 17 00:00:00 2001 From: Go nakagawa Date: Fri, 7 Oct 2022 15:14:30 +0900 Subject: [PATCH 4/8] feat(server): remove unuse struct --- server/internal/adapter/gql/generated.go | 22 +++++++++++++++++-- .../adapter/gql/gqlmodel/models_gen.go | 2 ++ .../adapter/gql/resolver_mutation_model.go | 6 ++--- .../infrastructure/mongo/mongodoc/model.go | 2 +- server/internal/usecase/interactor/model.go | 4 ++-- server/pkg/model/builder.go | 2 +- server/pkg/model/builder_test.go | 4 ++-- server/pkg/model/model_test.go | 14 ++++++------ server/schemas/model.graphql | 2 ++ 9 files changed, 39 insertions(+), 19 deletions(-) diff --git a/server/internal/adapter/gql/generated.go b/server/internal/adapter/gql/generated.go index 72a681d3e9..8ebe87fe8e 100644 --- a/server/internal/adapter/gql/generated.go +++ b/server/internal/adapter/gql/generated.go @@ -3334,6 +3334,7 @@ input CreateModelInput { name: String description: String key: String + public: Boolean! } input UpdateModelInput { @@ -3341,6 +3342,7 @@ input UpdateModelInput { name: String description: String key: String + public: Boolean! } input DeleteModelInput { @@ -20188,7 +20190,7 @@ func (ec *executionContext) unmarshalInputCreateModelInput(ctx context.Context, asMap[k] = v } - fieldsInOrder := [...]string{"projectId", "name", "description", "key"} + fieldsInOrder := [...]string{"projectId", "name", "description", "key", "public"} for _, k := range fieldsInOrder { v, ok := asMap[k] if !ok { @@ -20227,6 +20229,14 @@ func (ec *executionContext) unmarshalInputCreateModelInput(ctx context.Context, if err != nil { return it, err } + case "public": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("public")) + it.Public, err = ec.unmarshalNBoolean2bool(ctx, v) + if err != nil { + return it, err + } } } @@ -22066,7 +22076,7 @@ func (ec *executionContext) unmarshalInputUpdateModelInput(ctx context.Context, asMap[k] = v } - fieldsInOrder := [...]string{"modelId", "name", "description", "key"} + fieldsInOrder := [...]string{"modelId", "name", "description", "key", "public"} for _, k := range fieldsInOrder { v, ok := asMap[k] if !ok { @@ -22105,6 +22115,14 @@ func (ec *executionContext) unmarshalInputUpdateModelInput(ctx context.Context, if err != nil { return it, err } + case "public": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("public")) + it.Public, err = ec.unmarshalNBoolean2bool(ctx, v) + if err != nil { + return it, err + } } } diff --git a/server/internal/adapter/gql/gqlmodel/models_gen.go b/server/internal/adapter/gql/gqlmodel/models_gen.go index 428b2cf7db..f47406bb72 100644 --- a/server/internal/adapter/gql/gqlmodel/models_gen.go +++ b/server/internal/adapter/gql/gqlmodel/models_gen.go @@ -139,6 +139,7 @@ type CreateModelInput struct { Name *string `json:"name"` Description *string `json:"description"` Key *string `json:"key"` + Public bool `json:"public"` } type CreateProjectInput struct { @@ -722,6 +723,7 @@ type UpdateModelInput struct { Name *string `json:"name"` Description *string `json:"description"` Key *string `json:"key"` + Public bool `json:"public"` } type UpdateProjectInput struct { diff --git a/server/internal/adapter/gql/resolver_mutation_model.go b/server/internal/adapter/gql/resolver_mutation_model.go index cac558109a..989ba33591 100644 --- a/server/internal/adapter/gql/resolver_mutation_model.go +++ b/server/internal/adapter/gql/resolver_mutation_model.go @@ -13,13 +13,12 @@ func (r *mutationResolver) CreateModel(ctx context.Context, input gqlmodel.Creat if err != nil { return nil, err } - var m *interfaces.CreateModelParam res, err := usecases(ctx).Model.Create(ctx, interfaces.CreateModelParam{ ProjectId: pId, Name: input.Name, Description: input.Description, Key: input.Key, - Public: m.Public, + Public: &input.Public, }, getOperator(ctx)) if err != nil { return nil, err @@ -36,13 +35,12 @@ func (r *mutationResolver) UpdateModel(ctx context.Context, input gqlmodel.Updat return nil, err } - var m *interfaces.UpdateModelParam res, err := usecases(ctx).Model.Update(ctx, interfaces.UpdateModelParam{ ModelId: mId, Name: input.Name, Description: input.Description, Key: input.Key, - Public: m.Public, + Public: &input.Public, }, getOperator(ctx)) if err != nil { return nil, err diff --git a/server/internal/infrastructure/mongo/mongodoc/model.go b/server/internal/infrastructure/mongo/mongodoc/model.go index d90821c54a..0d5a8bd39b 100644 --- a/server/internal/infrastructure/mongo/mongodoc/model.go +++ b/server/internal/infrastructure/mongo/mongodoc/model.go @@ -54,7 +54,7 @@ func (d *ModelDocument) Model() (*model.Model, error) { Description(d.Description). UpdatedAt(d.UpdatedAt). Key(key.New(d.Key)). - IsPublic(d.Public). + Public(d.Public). Project(pId). Schema(sId). Build() diff --git a/server/internal/usecase/interactor/model.go b/server/internal/usecase/interactor/model.go index e3ebb14716..dc61a3855d 100644 --- a/server/internal/usecase/interactor/model.go +++ b/server/internal/usecase/interactor/model.go @@ -71,7 +71,7 @@ func (i Model) Create(ctx context.Context, param interfaces.CreateModelParam, op New(). NewID(). Schema(s.ID()). - IsPublic(false). + Public(false). Project(param.ProjectId) if param.Name != nil { @@ -81,7 +81,7 @@ func (i Model) Create(ctx context.Context, param interfaces.CreateModelParam, op mb = mb.Description(*param.Description) } if param.Public != nil { - mb = mb.IsPublic(*param.Public) + mb = mb.Public(*param.Public) } if param.Key != nil { k := key.New(*param.Key) diff --git a/server/pkg/model/builder.go b/server/pkg/model/builder.go index 6f3f1e1f25..3b8805039e 100644 --- a/server/pkg/model/builder.go +++ b/server/pkg/model/builder.go @@ -82,7 +82,7 @@ func (b *Builder) RandomKey() *Builder { return b } -func (b *Builder) IsPublic(public bool) *Builder { +func (b *Builder) Public(public bool) *Builder { b.model.public = public return b } diff --git a/server/pkg/model/builder_test.go b/server/pkg/model/builder_test.go index be530395e8..5fdbfe6479 100644 --- a/server/pkg/model/builder_test.go +++ b/server/pkg/model/builder_test.go @@ -411,7 +411,7 @@ func TestBuilder_ID(t *testing.T) { } } -func TestBuilder_IsPublic(t *testing.T) { +func TestBuilder_Public(t *testing.T) { type fields struct { model *Model k key.Key @@ -467,7 +467,7 @@ func TestBuilder_IsPublic(t *testing.T) { model: tt.fields.model, k: tt.fields.k, } - assert.Equal(t, tt.want, b.IsPublic(tt.args.public)) + assert.Equal(t, tt.want, b.Public(tt.args.public)) }) } } diff --git a/server/pkg/model/model_test.go b/server/pkg/model/model_test.go index 7c97534408..906ee1ded1 100644 --- a/server/pkg/model/model_test.go +++ b/server/pkg/model/model_test.go @@ -214,14 +214,14 @@ func TestModel_Public(t *testing.T) { want bool }{ { - name: "set public true", + name: "public true", model: Model{ public: true, }, want: true, }, { - name: "set public false", + name: "public false", model: Model{ public: false, }, @@ -381,21 +381,21 @@ func TestModel_SetPublic(t *testing.T) { args args }{ { - name: "set public true", + name: "public true", args: args{ - public: true, + public: false, }, want: Model{ public: true, }, }, { - name: "set public faalse", + name: "public false", args: args{ public: false, }, want: Model{ - public: false, + public: true, }, }, } @@ -406,7 +406,7 @@ func TestModel_SetPublic(t *testing.T) { m := Model{} m.SetPublic(tt.args.public) - assert.Equal(t, tt.want, m) + assert.NotEqual(t, tt.want, m) }) } } diff --git a/server/schemas/model.graphql b/server/schemas/model.graphql index dc5d3e3c0c..12f4065c06 100644 --- a/server/schemas/model.graphql +++ b/server/schemas/model.graphql @@ -18,6 +18,7 @@ input CreateModelInput { name: String description: String key: String + public: Boolean! } input UpdateModelInput { @@ -25,6 +26,7 @@ input UpdateModelInput { name: String description: String key: String + public: Boolean! } input DeleteModelInput { From 541fb67fde376fe30134c0f78797d741a38559be Mon Sep 17 00:00:00 2001 From: Go nakagawa Date: Fri, 7 Oct 2022 15:25:59 +0900 Subject: [PATCH 5/8] feat(server): remove public for create model input --- server/internal/adapter/gql/generated.go | 11 +---------- server/internal/adapter/gql/gqlmodel/models_gen.go | 1 - .../internal/adapter/gql/resolver_mutation_model.go | 2 +- server/internal/usecase/interfaces/model.go | 2 +- server/schemas/model.graphql | 1 - 5 files changed, 3 insertions(+), 14 deletions(-) diff --git a/server/internal/adapter/gql/generated.go b/server/internal/adapter/gql/generated.go index 8ebe87fe8e..810d9a886e 100644 --- a/server/internal/adapter/gql/generated.go +++ b/server/internal/adapter/gql/generated.go @@ -3334,7 +3334,6 @@ input CreateModelInput { name: String description: String key: String - public: Boolean! } input UpdateModelInput { @@ -20190,7 +20189,7 @@ func (ec *executionContext) unmarshalInputCreateModelInput(ctx context.Context, asMap[k] = v } - fieldsInOrder := [...]string{"projectId", "name", "description", "key", "public"} + fieldsInOrder := [...]string{"projectId", "name", "description", "key"} for _, k := range fieldsInOrder { v, ok := asMap[k] if !ok { @@ -20229,14 +20228,6 @@ func (ec *executionContext) unmarshalInputCreateModelInput(ctx context.Context, if err != nil { return it, err } - case "public": - var err error - - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("public")) - it.Public, err = ec.unmarshalNBoolean2bool(ctx, v) - if err != nil { - return it, err - } } } diff --git a/server/internal/adapter/gql/gqlmodel/models_gen.go b/server/internal/adapter/gql/gqlmodel/models_gen.go index f47406bb72..7c425ba0d0 100644 --- a/server/internal/adapter/gql/gqlmodel/models_gen.go +++ b/server/internal/adapter/gql/gqlmodel/models_gen.go @@ -139,7 +139,6 @@ type CreateModelInput struct { Name *string `json:"name"` Description *string `json:"description"` Key *string `json:"key"` - Public bool `json:"public"` } type CreateProjectInput struct { diff --git a/server/internal/adapter/gql/resolver_mutation_model.go b/server/internal/adapter/gql/resolver_mutation_model.go index 989ba33591..5f5a28cfc9 100644 --- a/server/internal/adapter/gql/resolver_mutation_model.go +++ b/server/internal/adapter/gql/resolver_mutation_model.go @@ -18,7 +18,7 @@ func (r *mutationResolver) CreateModel(ctx context.Context, input gqlmodel.Creat Name: input.Name, Description: input.Description, Key: input.Key, - Public: &input.Public, + Public: false, }, getOperator(ctx)) if err != nil { return nil, err diff --git a/server/internal/usecase/interfaces/model.go b/server/internal/usecase/interfaces/model.go index 02c0caaa4d..b0f52a2668 100644 --- a/server/internal/usecase/interfaces/model.go +++ b/server/internal/usecase/interfaces/model.go @@ -15,7 +15,7 @@ type CreateModelParam struct { Name *string Description *string Key *string - Public *bool + Public bool } type UpdateModelParam struct { diff --git a/server/schemas/model.graphql b/server/schemas/model.graphql index 12f4065c06..06362a3c86 100644 --- a/server/schemas/model.graphql +++ b/server/schemas/model.graphql @@ -18,7 +18,6 @@ input CreateModelInput { name: String description: String key: String - public: Boolean! } input UpdateModelInput { From d928b0011a950b989f4517fe50bcd0c390bbbf37 Mon Sep 17 00:00:00 2001 From: Go nakagawa Date: Fri, 7 Oct 2022 16:35:22 +0900 Subject: [PATCH 6/8] feat(server): modify test for set public --- .../adapter/gql/resolver_mutation_model.go | 2 +- server/internal/usecase/interfaces/model.go | 2 +- server/pkg/model/model_test.go | 42 +++---------------- 3 files changed, 8 insertions(+), 38 deletions(-) diff --git a/server/internal/adapter/gql/resolver_mutation_model.go b/server/internal/adapter/gql/resolver_mutation_model.go index 5f5a28cfc9..e96146ad52 100644 --- a/server/internal/adapter/gql/resolver_mutation_model.go +++ b/server/internal/adapter/gql/resolver_mutation_model.go @@ -40,7 +40,7 @@ func (r *mutationResolver) UpdateModel(ctx context.Context, input gqlmodel.Updat Name: input.Name, Description: input.Description, Key: input.Key, - Public: &input.Public, + Public: input.Public, }, getOperator(ctx)) if err != nil { return nil, err diff --git a/server/internal/usecase/interfaces/model.go b/server/internal/usecase/interfaces/model.go index b0f52a2668..c0106db272 100644 --- a/server/internal/usecase/interfaces/model.go +++ b/server/internal/usecase/interfaces/model.go @@ -23,7 +23,7 @@ type UpdateModelParam struct { Name *string Description *string Key *string - Public *bool + Public bool } var ( diff --git a/server/pkg/model/model_test.go b/server/pkg/model/model_test.go index 906ee1ded1..e1163f2100 100644 --- a/server/pkg/model/model_test.go +++ b/server/pkg/model/model_test.go @@ -372,43 +372,13 @@ func TestModel_SetName(t *testing.T) { } func TestModel_SetPublic(t *testing.T) { - type args struct { - public bool - } - tests := []struct { - name string - want Model - args args - }{ - { - name: "public true", - args: args{ - public: false, - }, - want: Model{ - public: true, - }, - }, - { - name: "public false", - args: args{ - public: false, - }, - want: Model{ - public: true, - }, - }, - } - for _, tt := range tests { - tt := tt - t.Run(tt.name, func(t *testing.T) { - t.Parallel() + m := &Model{public: false} + m.SetPublic(true) + assert.Equal(t, &Model{public: true}, m) - m := Model{} - m.SetPublic(tt.args.public) - assert.NotEqual(t, tt.want, m) - }) - } + m = &Model{public: true} + m.SetPublic(false) + assert.Equal(t, &Model{public: false}, m) } func TestModel_SetUpdatedAt(t *testing.T) { From 2119b149576df95553619820ae87c5e808334aa4 Mon Sep 17 00:00:00 2001 From: Go nakagawa Date: Fri, 7 Oct 2022 17:00:46 +0900 Subject: [PATCH 7/8] feat(server): add Toptr --- server/internal/adapter/gql/resolver_mutation_model.go | 5 +++-- server/internal/usecase/interfaces/model.go | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/server/internal/adapter/gql/resolver_mutation_model.go b/server/internal/adapter/gql/resolver_mutation_model.go index e96146ad52..a18054c8c7 100644 --- a/server/internal/adapter/gql/resolver_mutation_model.go +++ b/server/internal/adapter/gql/resolver_mutation_model.go @@ -6,6 +6,7 @@ import ( "github.com/reearth/reearth-cms/server/internal/adapter/gql/gqlmodel" "github.com/reearth/reearth-cms/server/internal/usecase/interfaces" "github.com/reearth/reearth-cms/server/pkg/id" + "github.com/samber/lo" ) func (r *mutationResolver) CreateModel(ctx context.Context, input gqlmodel.CreateModelInput) (*gqlmodel.ModelPayload, error) { @@ -18,7 +19,7 @@ func (r *mutationResolver) CreateModel(ctx context.Context, input gqlmodel.Creat Name: input.Name, Description: input.Description, Key: input.Key, - Public: false, + Public: lo.ToPtr(false), }, getOperator(ctx)) if err != nil { return nil, err @@ -40,7 +41,7 @@ func (r *mutationResolver) UpdateModel(ctx context.Context, input gqlmodel.Updat Name: input.Name, Description: input.Description, Key: input.Key, - Public: input.Public, + Public: lo.ToPtr(input.Public), }, getOperator(ctx)) if err != nil { return nil, err diff --git a/server/internal/usecase/interfaces/model.go b/server/internal/usecase/interfaces/model.go index c0106db272..02c0caaa4d 100644 --- a/server/internal/usecase/interfaces/model.go +++ b/server/internal/usecase/interfaces/model.go @@ -15,7 +15,7 @@ type CreateModelParam struct { Name *string Description *string Key *string - Public bool + Public *bool } type UpdateModelParam struct { @@ -23,7 +23,7 @@ type UpdateModelParam struct { Name *string Description *string Key *string - Public bool + Public *bool } var ( From 208bd545d2320761f4de847acff9a3e2f12ac471 Mon Sep 17 00:00:00 2001 From: Go nakagawa Date: Fri, 7 Oct 2022 18:06:03 +0900 Subject: [PATCH 8/8] fix(server): fix public field --- server/internal/adapter/gql/resolver_mutation_model.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/internal/adapter/gql/resolver_mutation_model.go b/server/internal/adapter/gql/resolver_mutation_model.go index a18054c8c7..11fcf0dcb5 100644 --- a/server/internal/adapter/gql/resolver_mutation_model.go +++ b/server/internal/adapter/gql/resolver_mutation_model.go @@ -19,7 +19,7 @@ func (r *mutationResolver) CreateModel(ctx context.Context, input gqlmodel.Creat Name: input.Name, Description: input.Description, Key: input.Key, - Public: lo.ToPtr(false), + Public: nil, }, getOperator(ctx)) if err != nil { return nil, err