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
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
}
5 changes: 4 additions & 1 deletion server/internal/adapter/gql/resolver_mutation_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ func (r *mutationResolver) CreateModel(ctx context.Context, input gqlmodel.Creat
if err != nil {
return nil, err
}

var m *interfaces.CreateModelParam
gop50k marked this conversation as resolved.
Show resolved Hide resolved
res, err := usecases(ctx).Model.Create(ctx, interfaces.CreateModelParam{
ProjectId: pId,
Name: input.Name,
Description: input.Description,
Key: input.Key,
Public: m.Public,
gop50k marked this conversation as resolved.
Show resolved Hide resolved
}, getOperator(ctx))
if err != nil {
return nil, err
Expand All @@ -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,
gop50k marked this conversation as resolved.
Show resolved Hide resolved
}, 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
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
6 changes: 6 additions & 0 deletions server/internal/usecase/interactor/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
gop50k marked this conversation as resolved.
Show resolved Hide resolved
}
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
13 changes: 10 additions & 3 deletions server/pkg/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,19 @@ func TestModel_Public(t *testing.T) {
want bool
}{
{
name: "test",
name: "set public true",
gop50k marked this conversation as resolved.
Show resolved Hide resolved
model: Model{
public: true,
},
want: true,
},
{
name: "set public false",
gop50k marked this conversation as resolved.
Show resolved Hide resolved
model: Model{
public: false,
},
want: false,
},
}
for _, tt := range tests {
tt := tt
Expand Down Expand Up @@ -374,7 +381,7 @@ func TestModel_SetPublic(t *testing.T) {
args args
}{
{
name: "test",
name: "set public true",
args: args{
public: true,
rot1024 marked this conversation as resolved.
Show resolved Hide resolved
},
Expand All @@ -383,7 +390,7 @@ func TestModel_SetPublic(t *testing.T) {
},
},
{
name: "test",
name: "set public faalse",
args: args{
public: false,
gop50k marked this conversation as resolved.
Show resolved Hide resolved
},
Expand Down
5 changes: 5 additions & 0 deletions server/pkg/project/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 9 additions & 0 deletions server/pkg/project/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
Expand Down
Loading