diff --git a/account/accountinfrastructure/accountmemory/workspace.go b/account/accountinfrastructure/accountmemory/workspace.go index bf84234..7b7c70f 100644 --- a/account/accountinfrastructure/accountmemory/workspace.go +++ b/account/accountinfrastructure/accountmemory/workspace.go @@ -76,7 +76,7 @@ func (r *Workspace) FindByID(_ context.Context, v workspace.ID) (*workspace.Work }), rerror.ErrNotFound) } -func (r *Workspace) NewOne(_ context.Context, t *workspace.Workspace) error { +func (r *Workspace) Create(_ context.Context, t *workspace.Workspace) error { if r.err != nil { return r.err } diff --git a/account/accountinfrastructure/accountmongo/workspace.go b/account/accountinfrastructure/accountmongo/workspace.go index eb648e5..c4c4c73 100644 --- a/account/accountinfrastructure/accountmongo/workspace.go +++ b/account/accountinfrastructure/accountmongo/workspace.go @@ -88,16 +88,7 @@ func (r *Workspace) FindByID(ctx context.Context, id accountdomain.WorkspaceID) func (r *Workspace) Create(ctx context.Context, workspace *workspace.Workspace) error { doc, id := mongodoc.NewWorkspace(workspace) - return r.client.NewOne(ctx, id, doc) -} - -func (r *Workspace) NewOne(ctx context.Context, workspace *workspace.Workspace) error { - if !r.f.CanWrite(workspace.ID()) { - return accountrepo.ErrOperationDenied - } - - doc, id := mongodoc.NewWorkspace(workspace) - return r.client.NewOne(ctx, id, doc) + return r.client.CreateOne(ctx, id, doc) } func (r *Workspace) Save(ctx context.Context, workspace *workspace.Workspace) error { diff --git a/account/accountusecase/accountinteractor/workspace.go b/account/accountusecase/accountinteractor/workspace.go index 7b57435..5e4a8b2 100644 --- a/account/accountusecase/accountinteractor/workspace.go +++ b/account/accountusecase/accountinteractor/workspace.go @@ -70,7 +70,7 @@ func (i *Workspace) Create(ctx context.Context, name string, firstUser workspace return nil, err } - if err := i.repos.Workspace.NewOne(ctx, ws); err != nil { + if err := i.repos.Workspace.Create(ctx, ws); err != nil { return nil, err } diff --git a/account/accountusecase/accountrepo/workspace.go b/account/accountusecase/accountrepo/workspace.go index 79ff7b8..60b152c 100644 --- a/account/accountusecase/accountrepo/workspace.go +++ b/account/accountusecase/accountrepo/workspace.go @@ -13,7 +13,7 @@ type Workspace interface { FindByIDs(context.Context, workspace.IDList) (workspace.List, error) FindByUser(context.Context, user.ID) (workspace.List, error) FindByIntegration(context.Context, workspace.IntegrationID) (workspace.List, error) - NewOne(context.Context, *workspace.Workspace) error + Create(context.Context, *workspace.Workspace) error Save(context.Context, *workspace.Workspace) error SaveAll(context.Context, workspace.List) error Remove(context.Context, workspace.ID) error diff --git a/mongox/collection.go b/mongox/collection.go index a082fb1..5fb7a31 100644 --- a/mongox/collection.go +++ b/mongox/collection.go @@ -192,7 +192,11 @@ func (c *Collection) RemoveOne(ctx context.Context, f any) error { return nil } -func (c *Collection) NewOne(ctx context.Context, id string, doc any) error { +func (c *Collection) CreateOne(ctx context.Context, id string, doc any) error { + if id == "" { + return errors.New("id is empty") + } + count, err := c.collection.CountDocuments(ctx, bson.M{idKey: id}) if err != nil { return wrapError(ctx, err)