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

feat(server): Implement from domain models to Adapter on the public #267

Merged
merged 10 commits into from
Oct 7, 2022
11 changes: 10 additions & 1 deletion server/internal/adapter/gql/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/internal/adapter/gql/gqlmodel/convert_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestToModel(t *testing.T) {
Key: k.String(),
Project: nil,
Schema: nil,
Public: false,
CreatedAt: mId.Timestamp(),
UpdatedAt: mId.Timestamp(),
},
Expand Down
33 changes: 33 additions & 0 deletions server/internal/adapter/gql/gqlmodel/convert_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions server/internal/adapter/gql/gqlmodel/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion server/internal/adapter/gql/resolver_mutation_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ 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) {
pId, err := gqlmodel.ToID[id.Project](input.ProjectID)
if err != nil {
return nil, err
}

res, err := usecases(ctx).Model.Create(ctx, interfaces.CreateModelParam{
ProjectId: pId,
Name: input.Name,
Description: input.Description,
Key: input.Key,
Public: nil,
}, getOperator(ctx))
if err != nil {
return nil, err
Expand All @@ -40,6 +41,7 @@ func (r *mutationResolver) UpdateModel(ctx context.Context, input gqlmodel.Updat
Name: input.Name,
Description: input.Description,
Key: input.Key,
Public: lo.ToPtr(input.Public),
}, getOperator(ctx))
if err != nil {
return nil, err
Expand Down
15 changes: 15 additions & 0 deletions server/internal/adapter/gql/resolver_mutation_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server/internal/infrastructure/mongo/mongodoc/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
26 changes: 26 additions & 0 deletions server/internal/infrastructure/mongo/mongodoc/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion server/internal/usecase/interactor/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.Public(*param.Public)
}
if param.Key != nil {
k := key.New(*param.Key)
if !k.IsValid() {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion server/internal/usecase/interactor/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
// },
Expand Down
14 changes: 14 additions & 0 deletions server/internal/usecase/interactor/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
30 changes: 27 additions & 3 deletions server/internal/usecase/interactor/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
},
{
Expand All @@ -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"),
Expand Down
2 changes: 2 additions & 0 deletions server/internal/usecase/interfaces/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ type CreateModelParam struct {
Name *string
Description *string
Key *string
Public *bool
}

type UpdateModelParam struct {
ModelId id.ModelID
Name *string
Description *string
Key *string
Public *bool
}

var (
Expand Down
6 changes: 6 additions & 0 deletions server/internal/usecase/interfaces/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion server/pkg/model/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions server/pkg/model/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
})
}
}
Expand Down
Loading