Skip to content

Commit

Permalink
fix(account): fix workspace creation
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Mar 8, 2024
1 parent 6b50be0 commit 890210a
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 20 deletions.
13 changes: 13 additions & 0 deletions account/accountinfrastructure/accountmemory/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ func (r *Workspace) FindByID(_ context.Context, v workspace.ID) (*workspace.Work
}), rerror.ErrNotFound)
}

func (r *Workspace) NewOne(_ context.Context, t *workspace.Workspace) error {
if r.err != nil {
return r.err
}

if _, ok := r.data.Load(t.ID()); ok {
return rerror.ErrAlreadyExists
}

r.data.Store(t.ID(), t)
return nil
}

func (r *Workspace) Save(_ context.Context, t *workspace.Workspace) error {
if r.err != nil {
return r.err
Expand Down
14 changes: 14 additions & 0 deletions account/accountinfrastructure/accountmongo/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ func (r *Workspace) FindByID(ctx context.Context, id accountdomain.WorkspaceID)
return r.findOne(ctx, bson.M{"id": id.String()})
}

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)
}

func (r *Workspace) Save(ctx context.Context, workspace *workspace.Workspace) error {
if !r.f.CanWrite(workspace.ID()) {
return accountrepo.ErrOperationDenied
Expand Down
2 changes: 1 addition & 1 deletion account/accountusecase/accountinteractor/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (i *Workspace) Create(ctx context.Context, name string, firstUser workspace
return nil, err
}

if err := i.repos.Workspace.Save(ctx, ws); err != nil {
if err := i.repos.Workspace.NewOne(ctx, ws); err != nil {
return nil, err
}

Expand Down
1 change: 1 addition & 0 deletions account/accountusecase/accountrepo/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +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
Save(context.Context, *workspace.Workspace) error
SaveAll(context.Context, workspace.List) error
Remove(context.Context, workspace.ID) error
Expand Down
7 changes: 1 addition & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
golang.org/x/crypto v0.18.0
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a
golang.org/x/text v0.14.0
gopkg.in/go-jose/go-jose.v2 v2.6.2
gopkg.in/square/go-jose.v2 v2.6.0
)

Expand All @@ -65,7 +66,6 @@ require (
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.7 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.26.7 // indirect
github.com/aws/smithy-go v1.19.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/trifles v0.0.0-20200705224438-cafc02a1ee2b // indirect
github.com/fatih/color v1.16.0 // indirect
Expand All @@ -88,25 +88,21 @@ require (
github.com/klauspost/compress v1.13.6 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/rs/cors v1.10.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sendgrid/rest v2.6.9+incompatible // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sosodev/duration v1.2.0 // indirect
github.com/stretchr/objx v0.5.1 // indirect
github.com/urfave/cli/v2 v2.27.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
github.com/zitadel/logging v0.3.4 // indirect
go.opencensus.io v0.24.0 // indirect
Expand All @@ -131,7 +127,6 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe // indirect
google.golang.org/grpc v1.61.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/go-jose/go-jose.v2 v2.6.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 0 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/xds/go v0.0.0-20231109132714-523115ebc101 h1:7To3pQ+pZo0i3dsWEbinPNFs5gPSBOsJtx3wTT94VBY=
github.com/cncf/xds/go v0.0.0-20231109132714-523115ebc101/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -231,7 +229,6 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g=
github.com/maxatome/go-testdeep v1.12.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM=
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/mitchellh/mapstructure v0.0.0-20170523030023-d0303fe80992/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand Down Expand Up @@ -259,8 +256,6 @@ github.com/ravilushqa/otelgqlgen v0.15.0 h1:U85nrlweMXTGaMChUViYM39/MXBZVeVVlpuH
github.com/ravilushqa/otelgqlgen v0.15.0/go.mod h1:o+1Eju0VySmgq2BP8Vupz2YrN21Bj7D7imBqu3m2uB8=
github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo=
github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/sendgrid/rest v2.6.9+incompatible h1:1EyIcsNdn9KIisLW50MKwmSRSK+ekueiEMJ7NEoxJo0=
Expand Down Expand Up @@ -300,8 +295,6 @@ github.com/uber/jaeger-client-go v2.30.0+incompatible h1:D6wyKGCecFaSRUpo8lCVbaO
github.com/uber/jaeger-client-go v2.30.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
github.com/uber/jaeger-lib v2.4.1+incompatible h1:td4jdvLcExb4cBISKIpHuGoVXh+dVKhn2Um6rjCsSsg=
github.com/uber/jaeger-lib v2.4.1+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
Expand All @@ -314,8 +307,6 @@ github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e h1:+SOyEddqYF09QP7vr7CgJ1eti3pY9Fn3LHO1M1r/0sI=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down
26 changes: 22 additions & 4 deletions mongox/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,32 @@ func (c *Collection) RemoveOne(ctx context.Context, f any) error {
return nil
}

func (c *Collection) SaveOne(ctx context.Context, id string, replacement any) error {
return c.ReplaceOne(ctx, bson.M{idKey: id}, replacement)
func (c *Collection) NewOne(ctx context.Context, id string, doc any) error {
count, err := c.collection.CountDocuments(ctx, bson.M{idKey: id})
if err != nil {
return wrapError(ctx, err)
}

if count > 0 {
return rerror.ErrAlreadyExists
}

_, err = c.collection.UpdateOne(
ctx,
bson.M{idKey: id},
bson.M{"$setOnInsert": doc},
options.Update().SetUpsert(true),
)
if err != nil {
return wrapError(ctx, err)
}
return nil
}

func (c *Collection) ReplaceOne(ctx context.Context, filter any, replacement any) error {
func (c *Collection) SaveOne(ctx context.Context, id string, replacement any) error {
_, err := c.collection.ReplaceOne(
ctx,
filter,
bson.M{idKey: id},
replacement,
options.Replace().SetUpsert(true),
)
Expand Down
4 changes: 4 additions & 0 deletions rerror/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
const (
IDErrInternal = "internal"
IDErrNotFound = "not found"
IDErrAlreadyExists = "already exists"
IDErrInvalidParams = "invalid params"
IDErrNotImplemented = "not implemented"
)
Expand All @@ -22,13 +23,16 @@ var (
errInternal = WrapE(&i18n.Message{ID: IDErrInternal}, errInternalRaw)
// ErrNotFound indicates something was not found.
ErrNotFound = WrapE(&i18n.Message{ID: IDErrNotFound}, ErrNotFoundRaw)
// ErrAlreadyExists indicates something was already exists.
ErrAlreadyExists = WrapE(&i18n.Message{ID: IDErrAlreadyExists}, ErrAlreadyExistsRaw)
// ErrInvalidParams represents the params are invalid, such as empty string.
ErrInvalidParams = WrapE(&i18n.Message{ID: IDErrInvalidParams}, ErrInvalidParamsRaw)
// ErrNotImplemented indicates unimplemented.
ErrNotImplemented = WrapE(&i18n.Message{ID: IDErrNotImplemented}, ErrNotImplementedRaw)

errInternalRaw = errors.New("internal")
ErrNotFoundRaw = errors.New("not found")
ErrAlreadyExistsRaw = errors.New("already exists")
ErrInvalidParamsRaw = errors.New("invalid params")
ErrNotImplementedRaw = errors.New("not implemented")
)
Expand Down

0 comments on commit 890210a

Please sign in to comment.