Skip to content

Commit

Permalink
use uniqueness detection provided by lungo
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Dec 20, 2019
1 parent 1e6d0bc commit d190a29
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 31 deletions.
21 changes: 0 additions & 21 deletions coal/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,24 +167,3 @@ func ToD(model Model) bson.D {

return d
}

// IsDuplicateError returns true if the provided error is generated due to a
// duplicate document failing a unique index constraint.
func IsDuplicateError(err error) bool {
// check error
if err == nil {
return false
}

// ge string
str := err.Error()

// check if duplicate key error
if strings.Contains(str, "duplicate key error") {
return true
} else if strings.Contains(str, "duplicate document for index") {
return true
}

return false
}
15 changes: 8 additions & 7 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/256dpi/jsonapi"
"github.com/256dpi/lungo"
"github.com/256dpi/serve"
"github.com/256dpi/stack"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -495,7 +496,7 @@ func (c *Controller) createResource(ctx *Context, doc *jsonapi.Document) {
}, bson.M{
"$setOnInsert": ctx.Model,
}, options.Update().SetUpsert(true))
if coal.IsDuplicateError(err) {
if lungo.IsUniquenessError(err) {
stack.Abort(jsonapi.ErrorFromStatus(http.StatusConflict, "document is not unique"))
}
stack.AbortIf(err)
Expand All @@ -507,7 +508,7 @@ func (c *Controller) createResource(ctx *Context, doc *jsonapi.Document) {
} else {
// insert model
_, err := ctx.TC(ctx.Model).InsertOne(ctx, ctx.Model)
if coal.IsDuplicateError(err) {
if lungo.IsUniquenessError(err) {
stack.Abort(jsonapi.ErrorFromStatus(http.StatusConflict, "document is not unique"))
}
stack.AbortIf(err)
Expand Down Expand Up @@ -616,7 +617,7 @@ func (c *Controller) updateResource(ctx *Context, doc *jsonapi.Document) {
}, bson.M{
"$set": ctx.Model,
})
if coal.IsDuplicateError(err) {
if lungo.IsUniquenessError(err) {
stack.Abort(jsonapi.ErrorFromStatus(http.StatusConflict, "document is not unique"))
}
stack.AbortIf(err)
Expand All @@ -630,7 +631,7 @@ func (c *Controller) updateResource(ctx *Context, doc *jsonapi.Document) {
_, err := ctx.TC(ctx.Model).ReplaceOne(ctx, bson.M{
"_id": ctx.Model.ID(),
}, ctx.Model)
if coal.IsDuplicateError(err) {
if lungo.IsUniquenessError(err) {
stack.Abort(jsonapi.ErrorFromStatus(http.StatusConflict, "document is not unique"))
}
stack.AbortIf(err)
Expand Down Expand Up @@ -1042,7 +1043,7 @@ func (c *Controller) setRelationship(ctx *Context, doc *jsonapi.Document) {
_, err := ctx.TC(ctx.Model).ReplaceOne(ctx, bson.M{
"_id": ctx.Model.ID(),
}, ctx.Model)
if coal.IsDuplicateError(err) {
if lungo.IsUniquenessError(err) {
stack.Abort(jsonapi.ErrorFromStatus(http.StatusConflict, "document is not unique"))
}
stack.AbortIf(err)
Expand Down Expand Up @@ -1132,7 +1133,7 @@ func (c *Controller) appendToRelationship(ctx *Context, doc *jsonapi.Document) {
_, err := ctx.TC(ctx.Model).ReplaceOne(ctx, bson.M{
"_id": ctx.Model.ID(),
}, ctx.Model)
if coal.IsDuplicateError(err) {
if lungo.IsUniquenessError(err) {
stack.Abort(jsonapi.ErrorFromStatus(http.StatusConflict, "document is not unique"))
}
stack.AbortIf(err)
Expand Down Expand Up @@ -1229,7 +1230,7 @@ func (c *Controller) removeFromRelationship(ctx *Context, doc *jsonapi.Document)
_, err := ctx.TC(ctx.Model).ReplaceOne(ctx, bson.M{
"_id": ctx.Model.ID(),
}, ctx.Model)
if coal.IsDuplicateError(err) {
if lungo.IsUniquenessError(err) {
stack.Abort(jsonapi.ErrorFromStatus(http.StatusConflict, "document is not unique"))
}
stack.AbortIf(err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/256dpi/fire

require (
github.com/256dpi/jsonapi v1.3.0
github.com/256dpi/lungo v0.1.6
github.com/256dpi/lungo v0.1.7
github.com/256dpi/oauth2 v1.3.0
github.com/256dpi/serve v0.1.2
github.com/256dpi/stack v0.3.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/256dpi/btree v0.0.0-20191029155653-36fa32f443ba h1:ZrWPG6s22qEsCB2M4h
github.com/256dpi/btree v0.0.0-20191029155653-36fa32f443ba/go.mod h1:HcRNieBy4k2gr4Q6iilWxJnA64GdbOds4o8Iz2/G1UY=
github.com/256dpi/jsonapi v1.3.0 h1:D0VoODA1OVWl2nr0bUXIdV/3CteoRpaRuYCPKR8vDAw=
github.com/256dpi/jsonapi v1.3.0/go.mod h1:Ko0ntY8Q7gz7L4tGM0adOU+xU1Qs3HmSbTyqKy2VY84=
github.com/256dpi/lungo v0.1.6 h1:9XyVNZijvB6IOFdkGg7JVbClm8cHqL7EExO42Gs7jlk=
github.com/256dpi/lungo v0.1.6/go.mod h1:k7P2WdslV9e+vW1LHK6OTnYss1hSG/r7mEzJVO2+kpM=
github.com/256dpi/lungo v0.1.7 h1:OL1lC+8bOJZgI+MAT3BntAz8EuHjuc5E1RDtNQDK7Rk=
github.com/256dpi/lungo v0.1.7/go.mod h1:k7P2WdslV9e+vW1LHK6OTnYss1hSG/r7mEzJVO2+kpM=
github.com/256dpi/oauth2 v1.3.0 h1:7w71KHYnxc7foY1iMleXLom7dCULxhdXjJdTbxZxvV0=
github.com/256dpi/oauth2 v1.3.0/go.mod h1:3gv0Juo4JalW4DQbmX4dTm7q04GGTmDwhm0CqRRHpVo=
github.com/256dpi/serve v0.1.2 h1:SDv6HBxKnaWJ1a8MNm/eCK067EvPhs4ZKPt3igPwR6o=
Expand Down

0 comments on commit d190a29

Please sign in to comment.