Skip to content

Commit

Permalink
Use the index unique to make sure the unique of display name
Browse files Browse the repository at this point in the history
  • Loading branch information
hwchiu committed Jun 22, 2018
1 parent 1b108c3 commit 504688c
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/server/handler_storage_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,20 @@ func CreateStorageProvider(ctx *web.Context) {
}

session := sp.Mongo.NewSession()
session.C(entity.StorageProviderCollectionName).EnsureIndex(mgo.Index{
Key: []string{"displayName"},
Unique: true,
})
defer session.Close()
// Check whether this displayname has been used
query := bson.M{"displayName": storageProvider.DisplayName}
count, err := session.Count(entity.StorageProviderCollectionName, query)
if err != nil && err.Error() != mgo.ErrNotFound.Error() {
response.InternalServerError(req.Request, resp.ResponseWriter, err)
return
}
if count > 0 {
response.Conflict(req.Request, resp, fmt.Errorf("displayName: %s already existed", storageProvider.DisplayName))
return
}

storageProvider.ID = bson.NewObjectId()
storageProvider.CreatedAt = timeutils.Now()
if err := session.Insert(entity.StorageProviderCollectionName, &storageProvider); err != nil {
response.InternalServerError(req.Request, resp.ResponseWriter, err)
if mgo.IsDup(err) {
response.Conflict(req.Request, resp.ResponseWriter, fmt.Errorf("The name %s is existed", storageProvider.DisplayName))
} else {
response.InternalServerError(req.Request, resp.ResponseWriter, err)
}
return
}

Expand Down

0 comments on commit 504688c

Please sign in to comment.